In CMake, how do I work around the Debug and Release directories Visual Studio 2010 tries to add?
It depends a bit on what you want precisely, but I would recommend to take a look at the available target properties, similar to this question.
It depends a bit on what you want exactly. For each target, you could manually set the library_output_directory or runtime_output_directory properties.
if ( MSVC )
set_target_properties( ${targetname} PROPERTIES LIBRARY_OUTPUT_DIRECTORY ${youroutputdirectory} )
set_target_properties( ${targetname} PROPERTIES LIBRARY_OUTPUT_DIRECTORY_DEBUG ${youroutputdirectory} )
set_target_properties( ${targetname} PROPERTIES LIBRARY_OUTPUT_DIRECTORY_RELEASE ${youroutputdirectory} )
# etc for the other available configuration types (MinSizeRel, RelWithDebInfo)
endif ( MSVC )
You could also do this globally for all sub-projects, using something like this:
# First for the generic no-config case (e.g. with mingw)
set( CMAKE_RUNTIME_OUTPUT_DIRECTORY ${youroutputdirectory} )
set( CMAKE_LIBRARY_OUTPUT_DIRECTORY ${youroutputdirectory} )
set( CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${youroutputdirectory} )
# Second, for multi-config builds (e.g. msvc)
foreach( OUTPUTCONFIG ${CMAKE_CONFIGURATION_TYPES} )
string( TOUPPER ${OUTPUTCONFIG} OUTPUTCONFIG )
set( CMAKE_RUNTIME_OUTPUT_DIRECTORY_${OUTPUTCONFIG} ${youroutputdirectory} )
set( CMAKE_LIBRARY_OUTPUT_DIRECTORY_${OUTPUTCONFIG} ${youroutputdirectory} )
set( CMAKE_ARCHIVE_OUTPUT_DIRECTORY_${OUTPUTCONFIG} ${youroutputdirectory} )
endforeach( OUTPUTCONFIG CMAKE_CONFIGURATION_TYPES )
CMake set different Debug and Release working directories in Visual Studio
If there are no configuration specific properties and generator expressions like $<CONFIG>
don't work (tested it with VS_DEBUGGER_WORKING_DIRECTORY
to no avail), you can still use VS variables:
set_target_properties(${PROJECT_BIN} PROPERTIES
VS_DEBUGGER_WORKING_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/$(Configuration)")
Move cmake release and debug folder
So i found my answer by myself. I think i've not explain correctly for my first question. It was for just remove the folder after build. The following commands are working :
set(BIN_PATH "../path") -> define your principal path
set( CMAKE_ARCHIVE_OUTPUT_DIRECTORY_DEBUG "${BIN_PATH}/bin/" ) ->define path for archive
set( CMAKE_LIBRARY_OUTPUT_DIRECTORY_DEBUG "${BIN_PATH}/bin/" ) ->define path for Library
set( CMAKE_RUNTIME_OUTPUT_DIRECTORY_DEBUG "${BIN_PATH}/bin/" ) ->define path for Runtime
Normally, all the files are created in /bin.
How to not add Release or Debug to output path?
A similar question was asked a few months ago, where I advised the use of target properties and also referred to another answer. For MSVC you can completely specify the locations of executables, libraries, archives, etc. on a per-configuration basis.
E.g. using something like:
if ( MSVC )
set_target_properties( ${targetname} PROPERTIES LIBRARY_OUTPUT_DIRECTORY ${youroutputdirectory} )
set_target_properties( ${targetname} PROPERTIES LIBRARY_OUTPUT_DIRECTORY_DEBUG ${youroutputdirectory} )
set_target_properties( ${targetname} PROPERTIES LIBRARY_OUTPUT_DIRECTORY_RELEASE ${youroutputdirectory} )
# etc for the other available configuration types (MinSizeRel, RelWithDebInfo)
endif ( MSVC )
which will put all your libraries in a single output-directory ${youroutputdirectory}, whether it is in Debug or Release config.
How to refer to Visual Studio output directory in CMakeLists.txt?
I solved my problem by copying the files with the "file (GENERATE OUTPUT)" which supports the cmake-generator-expressions suggested by StAlphonzo:
foreach (file ${SHADER_SOURCES})
file (GENERATE OUTPUT $<CONFIG>/${file} INPUT ${PROJECT_SOURCE_DIR}/${file})
endforeach()
I would also like to mention a second way of solving this problem I discovered that I think is actually more correct, by adding a custom command:
add_custom_command(TARGET target ${CMAKE_COMMAND} -E copy ${PROJECT_SOURCES_DIR}/foo $<TARGET_FILE_DIR:target>)
Linking different libraries for Debug and Release builds in Cmake on windows?
According to the CMake documentation:
target_link_libraries(<target> [lib1 [lib2 [...]]] [[debug|optimized|general] <lib>] ...)
A "debug", "optimized", or "general"
keyword indicates that the library
immediately following it is to be used
only for the corresponding build
configuration.
So you should be able to do this:
add_executable( MyEXE ${SOURCES})
target_link_libraries( MyEXE debug 3PDebugLib)
target_link_libraries( MyEXE optimized 3PReleaseLib)
How to change the executable output directory for Win32 builds, in CMake?
There are several options:
- Copy the executable after building
- Customizing the output-directory for your executable(s)
Copy the executable after building
After a succesful build you can copy the executable (see Beginners answer), but perhaps it is nicer to use an install target:
Use the install command to specify targets (executables, libraries, headers, etc.) which will be copied to the CMAKE_INSTALL_PREFIX directory. You can specify the CMAKE_INSTALL_PREFIX on the commandline of cmake (or in the cmake GUI).
Customizing the output-directory for your executable(s)
Warning: It is not advised to set absolute paths directly in your cmakelists.txt.
Use set_target_properties to customize the RUNTIME_OUTPUT_DIRECTORY
set_target_properties( yourexe PROPERTIES RUNTIME_OUTPUT_DIRECTORY E:/parsec/bin/ )
As an alternative, modifying the CMAKE_RUNTIME_OUTPUT_DIRECTORY allows you to specify this for all targets in the cmake project. Take care that you modify the CMAKE_LIBRARY_OUTPUT_DIRECTORY as well when you build dlls.
set( CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin )
set( CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib )
Additional info: Take a look at these questions:
In CMake, how do I work around the Debug and Release directories Visual Studio 2010 tries to add?
CMake : Changing name of Visual Studio and Xcode exectuables depending on configuration in a project generated by CMake
How to not add Release or Debug to output path?
Building yaml-cpp with cmake and visual studio 2010
Classic Pebkac. After a good nights sleep I found the file in the project.
libyaml-cppmdd.lib was generated in the Debug folder. I had forgotten to add .lib to the file ending while adding the file to Additional dependencies to the Linker. Therefore the Linker thought it was a .obj file and complained.
How to get current configuration (Release/Debug) in CMake for Visual Studio
The file
command is executed during CMake runtime, not during build time (i.e. VS runtime).
This also means, that the generator expressions (e.g. $<CONFIG>
) can not be used, as these are evaluated during build time.
(Hint: As long as there is no explicit reference to the use of generator expressions for a particular command in the CMake docu, they are not supported by that command).
The reason, why ${CMAKE_BUILD_TYPE}
is empty, is due to the reason that you probably haven't specified it on the invocation of CMake:
cmake -DCMAKE_BUILD_TYPE=Debug ..
However, using that, would mean that the build files are only generated for the Debug configuration. That's obviously not what you want.
To solve your problem: Using generator expressions is the right way, as you've already figured out with the use of add_custom_target
(or add_custom_command
).
You can use custom commands as dependencies for other "real" targets and you can specify post-/pre-build and pre-link commands for a specific target via add_custom_command
.
As the docu states for the COMMAND
argument of add_custom_command
:
Arguments to
COMMAND
may use generator expressions. References to target names in generator expressions imply target-level dependencies, but NOT file-level dependencies. List target names with theDEPENDS
option to add file-level dependencies.
To copy a file after a successful build of a target:
add_custom_command(TARGET myTarget POST_BUILD
COMMAND "${CMAKE_COMMAND}" -E copy_if_different "${IMAGE1}" "${CMAKE_BINARY_DIR}/bin/$<CONFIG>/"
COMMAND "${CMAKE_COMMAND}" -E copy_if_different "${IMAGE2}" "${CMAKE_BINARY_DIR}/bin/$<CONFIG>/"
)
Related Topics
Serial Port (Rs -232) Connection in C++
What Does "Typedef Void (*Something)()" Mean
C++ How to Allocate Memory Dynamically on Stack
Link Error "Undefined Reference to '_Gxx_Personality_V0'" and G++
Should I Use Printf in My C++ Code
Counting the Number of Lines in a Text File
Why Is Std::Vector::Operator[] 5 to 10 Times Faster Than Std::Vector::At()
C++ Standard Practice: Virtual Interface Classes VS. Templates
How to Initialize and Print a Std::Wstring
How to Use Memcpy in C++ to Copy Classes That Have No Pointers or Virtual Functions
Set Local Environment Variables in C++
Libzip with Visual Studio 2010
C++: How to Implement a Timeout for an Arbitrary Function Call
Std::Enable_If:Parameter VS Template Parameter
Getline Keeps on Getting Newline Character. How to Avoid This