CMake可执行文件位置 [英] CMake executable location

查看:740
本文介绍了CMake可执行文件位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a very simple directory structure:

Project
Project/src
Project/build

源文件位于 Project / src 中,然后在 Project / build 中执行out-of-src构建。运行 cmake ../之后; make ,这样就可以运行可执行文件: Project / build $ src / Executable -即 Executable build / src 目录中创建。

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.

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

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.

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

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})

和顶级 Project / CMakeLists.txt


cmake_minimum_required (VERSION 2.6)
project (SBSProject)

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

add_subdirectory(src)


推荐答案

您有两种选择。

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

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})

Project / CMakeLists.txt 之前 add_subdirectory 命令,您的可执行文件将最终出现在 Project / build (对于Unix构建)或 build /< config type> ; 用于Win32版本。有关更多详细信息,请运行:

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

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

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

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

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

另一个问题是 CMAKE_CXX_FLAGS 适用于Unix构建,因此应该将其包装在 if(UNIX)... endif()块中。当然,如果您不打算在Unix以外的任何版本上构建,则这不是问题。

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.

最后,我建议将CMake 2.8作为最低,除非必须使用2.6-CMake是一个积极开发的项目,当前版本比2.6有了很多显着改进。

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

因此,单个替换 Project / CMakeLists.txt 看起来像是:

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})

这篇关于CMake可执行文件位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆