如何在CMAKE Visual Studio 2022中设置工作目录?
我在使用CMAKE设置工作目录(Visual Studio 2022)时有问题。
我目前正在从事一个项目(一些OpenGL学习内容),并决定从典型的VS解决方案项目转换为CMAKE项目。我需要从 Resources 文件夹(LearnOpengl/Resources)加载一些文件(.OBJ,着色器),但是我看到C ++代码中的路径相对于 LearnOpengl/learnopengl/out of/build/build/x64-debug /。
我已经尝试过:
- 设置属性vs_debugger_working_directory喜欢(也不落后斜线):
set_property(TARGET LearnOpenGL PROPERTY VS_DEBUGGER_WORKING_DIRECTORY "${CMAKE_SOURCE_DIR}/")
- 将“ CurrentDir”或“ CWD”添加到 cmakesettings.json.json 喜欢:
"currentDir": "${projectDir}"
"cwd": "${projectDir}"
但是没有任何更改的效果。有人有一些想法,我还能做什么?还是我在这里做错了什么?
I have a problem with setting the working directory with 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/.
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以将 CurrentDir 属性添加到您的 lainmy.vs.json ,通常在 $ {WorksPaceroot}/。vs Directory中。
要从Visual Studio 2022 CMake项目访问它,您可以按照以下步骤进行以下步骤:
在解决方案资源管理器中,单击解决方案和可用视图之间的切换按钮按钮:
然后单击 cmake cmake目标查看

现在右键单击您的项目,然后按在上下文菜单中添加debug配置
这将打开 abound.vs.json 文件您可以在其中编辑 currentdir 属性,例如 - 我的项目 02_texture.exe 应该从根目录中开始,因此我的启动配置看起来像这样:
You can add currentDir property to your launch.vs.json which will typically be in ${workspaceRoot}/.vs directory.
To access it from Visual Studio 2022 CMake project you can follow these steps:
In the solution explorer click on Switch between solutions and available views button:
Then click on CMake Targets View

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:
作为与Stjepano的答案有关的附加提示,还可以保存您旁边的
abound.vs.json
文件cmakelists.txt
。这很方便,因为它允许在源代码存储库中保存abougn.vs.json
文件,因此,与您的同事共享该配置(其默认位置,在中。 vs
文件夹不可能成为可能,因为该文件夹不应合并)。我找不到微软网站上的文档,解释了这是可能的,并且不得不依靠盲实验来找到此“隐藏”功能,所以我很有趣。
As an additional tip related to the answer from stjepano, it is also possible to save that
launch.vs.json
file next to youCMakeLists.txt
. This is convenient as it allows to save thelaunch.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).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: