如何使用cmake使相对文件路径在visual studio中工作? [英] How to make relative file path work in visual studio using cmake?
问题描述
我们正在使用 CMake 开发一个 C++ 项目.在项目中,我们使用 std::ifstream
类引用一些资源文件,例如 resources/file.txt
(直接位于项目文件夹内).现在的问题是,虽然我们的其他团队成员可以在使用 Linux 时引用这些文件,但我不能(我使用的是 Visual Studio 2017).
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 或 Visual Studio,以便在不更改其路径的情况下引用这些文件?我仍然是使用 CMake 的新手,但我没有找到任何有效的方法.
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.
显示问题的最少代码:
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;
}
此程序应加载位于 src 文件夹内的Test_File.txt",文件夹结构为:
This program should load the "Test_File.txt" located inside the src folder with the folder structure being:
- 源代码
- main.cpp
- CMakeList.txt
- Test_File.txt
这里的问题是程序找不到Text_File.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.
我已经尝试使用
VS_DEBUGGER_WORKING_DIRECTORY
参数与set_target_properties(${PROJECT_NAME} PROPERTIES VS_DEBUGGER_WORKING_DIRECTORY "${CMAKE_SOURCE_DIR}")
在add_executable(${PROJECT_NAME}CMOURDIR} 后的
cpp")
但这似乎也不起作用.I've already tried to set the working directory using the
VS_DEBUGGER_WORKING_DIRECTORY
parameter withset_target_properties(${PROJECT_NAME} PROPERTIES VS_DEBUGGER_WORKING_DIRECTORY "${CMAKE_SOURCE_DIR}")
afteradd_executable(${PROJECT_NAME} "${CMAKE_CURRENT_SOURCE_DIR}/main.cpp")
but that does not seem to work either.推荐答案
多亏了@Tsyvarev,经过多次尝试,我终于可以正常工作了.正如在这个问题中所说的那样,设置currentDir"有效.对我来说,它以前不起作用,因为
"name"
和"projectTarget"
设置不正确.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.我现在在修复
"name"
和"projectTarget"
值后添加了"currentDir": "${workspaceRoot}"
并且它工作.I now added
"currentDir": "${workspaceRoot}"
after fixing the"name"
and"projectTarget"
values and it worked.这篇关于如何使用cmake使相对文件路径在visual studio中工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!