CMake可执行文件位置
我有一个非常简单的目录结构:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
你有几个选择。
要更改可执行文件的默认位置,请将
CMAKE_RUNTIME_OUTPUT_DIRECTORY
设置为所需位置。例如,如果您在
add_subdirectory
命令之前添加Project/CMakeLists.txt
,则您的可执行文件最终将位于Project/ build
用于 Unix 构建,或build/
用于 Win32 构建。有关更多详细信息,请运行:对于这种规模的项目,另一种选择是只有一个 CMakeLists.txt。您或多或少可以将
add_subdirectory(src)
替换为Project/src/CMakeLists.txt
的内容,以实现相同的输出路径。然而,还有一些进一步的问题。
您可能通常希望避免使用
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> 可能看起来像:
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 addto your
Project/CMakeLists.txt
before theadd_subdirectory
command, your executable will end up inProject/build
for Unix builds orbuild/<config type>
for Win32 builds. For further details, run: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 ofProject/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, runEven 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 anif (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:重新定位可执行文件位置的另一种方法是通过 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)
对于 MSVC,避免默认创建的“/Debug”文件夹
For MSVC, to avoid the default "/Debug" created folder