I have a problem with setting the working directory with CMake (Visual Studio 2022).
我在使用CMake(Visual Studio 2022)设置工作目录时遇到问题。
I'm currently working on a project (some OpenGL learning stuff) and decided to switch from typical VS solution-project to CMake project. I need to load some files (.obj, shaders) from Resources folder (LearnOpenGL/Resources) but I see that paths in c++ code are relative to LearnOpenGL/out/build/x64-Debug/.
我目前正在做一个项目(一些OpenGL学习材料),并决定从典型的VS解决方案项目切换到CMake项目。我需要从Resources文件夹(LearnOpenGL/Resources)加载一些文件(.obj、着色器),但我看到C++代码中的路径是相对于LearnOpenGL/out/Build/x64-Debug/的。
I've already tried :
我已经试过了:
- setting property VS_DEBUGGER_WORKING_DIRECTORY like (also without trailing slash):
set_property(TARGET LearnOpenGL PROPERTY VS_DEBUGGER_WORKING_DIRECTORY "${CMAKE_SOURCE_DIR}/")
- adding "currentDir" or "cwd" to CMakeSettings.json like:
"currentDir": "${projectDir}"
"cwd": "${projectDir}"
but there is no effect of any of those changes. Does anyone have some ideas what else can I do? Or maybe I'm doing here something wrong?
但这些变化都没有任何影响。有没有人知道我还能做什么?或者也许我在这里做错了什么?
更多回答
How do you run your executable? The property VS_DEBUGGER_WORKING_DIRECTORY
affects only on running the executable inside Visual Studio by hitting "debug" or "run".
您如何运行您的可执行文件?VS_DEBUGER_WORKING_DIRECTORY属性仅在通过点击“DEBUG”或“RUN”在Visual Studio中运行可执行文件时才会影响。
@Tsyvarev I've selected the CMakeLists.txt as a startup item, a then pressed the button (with green arrow) to run the project, is that what you're referring to? I also run once or twice by pressing F10 to immediately start debugging.
@Tsyvarev我选择了CMakeLists.txt作为启动项,然后按下按钮(带绿色箭头)来运行项目,这是你指的吗?我还通过按F10立即开始调试来运行一到两次。
Hm, in that case setting the property VS_DEBUGGER_WORKING_DIRECTORY
should work. Not sure why it doesn't work for you.
嗯,在这种情况下,设置属性VS_DEBUGER_WORKING_DIRECTORY应该可以。不知道为什么它对你不起作用。
Have you selected your executable as startup project via the solution explorer? How exactly does the program fail? Does it run, but just cannot open some files or is there some different error, like e.g. dlls not being found?
是否已通过解决方案资源管理器选择可执行文件作为启动项目?这个计划到底是怎么失败的?它是否运行,但就是无法打开一些文件,还是有一些不同的错误,例如,找不到dll?
@fabian I've selected CMakeLists.txt in solution explorer as startup item. The only problem I encounter is that it can't open some files (e.g. 3D object models - .obj), the program works - it runs, it creates window, it prints messages in console etc.
@Fabian我已在解决方案资源管理器中选择了CMakeLists.txt作为启动项目。我遇到的唯一问题是它无法打开一些文件(例如3D对象模型-.obj),程序工作-它运行,它创建窗口,它在控制台打印消息等。
You can add currentDir property to your launch.vs.json which will typically be in ${workspaceRoot}/.vs directory.
您可以将CurrentDir属性添加到您的Launch.vs.json,该属性通常位于${workspaceRoot}/.vs目录中。
To access it from Visual Studio 2022 CMake project you can follow these steps:
若要从Visual Studio 2022 CMake项目访问它,您可以执行以下步骤:
In the solution explorer click on Switch between solutions and available views button:
在解决方案资源管理器中,单击在解决方案和可用视图之间切换按钮:
Then click on CMake Targets View
然后单击CMake目标视图
Now right click on your project and press Add Debug Configuration in context menu
现在,右键单击您的项目,然后在上下文菜单中按下添加调试配置
This will open launch.vs.json file where you can edit currentDir property, for example - my project 02_texture.exe should start in root directory so my launch config looks like this:
这将打开启动.vs.json文件,您可以在其中编辑CurentDir属性,例如,我的项目02_texture.exe应该在根目录中启动,因此我的启动配置如下所示:
{
"version": "0.2.1",
"defaults": {},
"configurations": [
{
"type": "default",
"project": "CMakeLists.txt",
"projectTarget": "",
"name": "CMakeLists.txt"
},
{
"type": "default",
"project": "CMakeLists.txt",
"projectTarget": "02_texture.exe (02_texture\\02_texture.exe)",
"name": "02_texture.exe (02_texture\\02_texture.exe)",
"currentDir": "${workspaceRoot}"
}
]
}
As an additional tip related to the answer from stjepano, it is also possible to save that launch.vs.json
file next to you CMakeLists.txt
. This is convenient as it allows to save the launch.vs.json
file in your source code repository and, consequently, share that configuration with your co-workers (its default location, in the .vs
folder doesn't make it possible as that folder should not be commited).
作为与stjepano的回答相关的额外提示,还可以在您的CMakeLists.txt旁边保存启动.vs.json文件。这很方便,因为它允许在您的源代码存储库中保存Launch.vs.json文件,并因此与您的同事共享该配置(其默认位置在.vs文件夹中,这是不可能的,因为该文件夹不应该被提交)。
I could not find documentation on Microsoft's website explaining this was possible and had to rely on blind experiments to find this "hidden" feature, so I though it was interesting to note.
我在微软的网站上找不到解释这一可能性的文档,不得不依靠盲目实验来找到这个“隐藏”的功能,所以我觉得这很有趣。
I'm also currently working myself through the examples of https://www.learnopengl.com and I also use CMake to generate Visual Studio solutions / projects.
I ran into the same issue with loading shaders and textures. They were not found when running the sample apps from Visual Studio Debugger. My solution was:
我目前也在研究https://www.learnopengl.com的示例,我还使用CMake来生成Visual Studio解决方案/项目。我在加载着色器和纹理时遇到了同样的问题。从Visual Studio调试器运行示例应用程序时未找到它们。我的解决方案是:
- copy shaders and textures via a short npm script to both the build/Debug and build/Release subdirectories. You could also use a batch file.
- In CMakeLists.txt, add the following code so that visual studio debugger runs in build/Debug or build/Release respectively and not in build/ which is default:
#set visual studio's working directory for debugging to 'build/debug' respectively 'build/release' instead of 'build'
set_property(TARGET triangle-sample PROPERTY VS_DEBUGGER_WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_CFG_INTDIR})
更多回答
It worked! Thank you so much! I need to mention one more thing - after those steps I had to also change the project in the "start" button (the one with green arrow) so it could work.
啊,真灵!非常感谢!我需要再提一件事--在这些步骤之后,我还必须在“Start”按钮(带绿色箭头的那个)中更改项目,以便它可以工作。
Note: the context option seems to now be under "[Right Click] -> Debug and Launch Settings -> [Target Name]" instead of "[Right Click] -> Add Debug Configuration".
注意:上下文选项现在似乎位于“[Right Click]->Debug and Launch Settings->[Target Name]”下,而不是“[Right Click]->Add Debug Configuration”。
When running from command-line in case you don't need to debug just change current working directory to build/Debug or build/Release and start the app.
从命令行运行时,如果您不需要调试,只需将当前工作目录更改为Build/Debug或Build/Release并启动应用程序。
我是一名优秀的程序员,十分优秀!