cmake RUNTIME_OUTPUT_DIRECTORY on Windows
Asked Answered
C

9

30

I'm using 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 to copy the executable to the proper directory, or (even better) stop using these sub-directories altogether?

Cecilececiley answered 12/2, 2009 at 20:55 Comment(2)
I assume you are using cmake to generate Visual Studio solution/project files, is that right? What version of Visual Studio is used? What version of cmake?Glen
Yes, I'm using CMake to generate the solution files (cmake 2.6, VS 2005)Cecilececiley
F
23

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}")
Fluting answered 15/8, 2014 at 14:12 Comment(2)
I haven't looked around for solution. And came across this answer and at the same time I found this link: cmake.org/Bug/view.php?id=9163. This answer uses exactly the same technique. I think this answer is the best solution so far. Works pretty well for me. Thanks.Brueghel
For anyone stumbling onto this recently, those variables are always undefined as of CMake v3.10.Cai
S
12

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

Sniffy answered 16/6, 2009 at 6:20 Comment(1)
This works fine for me too. For .lib files use SET_TARGET_PROPERTIES(${PROJECT_NAME} PROPERTIES IMPORT_PREFIX "../")Hoist
B
1

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.

Brandling answered 17/2, 2009 at 18:36 Comment(0)
C
1

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.

Centrosphere answered 22/2, 2009 at 17:15 Comment(0)
C
1

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.

Cecilececiley answered 21/9, 2009 at 17:0 Comment(0)
B
1

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)

Bolte answered 9/9, 2011 at 16:28 Comment(0)
E
1

now there is a simple solution. cmake said:

Multi-configuration generators (Visual Studio, Xcode, Ninja Multi-Config) append a per-configuration subdirectory to the specified directory unless a generator expression is used.

so we can use a generator expression. like below:

set_target_properties(foo PROPERTIES ARCHIVE_OUTPUT_DIRECTORY $<1:${PROJECT_SOURCE_DIR}/lib>)
set_target_properties(foo PROPERTIES LIBRARY_OUTPUT_DIRECTORY $<1:${PROJECT_SOURCE_DIR}/lib>)
set_target_properties(foo PROPERTIES RUNTIME_OUTPUT_DIRECTORY $<1:${PROJECT_SOURCE_DIR}/bin>)

if you want to put to config dir, use this:

set_target_properties(foo PROPERTIES ARCHIVE_OUTPUT_DIRECTORY ${PROJECT_SOURCE_DIR}/output/$<CONFIG>/lib)
set_target_properties(foo PROPERTIES LIBRARY_OUTPUT_DIRECTORY ${PROJECT_SOURCE_DIR}/output/$<CONFIG>/lib)
set_target_properties(foo PROPERTIES RUNTIME_OUTPUT_DIRECTORY ${PROJECT_SOURCE_DIR}/output/$<CONFIG>/bin)
Erwinery answered 7/11, 2022 at 6:23 Comment(0)
S
0

The answer of hgyxbll worked for me on a global bases( for all targets )

set(CMAKE_RUNTIME_OUTPUT_DIRECTORY $<1:${PROJECT_SOURCE_DIR}/bin>)
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY $<1:${PROJECT_SOURCE_DIR}/bin>)
Shillong answered 7/10, 2023 at 17:0 Comment(0)
C
-5

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.

Celanese answered 20/2, 2009 at 14:1 Comment(1)
Each time one runs CMake, it regenerates the Visual Studio project; so hacking that project by hand is certainly not a good idea.Pytlik

© 2022 - 2024 — McMap. All rights reserved.