cmake RUNTIME_OUTPUT_DIRECTORY on Windows

十年热恋 提交于 2019-12-03 05:53:15

问题


I'm using cmake for managing my cross-platform builds, and I have everything worked out except for this problem. I set RUNTIME_OUTPUT_DIRECTORY to a bin/ directory where I have data files stored. On Linux, this works fine. On Windows, the executables get placed in the Debug/Release sub-directory depending on the build type. Is there any way to get cmake to copy the executable to the proper directory, or (even better) stop using these sub-directories altogether?


回答1:


I've been using the fine prefix property hack reported by Ogapo for years. It works.

But as of CMake version 2.8, there is official support for avoiding the Release/Debug subdirectories on Windows.

Use either the global CMAKE_<ARTIFACT>_OUTPUT_DIRECTORY_<CONFIGURATION> variables, or the per-target <ARTIFACT>_OUTPUT_DIRECTORY_<CONFIGURATION> properties, like so:

SET( CMAKE_RUNTIME_OUTPUT_DIRECTORY_DEBUG "${OUTPUT_DIRECTORY}")
SET( CMAKE_RUNTIME_OUTPUT_DIRECTORY_RELEASE "${OUTPUT_DIRECTORY}")
SET( CMAKE_LIBRARY_OUTPUT_DIRECTORY_DEBUG "${OUTPUT_DIRECTORY}")
SET( CMAKE_LIBRARY_OUTPUT_DIRECTORY_RELEASE "${OUTPUT_DIRECTORY}")
SET( CMAKE_ARCHIVE_OUTPUT_DIRECTORY_DEBUG "${OUTPUT_DIRECTORY}")
SET( CMAKE_ARCHIVE_OUTPUT_DIRECTORY_RELEASE "${OUTPUT_DIRECTORY}")



回答2:


I am not sure if these directories are intentional or a bug, but at the risk of forward incompatibility you could add:

if (MSVC_IDE)
    # hack to get around the "Debug" and "Release" directories cmake tries to add on Windows
    set_target_properties (${NAME} PROPERTIES PREFIX "../")
endif()

this has been working for me




回答3:


I found a few good discussions on this topic:

http://www.cmake.org/pipermail/cmake/2008-April/021355.html

http://www.vtk.org/Bug/bug_view_advanced_page.php?bug_id=8366

Would it be possible to use the deprecated EXECUTABLE_OUTPUT_PATH instead of RUNTIME_OUTPUT_DIRECTORY? I'm not sure what functionality has changed between the 2, but it might be worth a try.




回答4:


Some cmake variables have build specific versions.

CMAKE_C_FLAGS 
    the compiler flags for compiling C sources. Note you can also specify switches with ADD_DEFINITIONS(). 
CMAKE_C_FLAGS_DEBUG 
    compiler flags for compiling a debug build from C sources. 
CMAKE_C_FLAGS_RELEASE 
    compiler flags for compiling a release build from C sources. 
CMAKE_C_FLAGS_RELWITHDEBINFO 
    compiler flags for compiling a release build with debug flags from C sources. 

I have not verified these vars exist, but maybe setting RUNTIME_OUTPUT_DIRECTORY_DEBUG && RUNTIME_OUTPUT_DIRECTORY_RELEASE to the same thing might work.




回答5:


So far, the best answer I've found is to simply write CMake install commands for each of my targets and data files, and set up the MSVC debugger to run out of the install directory. This has the added benefit of using CPack for creating installers.




回答6:


For me a global:

set(CMAKE_STATIC_LIBRARY_PREFIX "../lib")
set(CMAKE_SHARED_LIBRARY_PREFIX "../lib")

did the work, so I don't have to set it for each library (CMake 2.8.4)




回答7:


You need to change the build location of your visual studio projects. Go to Project Properties and on the Compile tab specify the 'Build output path' to be wherever you wish.

Note

Do not know how relevant this is as I don't know about CMake.

You can use the following token in Visual Studio build events:

$(TargetPath)

This will be the path to the location that your project is built to so depending on your project settings this will either be the Debug or Release folder.



来源:https://stackoverflow.com/questions/543203/cmake-runtime-output-directory-on-windows

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!