I am using a external library in my project and I need to to copy a dll next to the exe during the build process. The debug and release dll's are named the same and are in a "Debug" and "Release" directory.
Building with visual studio.
This is what I have right now:
add_custom_command(TARGET App POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy_if_different
${api}/lib/x64/$<CONFIG>/libfbxsdk.dll
$<TARGET_FILE_DIR:App>)
But this fails for RelWithDebInfo - how do I copy the Debug dll to the Debug directory and the Release dll to the Release and RelWithDebInfo directory?
This is what I ended up using...
add_custom_command(TARGET App POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy_if_different
${fbxroot}/lib/x64/$<$<CONFIG:RelWithDebInfo>:Release>$<$<NOT:$<CONFIG:RelWithDebInfo>>:$<CONFIG>>/libfbxsdk.dll
$<TARGET_FILE_DIR:App>)
which seems to be working.
Related
I would like to convert my project from a Visual Studio solution to build with CMake and compile it with Makefiles.
This is a 2-part question.
Right now the CMakeLists.txt is:
cmake_minimum_required(VERSION 3.13.0)
project(Project2015 CXX)
add_executable(Project Source/main.cpp)
When I run cmake .. out of the build directory, it generates *.vcxproj and *.sln files, but there is no Makefile. How can I change the CMakeLists file to generate a Makefile?
What is the command line equivalent compiler to gcc for windows? And how do I set this compiler as the target for CMake and the generated Makefile?
Reading about the build tools https://learn.microsoft.com/en-us/cpp/build/walkthrough-compile-a-c-program-on-the-command-line?view=vs-2019
Do I need to target the cl.exe compiler? Would this work with CMake and Makefiles?
I'm reading online that these command line flags will set the compiler, how can I add these to the CMakeLists.txt to be used automatically?
DCMAKE_C_COMPILER=cl
DCMAKE_C_COMPILER_FORCED=ON
DCMAKE_CXX_COMPILER=cl
DCMAKE_CXX_COMPILER_FORCED=ON
DCMAKE_BUILD_TYPE=Debug
DCMAKE_INSTALL_PREFIX=%CFITSIO_DIR%
G"NMake Makefiles"
You should use the build tool mode of CMake for builds from the command line.
After configuring your project for a 64bit build using Visual Studio 2019 e.g. with
cmake -S <sourcedir> -B <builddir> -G "Visual Studio 16 2019" -A x64
you would run
cmake --build <builddir> --target ALL_BUILD --config Release
For further options see here for an almost quiet build from the command line see here.
As suggested by #vre, you can run everything from the command line, while still using the Visual Studio generator. Just use CMake's command line build tools:
cmake ..
cmake --build . --config Release
This way, you don't have to open Visual Studio at all to build your libraries/executables.
Another option is to use Microsoft's nmake utility, which will generate NMake Makefiles. You can tell CMake to use this generator instead using this:
cmake -G"NMake Makefiles" ..
The full list of CMake generators you can choose from is listed here.
If you don't want to manually set the CMake generator in the command line, you can set it at the top of your CMakeLists.txt file:
set (CMAKE_GENERATOR "NMake Makefiles" CACHE INTERNAL "" FORCE)
It will be used on the second CMake configuration in this case, as the first run will use the system default generator. If you want CMake to use it on the first configuration, you can utilize the Preload.cmake procedure outlined in this answer.
I use Windows 10, QtCreator 4.7.1, CMake 3.11.4 (Ninja, CodeBlocks), MSVC 2017. Try to integrate PVS-Studio to CMake project.
cmake_minimum_required(VERSION 3.10)
project(tst CXX)
add_executable(${PROJECT_NAME} main.cpp)
include(PVS-Studio.cmake)
pvs_studio_add_target(
TARGET ${PROJECT_NAME}.analyze ALL
OUTPUT FORMAT errorfile
ANALYZE ${PROJECT_NAME}
BIN "C:/Program Files (x86)/PVS-Studio/x64/PVS-Studio.exe")
I get error
[3/6 4.2/sec] Generating PVS-Studio.cfg
[4/6 5.1/sec] Analyzing CXX file main.cpp
FAILED: PVS-Studio/main.cpp.plog
cmd.exe /C "cd /D D:\work\v2.0\ui_tools\build-tst-Desktop_Qt_5_11_2_MSVC2017_64bit-u041eu0442u043bu0430u0434u043au0430 && "C:\Program Files\CMake\bin\cmake.exe" -E make_directory D:/work/v2.0/ui_tools/build-tst-Desktop_Qt_5_11_2_MSVC2017_64bit-u041eu0442u043bu0430u0434u043au0430/PVS-Studio && "C:\Program Files\CMake\bin\cmake.exe" -E remove_directory D:/work/v2.0/ui_tools/build-tst-Desktop_Qt_5_11_2_MSVC2017_64bit-u041eu0442u043bu0430u0434u043au0430/PVS-Studio/main.cpp.plog && "C:\Program Files\CMake\bin\cmake.exe" -D PVS_STUDIO_AS_SCRIPT=TRUE -D "PVS_STUDIO_COMMAND=C:/Program Files (x86)/PVS-Studio/x64/PVS-Studio.exe;analyze;--output-file;D:/work/v2.0/ui_tools/build-tst-Desktop_Qt_5_11_2_MSVC2017_64bit-u041eu0442u043bu0430u0434u043au0430/PVS-Studio/main.cpp.plog;--source-file;D:/work/v2.0/ui_tools/tst/main.cpp;--dep-file;D:/work/v2.0/ui_tools/build-tst-Desktop_Qt_5_11_2_MSVC2017_64bit-u041eu0442u043bu0430u0434u043au0430/PVS-Studio/main.cpp.plog.d;--dep-file-target;PVS-Studio/main.cpp.plog;--cfg;D:/work/v2.0/ui_tools/build-tst-Desktop_Qt_5_11_2_MSVC2017_64bit-u041eu0442u043bu0430u0434u043au0430/PVS-Studio.cfg;--platform;x64;--preprocessor;visualcpp;--cxx;C:/Program Files (x86)/Microsoft Visual Studio/2017/Community/VC/Tools/MSVC/14.15.26726/bin/HostX86/x64/cl.exe;--cc;C:/Program Files (x86)/Microsoft Visual Studio/2017/Community/VC/Tools/MSVC/14.15.26726/bin/HostX86/x64/cl.exe;--cl-params;;;-DPVS_STUDIO;D:/work/v2.0/ui_tools/tst/main.cpp" -P D:/work/v2.0/ui_tools/tst/PVS-Studio.cmake"
CMake Error at D:/work/v2.0/ui_tools/tst/PVS-Studio.cmake:46 (message):
Incorrect parameter syntax: An unknown parameter is present in the command
line: dep-file.
Any idea what to do?
In your CMake integration code, the 'BIN' parameter should point to the 'CompilerCommandsAnalyzer.exe' tool (under Windows) instead of the 'PVS-Studio.exe' C++ analyzer core.
However, PVS-Studio CMake module support for Windows will become available in PVS-Studio 6.26, which is expected to be released in a couple of days. The current 6.25 release is missing this tool.
Please check whether you have the 'CompilerCommandsAnalyzer.exe' inside your PVS-Studio installation folder ('c:\Program Files (x86)\PVS-Studio\' by default). If you do not have it, please write us at support#viva64.com, so we can give you a pre-release version to try. Or you can just wait for a 6.26 release which will become available quite soon.
this is a simplified version of my cmake
cmake_minimum_required(VERSION 2.8.4)
project(math)
add_library(math math.cpp)
function(install_package)
install(TARGETS math
LIBRARY DESTINATION lib
ARCHIVE DESTINATION lib)
add_custom_command(TARGET math
POST_BUILD
COMMAND cmake ARGS -P cmake_install.cmake)
endfunction()
install_package()
But when I build Debug version I get the following error
CMake Error at cmake_install.cmake:55 (file):
file INSTALL cannot find
"<my project's root>/build/Release/math.lib".
Why is it looking in Release folder despite that I build for Debug?
When I build for Release then, obviously, everything works.
I tried to add CONFIGURATIONS option to install method, but it doesn't help.
I'm using Visual Studio 15.
If I look into my cmake_install.cmake, Release is the default if you don't specify anything in your add_custom_command() call:
# Set the install configuration name.
if(NOT DEFINED CMAKE_INSTALL_CONFIG_NAME)
if(BUILD_TYPE)
string(REGEX REPLACE "^[^A-Za-z0-9_]+" ""
CMAKE_INSTALL_CONFIG_NAME "${BUILD_TYPE}")
else()
set(CMAKE_INSTALL_CONFIG_NAME "Release")
endif()
message(STATUS "Install configuration: \"${CMAKE_INSTALL_CONFIG_NAME}\"")
endif()
So if you look into INSTALL.vcxproj the call that CMake is generating looks like:
"C:\Program Files\CMake\bin\cmake.exe" -DBUILD_TYPE=$(Configuration) -P cmake_install.cmake
Which would translate into:
add_custom_command(TARGET math
POST_BUILD
COMMAND ${CMAKE_COMMAND} ARGS -D BUILD_TYPE=$<CONFIG> -P cmake_install.cmake)
New to CMake, and I'm having a hard time understanding how to use generator expressions. I'm trying to use add_custom_command to create a post-build command to copy Qt DLLs to the executable directory.
In Qt5WidgetsConfig.cmake I can see it creates different properties for the Qt5::Widgets target to refer to the DLL, depending on the currently active configuration. Either IMPORTED_LOCATION_DEBUG or IMPORTED_LOCATION_RELEASE. I expected to be able to use the $<CONFIG:Debug> generator expression as a condition in an if() but that doesn't work.
My CMakeLists.txt:
# minimum version required for proper support of C++11 features in Qt
cmake_minimum_required(VERSION 3.1.0)
set(CMAKE_CONFIGURATION_TYPES Debug;Release)
# project name and version
project(TPBMon VERSION 0.0.0.1)
# Qt5 libs
find_package(Qt5Widgets REQUIRED)
# run Qt's MOC when needed
set(CMAKE_AUTOMOC ON)
add_executable(
tpbmon
src/main.cpp
src/mainwindow.hpp
src/mainwindow.cpp
)
target_link_libraries(tpbmon Qt5::Widgets)
set_target_properties(
tpbmon
PROPERTIES
RUNTIME_OUTPUT_DIRECTORY ${PROJECT_SOURCE_DIR}/bin
)
if(WIN32)
if($<CONFIG:Debug>)
get_target_property(WIDGETDLL Qt5::Widgets IMPORTED_LOCATION_DEBUG)
else($<CONFIG:Debug>)
get_target_property(WIDGETDLL Qt5::Widgets IMPORTED_LOCATION_RELEASE)
endif($<CONFIG:Debug>)
add_custom_command(
TARGET tpbmon POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy ${WIDGETDLL} $<TARGET_FILE_DIR:tpbmon>
)
endif(WIN32)
Figured it out myself by modifying the add_custom_command call to
add_custom_command(
TARGET tpbmon POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy_if_different
$<TARGET_FILE:Qt5::Widgets>
$<TARGET_FILE_DIR:tpbmon>
)
It's amazing what a fresh perspective after a good night's sleep can do. ;)
You can use windeployqt program which is part of Qt binary release. It will scan your binary and collect all used Qt DLLs, plugins and QML modules. It could be wrapped in CMake as a post-build event by add_custom_command(TARGET target_name POST_BUILD ...) signature.
For the future you can add all Qt5 dependencies to your executable folder:
find_package(Qt5 COMPONENTS Core Gui Widgets)
...
add_custom_command(TARGET MyQtProj POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy_if_different $<TARGET_FILE:Qt5::Core> $<TARGET_FILE_DIR:MyQtProj>
COMMAND ${CMAKE_COMMAND} -E copy_if_different $<TARGET_FILE:Qt5::Gui> $<TARGET_FILE_DIR:MyQtProj>
COMMAND ${CMAKE_COMMAND} -E copy_if_different $<TARGET_FILE:Qt5::Widgets> $<TARGET_FILE_DIR:MyQtProj>
)
I am trying to build a project in Release mode. By default it is built in debug mode. I am setting the variable CMAKE_BUILD_TYPE to "Release" in CMakeLists.txt. But it is still building the project in debug mode.
When I pass "Release" as the build type in the CMake command, it still does not work.
The CMake command that I am using is:
cmake -G"Visual Studio 10" -DCMAKE_BUILD_TYPE=Release
-H"source_path" -B"Build path"
Please provide a solution if any.
To change the build type, on Windows, it must be done at build time:
cmake --build {DIR} --config Release
By default it's Debug. I'm still looking for a way of changing this default. CMAKE_BUILD_TYPE doesn't work of course, and tweaking CMAKE_CONFIGURATION_TYPES doesn't work either, obviously for the same reason, they only apply for Unix makefiles, not for Visual projects.
I checked it with Visual Studio 2015 and cmake 3.3 .
Short answer
Link
cmake --build {BUILD_DIR_PATH} --target ALL_BUILD --config {BUILD_TYPE}
Example
cmake --build . --target ALL_BUILD --config Release
Long answer
cmake -G{GENERATOR_NAME} -B{BUILD_DIR_PATH} -H{SOURCE_DIR_PATH}
cmake --build {BUILD_DIR_PATH} --target ALL_BUILD --config {BUILD_TYPE}
Example
cmake -GVisual Studio 14 -Bbuild/win32/x86 -H.
cmake --build build/win32/x86 --target ALL_BUILD --config Release
Additional info
"-G" - specifies the generator name
"-B" - specifies path to the build folder
"-H" - specifies path to the source folder
You cannot set the default build type for Visual Studio from the command line.
CMake's Visual Studio Generators will generate the four standard profiles (Debug, RelWithDebInfo, MinSizeRel and Release) and you have to choose the one you want to build from within VS. This is because the information about the active configuration is not part of the project files generated by CMake, but part of the .suo file generated by VS.
If you want an automated build of a particular configuration, use MSBuild instead of VS which allows you to specify a configuration on the command line.
Tried the things below which worked for me to build the binaries in release/debug mode in Windows.
Added the following line in the root CMakeLists.txt file, just above the project command:
SET(CMAKE_CONFIGURATION_TYPES ${CMAKE_BUILD_TYPE} CACHE STRING "" FORCE)
Used the following command for setting the release mode configuration:
cmake -DCMAKE_BUILD_TYPE=Release ..
Used this command to build the same in Release mode:
cmake --build . --config Release
You can repeat the same procedure for debug mode as well, it works.
Bit late, but I found this worked for me and was quite clean:
This means that just calling cmake builds in release mode, but if you want debug mode just call cmake -DCMAKE_BUILD_TYPE=Debug
if(NOT CMAKE_BUILD_TYPE)
set(CMAKE_BUILD_TYPE "Release" CACHE STRING "Choose Release or Debug" FORCE)
endif()
If your generator is a single-config generator like "Unix Makefiles" or "Ninja", then the build type is specified by the CMAKE_BUILD_TYPE variable, which can be set in the configure command by using -DCMAKE_BUILD_TYPE:STRING=Release.
For multi-config generators like the Visual Studio generators and "Ninja Multi-Config", the config to build is specified in the build command using the --config argument argument like --config Release. A default value can currently be specified at configure time for the Ninja Multi-Config generator by setting the value of the CMAKE_DEFAULT_BUILD_TYPE variable, which will be used if the --config argument isn't passed to the build command. At the current time, a default cannot be set for Visual Studio generators.
Use it as you do it but in the root cmake file add the following before the project keyword
SET(CMAKE_CONFIGURATION_TYPES ${CMAKE_BUILD_TYPE} CACHE STRING "" FORCE)
PROJECT(MY_PROJECT)#It's here just to show where you should add it.