C++ 如何将 CMake 输出变成“bin”目录?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6594796/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 20:23:50  来源:igfitidea点击:

How do I make CMake output into a 'bin' dir?

c++pluginscmake

提问by Martin Kristiansen

I'm currently constructing a project with a plugin structure. I'm using CMake to compile the project. The plugins are compiled in separate directories. My problem is that CMake compiles and saves the binaries and plugins, dynamic libraries, in the directory structure of the source. How do I make CMake save the files in something like a ./bindirectory?

我目前正在构建一个带有插件结构的项目。我正在使用 CMake 编译项目。插件编译在单独的目录中。我的问题是 CMake 在源的目录结构中编译并保存二进制文件和插件、动态库。如何让 CMake 将文件保存在./bin目录中?

回答by Adam Bowen

As in Oleg's answer, I believe the correct variable to set is CMAKE_RUNTIME_OUTPUT_DIRECTORY. We use the following in our root CMakeLists.txt:

在 Oleg 的回答中,我相信要设置的正确变量是CMAKE_RUNTIME_OUTPUT_DIRECTORY。我们在根 CMakeLists.txt 中使用以下内容:

set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)

You can also specify the output directories on a per-target basis:

您还可以为每个目标指定输出目录:

set_target_properties( targets...
    PROPERTIES
    ARCHIVE_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/lib"
    LIBRARY_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/lib"
    RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/bin"
)

In both cases you can append _[CONFIG]to the variable/property name to make the output directory apply to a specific configuration (the standard values for configuration are DEBUG, RELEASE, MINSIZERELand RELWITHDEBINFO).

在这两种情况下,你可以追加_[CONFIG]到变量/属性名称,使输出目录适用于特定的配置(配置标准值是DEBUGRELEASEMINSIZERELRELWITHDEBINFO)。

回答by ovk

Use set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "/some/full/path/to/bin")

set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "/some/full/path/to/bin")

回答by gtikok

Use the EXECUTABLE_OUTPUT_PATHCMake variable to set the needed path. For details, refer to the online CMake documentation:

使用EXECUTABLE_OUTPUT_PATHCMake 变量设置所需的路径。有关详细信息,请参阅在线 CMake 文档:

CMake 2.8.8 Documentation

CMake 2.8.8 文档

回答by Jayhello

As to me I am using cmake 3.5, the below(set variable) does not work:

对我而言,我使用的是 cmake 3.5,以下(set variable)不起作用:

set(
      ARCHIVE_OUTPUT_DIRECTORY "/home/xy/cmake_practice/lib/"
      LIBRARY_OUTPUT_DIRECTORY "/home/xy/cmake_practice/lib/"
      RUNTIME_OUTPUT_DIRECTORY "/home/xy/cmake_practice/bin/"
)

but this works(set set_target_properties):

但这有效(set set_target_properties):

set_target_properties(demo5
    PROPERTIES
    ARCHIVE_OUTPUT_DIRECTORY "/home/xy/cmake_practice/lib/"
    LIBRARY_OUTPUT_DIRECTORY "/home/xy/cmake_practice/lib/"
    RUNTIME_OUTPUT_DIRECTORY "/home/xy/cmake_practice/bin/"
)

回答by mcandre

$ cat CMakeLists.txt
project (hello)
set(EXECUTABLE_OUTPUT_PATH "bin")
add_executable (hello hello.c)

回答by jdir.s

English is not my native language; please excuse typing errors.

英语不是我的母语;请原谅打字错误。

use this line config :
set(EXECUTABLE_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/build/)
place your any CMakeLists.txt project.
this ${PROJECT_SOURCE_DIR} is your current source directory where project place .
and if wander why is ${EXECUTABLE_OUTPUT_PATH}
check this file CMakeCache.txtthen search the key word output path,
all the variables define here , it would give a full explanation of the project all setting·

使用这一行配置:
set(EXECUTABLE_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/build/)
放置您的任何 CMakeLists.txt 项目。
这个 ${PROJECT_SOURCE_DIR} 是你当前项目所在的源目录。
如果wander为什么${EXECUTABLE_OUTPUT_PATH}
检查这个文件CMakeCache.txt然后搜索关键字output path
这里定义的所有变量,它会给出项目所有的完整解释 setting·

回答by Jerry Miller

Regardless of whether I define this in the main CMakeLists.txt or in the individual ones, it still assumes I want all the libs and bins off the main path, which is the least useful assumption of all.

无论我是在主 CMakeLists.txt 中还是在单独的 CMakeLists.txt 中定义它,它仍然假设我希望所有库和 bin 都远离主路径,这是所有假设中最没有用的假设。

回答by serg06

To add on to this:

补充一下:

If you're using CMAKE to generate a Visual Studiosolution, and you want Visual Studioto output compiled files into /bin, Peter's answer needs to be modified a bit:

如果您使用 CMAKE 生成Visual Studio解决方案,并且希望Visual Studio将编译后的文件输出到 /bin,则需要稍微修改 Peter 的答案:

# set output directories for all builds (Debug, Release, etc.)
foreach( OUTPUTCONFIG ${CMAKE_CONFIGURATION_TYPES} )
    string( TOUPPER ${OUTPUTCONFIG} OUTPUTCONFIG )
    set( CMAKE_ARCHIVE_OUTPUT_DIRECTORY_${OUTPUTCONFIG} ${CMAKE_SOURCE_DIR}/lib )
    set( CMAKE_LIBRARY_OUTPUT_DIRECTORY_${OUTPUTCONFIG} ${CMAKE_SOURCE_DIR}/lib )
    set( CMAKE_RUNTIME_OUTPUT_DIRECTORY_${OUTPUTCONFIG} ${CMAKE_SOURCE_DIR}/bin )
endforeach( OUTPUTCONFIG CMAKE_CONFIGURATION_TYPES )

回答by Meet Modia

cat CMakeLists.txt
project (hello)
set(CMAKE_BINARY_DIR "/bin")
set(EXECUTABLE_OUTPUT_PATH ${CMAKE_BINARY_DIR})
add_executable (hello hello.c)