如何在CMAKE Visual Studio 2022中设置工作目录?

发布于 2025-02-08 08:22:04 字数 713 浏览 2 评论 0原文

我在使用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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

白色秋天 2025-02-15 08:22:04

您可以将 CurrentDir 属性添加到您的 lainmy.vs.json ,通常在 $ {WorksPaceroot}/。vs Directory中。

要从Visual Studio 2022 CMake项目访问它,您可以按照以下步骤进行以下步骤:

在解决方案资源管理器中,单击解决方案和可用视图之间的切换按钮按钮:

然后单击 cmake cmake目标查看

现在右键单击您的项目,然后按在上下文菜单中添加debug配置

“在此处输入映像”

这将打开 abound.vs.json 文件您可以在其中编辑 currentdir 属性,例如 - 我的项目 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}"
    }
  ]
}

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:

enter image description here

Then click on CMake Targets View
enter image description here

Now right click on your project and press Add Debug Configuration in context menu

enter image description here

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:

{
  "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}"
    }
  ]
}
内心荒芜 2025-02-15 08:22:04

作为与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 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).

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.

未央 2025-02-15 08:22:04

我目前还通过

  1. 通过简短的NPM脚本复制着着色器和纹理到构建/调试和构建/发布子目录。您也可以使用批处理文件。
  2. 在cmakelists.txt中,添加以下代码,以使Visual Studio调试器分别在构建/调试或构建/发布中运行,而不是在build/build/是默认情况下:
#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})

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:

  1. 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.
  2. 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})
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文