如何使用cmake在Visual Studio中使相对文件路径工作?

时间:2021-06-15 23:20:44

We are working on a c++ project using CMake. In the project we are referencing some resource files like resources/file.txt (located directly inside the project folder) using the std::ifstream class. The problem now is that, while our other team members can reference these files as they use Linux, I cannot (I am using Visual Studio 2017).

我们正在使用CMake开发一个c ++项目。在项目中,我们使用std :: ifstream类引用一些资源文件,如resources / file.txt(直接位于项目文件夹中)。现在的问题是,虽然我们的其他团队成员可以在使用Linux时引用这些文件,但我不能(我使用的是Visual Studio 2017)。

How can I set up CMake or Visual Studio so I can reference these files without changing their paths? I am still a pretty big novice using CMake but I didn't find anything that worked.

如何设置CMake或Visual Studio,以便我可以在不更改路径的情况下引用这些文件?我仍然是一个使用CMake的新手,但我找不到任何有用的东西。

EDIT:
Minimal code showing the issue:

编辑:显示问题的最小代码:

#include <iostream>
#include <fstream>
#include <string>

std::string read_file(std::string filename)
{
    std::string result = "";

    std::ifstream file(filename);
    if (!file.is_open())
    {
        std::cout << "Could not open file " << filename.c_str() << "." << std::endl;
        return result;
    }

    std::string line;
    while (getline(file, line))
        result += line + '\n';
    return result;
}

int main(int argc, char *argv[])
{
    std::string fileContent = read_file("src/Test_File.txt");

    if (fileContent.compare(""))
    {
        std::cout << fileContent.c_str() << std::endl;
    }

    return 0;
}

This program should load the "Test_File.txt" located inside the src folder with the folder structure being:

该程序应加载位于src文件夹内的“Test_File.txt”,文件夹结构为:

  • src
    • main.cpp
    • CMakeList.txt
    • Test_File.txt
  • src main.cpp CMakeList.txt Test_File.txt

  • CMakeList.txt

The problem here is that the program cannot find the Text_File.txt while the other team members don't have such issue. Absolute paths of course work but they are obviously not the way to go.

这里的问题是程序找不到Text_File.txt而其他团队成员没有这样的问题。绝对路径当然有效,但它们显然不是可行的方法。

I've already tried to set the working directory using the VS_DEBUGGER_WORKING_DIRECTORY parameter with set_target_properties(${PROJECT_NAME} PROPERTIES VS_DEBUGGER_WORKING_DIRECTORY "${CMAKE_SOURCE_DIR}") after add_executable(${PROJECT_NAME} "${CMAKE_CURRENT_SOURCE_DIR}/main.cpp") but that does not seem to work either.

我已经尝试在add_executable($ {PROJECT_NAME}“$ {CMAKE_CURRENT_SOURCE_DIR} /main.cpp”)之后使用带有set_target_properties($ {PROJECT_NAME} PROPERTIES VS_DEBUGGER_WORKING_DIRECTORY“$ {CMAKE_SOURCE_DIR}”)的VS_DEBUGGER_WORKING_DIRECTORY参数设置工作目录,但是似乎也没有用。

1 个解决方案

#1


0  

After some more tries I have got it working thanks to @Tsyvarev. As states in this question they brought up, setting "currentDir" worked. For me it didn't work before because "name" and "projectTarget" were improperly set.

经过一些尝试,我得到了它,感谢@Tsyvarev。正如他们提出的这个问题中的状态,设置“currentDir”有效。对我来说它之前没有用,因为“name”和“projectTarget”设置不正确。

I now added "currentDir": "${workspaceRoot}" after fixing the "name" and "projectTarget" values and it worked.

我现在在修复“name”和“projectTarget”值之后添加了“currentDir”:“$ {workspaceRoot}”并且它有效。

#1


0  

After some more tries I have got it working thanks to @Tsyvarev. As states in this question they brought up, setting "currentDir" worked. For me it didn't work before because "name" and "projectTarget" were improperly set.

经过一些尝试,我得到了它,感谢@Tsyvarev。正如他们提出的这个问题中的状态,设置“currentDir”有效。对我来说它之前没有用,因为“name”和“projectTarget”设置不正确。

I now added "currentDir": "${workspaceRoot}" after fixing the "name" and "projectTarget" values and it worked.

我现在在修复“name”和“projectTarget”值之后添加了“currentDir”:“$ {workspaceRoot}”并且它有效。