CMake可执行文件位置

发布于 2025-01-07 16:21:19 字数 897 浏览 1 评论 0原文

我有一个非常简单的目录结构:

Project
Project/src
Project/build

源文件位于 Project/src 中,并且我在 Project/build 中进行 out-of-src 构建。运行 cmake ../ 后; make,我可以这样运行可执行文件: Project/build$ src/Executable - 也就是说,Executable是在build/src中创建的目录。

如何在 CMakeLists.txt 文件中设置可执行文件的位置?我尝试遵循 cmake.org 中找到的一些示例,但有效的链接似乎并未显示此行为。

此处列出了我的 Project/src/CMakeLists.txt 文件。


include_directories(${SBSProject_SOURCE_DIR}/src)
link_directories(${SBSProject_BINARY_DIR}/src)

set ( SBSProject_SOURCES
    main.cpp
    )

add_executable( TIOBlobs ${SBSProject_SOURCES})

以及顶级 Project/CMakeLists.txt


cmake_minimum_required (VERSION 2.6)
project (SBSProject)

set (CMAKE_CXX_FLAGS "-g3 -Wall -O0") 

add_subdirectory(src)

I have a very simple directory structure:

Project
Project/src
Project/build

Source files are in Project/src, and I do the out-of-src build in Project/build. After running cmake ../ ; make, I can run the executable thusly: Project/build$ src/Executable - that is, the Executable is created in the build/src directory.

How do I set the location of the executable in the CMakeLists.txt file? I've attempted to follow some of the examples found at cmake.org, but the links that work don't seem to show this behaviour.

My Project/src/CMakeLists.txt file is listed here.


include_directories(${SBSProject_SOURCE_DIR}/src)
link_directories(${SBSProject_BINARY_DIR}/src)

set ( SBSProject_SOURCES
    main.cpp
    )

add_executable( TIOBlobs ${SBSProject_SOURCES})

And the top-level Project/CMakeLists.txt:


cmake_minimum_required (VERSION 2.6)
project (SBSProject)

set (CMAKE_CXX_FLAGS "-g3 -Wall -O0") 

add_subdirectory(src)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

睡美人的小仙女 2025-01-14 16:21:19

你有几个选择。

要更改可执行文件的默认位置,请将 CMAKE_RUNTIME_OUTPUT_DIRECTORY 设置为所需位置。例如,如果您

set (CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR})

add_subdirectory 命令之前添加 Project/CMakeLists.txt,则您的可执行文件最终将位于 Project/ build 用于 Unix 构建,或 build/ 用于 Win32 构建。有关更多详细信息,请运行:

cmake --help-property RUNTIME_OUTPUT_DIRECTORY

对于这种规模的项目,另一种选择是只有一个 CMakeLists.txt。您或多或少可以将 add_subdirectory(src) 替换为 Project/src/CMakeLists.txt 的内容,以实现相同的输出路径。

然而,还有一些进一步的问题。

您可能通常希望避免使用 link_directories。有关解释,请运行

cmake --help-command link_directories

即使您确实使用 link_directories,也不太可能在 ${SBSProject_BINARY_DIR}/src 中找到任何库

另一个问题是 CMAKE_CXX_FLAGS 适用于 Unix 版本,因此可能应该包含在 if (UNIX) ... endif() 块中。当然,如果您不打算在 Unix 以外的任何平台上进行构建,那么这不是问题。

最后,我建议至少需要 CMake 2.8,除非您必须使用 2.6 - CMake 是一个积极开发的项目,当前版本比 2.6 有许多重大改进,

因此只需替换 Project/CMakeLists.txt< /code> 可能看起来像:

cmake_minimum_required (VERSION 2.8)
project (SBSProject)

if (UNIX)
  set (CMAKE_CXX_FLAGS "-g3 -Wall -O0")
endif ()

include_directories (${SBSProject_SOURCE_DIR}/src)

set (SBSProject_SOURCES
    ${SBSProject_SOURCE_DIR}/src/main.cpp
    )

add_executable (TIOBlobs ${SBSProject_SOURCES})

You have a couple of choices.

To change the default location of executables, set CMAKE_RUNTIME_OUTPUT_DIRECTORY to the desired location. For example, if you add

set (CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR})

to your Project/CMakeLists.txt before the add_subdirectory command, your executable will end up in Project/build for Unix builds or build/<config type> for Win32 builds. For further details, run:

cmake --help-property RUNTIME_OUTPUT_DIRECTORY

Another option for a project of this size is to have just one CMakeLists.txt. You could more or less replace add_subdirectory(src) with the contents of Project/src/CMakeLists.txt to achieve the same output paths.

However, there are a couple of further issues.

You probably want to avoid using link_directories generally. For an explanation, run

cmake --help-command link_directories

Even if you do use link_directories, it's unlikely that any libraries will be found in ${SBSProject_BINARY_DIR}/src

Another issue is that the CMAKE_CXX_FLAGS apply to Unix builds, so should probably be wrapped in an if (UNIX) ... endif() block. Of course, if you're not planning on building on anything other than Unix, this is a non-issue.

Finally, I'd recommend requiring CMake 2.8 as a minimum unless you have to use 2.6 - CMake is an actively-developed project and the current version has many significant improvements over 2.6

So a single replacement for Project/CMakeLists.txt could look like:

cmake_minimum_required (VERSION 2.8)
project (SBSProject)

if (UNIX)
  set (CMAKE_CXX_FLAGS "-g3 -Wall -O0")
endif ()

include_directories (${SBSProject_SOURCE_DIR}/src)

set (SBSProject_SOURCES
    ${SBSProject_SOURCE_DIR}/src/main.cpp
    )

add_executable (TIOBlobs ${SBSProject_SOURCES})
羅雙樹 2025-01-14 16:21:19

重新定位可执行文件位置的另一种方法是通过 set(EXECUTABLE_OUTPUT_PATH Dir_where_executable_program_is_ located)

Another way of relocating the executable file location is via set(EXECUTABLE_OUTPUT_PATH Dir_where_executable_program_is_located)

薄荷港 2025-01-14 16:21:19

Win32 构建的构建/“配置类型”。

对于 MSVC,避免默认创建的“/Debug”文件夹

set_target_properties(my_target
PROPERTIES 
RUNTIME_OUTPUT_DIRECTORY_DEBUG ${CMAKE_CURRENT_BINARY_DIR})

build/'config type' for Win32 builds.

For MSVC, to avoid the default "/Debug" created folder

set_target_properties(my_target
PROPERTIES 
RUNTIME_OUTPUT_DIRECTORY_DEBUG ${CMAKE_CURRENT_BINARY_DIR})
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文