I'm trying to use windeployqt.exe (Qt 5.13.2) to deploy dlls for a debug application generated by CMake 3.16. All the dlls are deployed correctly except for the platform plugin dll, which deploys qwindows.dll instead of qwindowsd.dll and results in the following error when I try to run the executable:
This application failed to start because no Qt platform plugin could be initialized.
So far, I've tried:
Specifying --debug on the windeployqt command line. That failed because Qt5Coredd.dll could not be found (note the double d's).
Verifying that no Qt plugin related environment variables are set.
Checked PATH to make sure it doesn't contain any folder with a platforms directory.
If I copy qwindowsd.dll manually, everything works fine. However I'd really like to figure out what I'm doing wrong with windeployqt.
This is apparently a known problem that Qt have dragged their heels on fixing, but I figured out a workaround in CMake - this works for both the Ninja generator/Visual Studio's built in CMake support as well as the regular Visual Studio solution generator
# Split windeployqt into 2 parts to fix issue with deploying debug plugins
add_custom_command(TARGET MyApp POST_BUILD
COMMAND ${QT_PATH}/bin/windeployqt --compiler-runtime --no-plugins ${MY_APP_EXE})
if (CMAKE_GENERATOR STREQUAL "Ninja")
# Ninja is a single-config generator so we can use CMAKE_BUILD_TYPE to generate different commands
if (CMAKE_BUILD_TYPE STREQUAL "Debug")
add_custom_command(TARGET MyApp POST_BUILD
COMMAND ${QT_PATH}/bin/windeployqt --debug --no-compiler-runtime --no-translations --no-libraries ${MY_APP_EXE})
else()
add_custom_command(TARGET MyApp POST_BUILD
COMMAND ${QT_PATH}/bin/windeployqt --release --no-compiler-runtime --no-translations --no-libraries ${MY_APP_EXE})
endif()
else()
# if in MSVC we have to check the configuration at runtime instead of generating different commands
add_custom_command(TARGET MyApp POST_BUILD
COMMAND cmd.exe /c if "$(Configuration)" == "Debug" ${QT_PATH}/bin/windeployqt --debug --no-compiler-runtime --no-translations --no-libraries ${MY_APP_EXE})
add_custom_command(TARGET MyApp POST_BUILD
COMMAND cmd.exe /c if not "$(Configuration)" == "Debug" ${QT_PATH}/bin/windeployqt --release --no-compiler-runtime --no-translations --no-libraries ${MY_APP_EXE})
endif()
Related
I have a cmake-based C++ project on github that builds with Travis CI. The .travis.yml file has the following for building on OSX with clang:
language: cpp
jobs:
include:
- os: osx
compiler: gcc
osx_image: xcode11.2
env:
- GCC_VER="9"
- MATRIX_EVAL="CC=gcc-${GCC_VER} && CXX=g++-${GCC_VER}"
addons:
homebrew:
packages:
- cppunit
- mruby
... (other OS) ...
before_script:
- eval "${MATRIX_EVAL}"
script:
- cmake -D ENABLE_TESTS:BOOL=TRUE .
- cmake --build . -- -j2
- ctest -j2
CppUnit is installed by brew.
The cmake command runs fine and finds CppUnit in /usr/local (i.e. CPPUNIT_INDLUE_DIR is correctly set to /usr/local/include and CPPUNIT_LIBRARIES is correctly set to /usr/local/lib/libcppunit.dylib. However when building the tests, I get link errors about undefined symbols related to cppunit, indicating that it is not correctly linking against cppunit.
The same project builds fine on my MacBook (also with a brew-installed cppunit), and it correctly builds on Linux with both gcc-9 and with clang, with cppunit installed via apt.
I thought I needed to set DYLD_LIBRARY_PATH=$DYLD_LIBRARY_PATH:/usr/local/lib somewhere, but when I do that I get new errors:
$ cmake -D ENABLE_TESTS:BOOL=TRUE .
dyld: Symbol not found: __cg_jpeg_resync_to_restart
Referenced from: /System/Library/Frameworks/ImageIO.framework/Versions/A/ImageIO
Expected in: /usr/local/lib/libJPEG.dylib
in /System/Library/Frameworks/ImageIO.framework/Versions/A/ImageIO
/Users/travis/.travis/functions: line 109: 3565 Abort trap: 6 cmake -D ENABLE_TESTS:BOOL=TRUE .
The command "cmake -D ENABLE_TESTS:BOOL=TRUE ." exited with 134.
Here is the CMakeLists.txt file at the root of the source:
cmake_minimum_required (VERSION 3.1)
project (mrbind17 CXX)
set (CMAKE_CXX_STANDARD 17)
set (CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH}
"${CMAKE_CURRENT_SOURCE_DIR}/cmake")
add_definitions(-g)
option(ENABLE_TESTS "Build tests. May require CppUnit_ROOT" OFF)
include_directories (${CMAKE_CURRENT_SOURCE_DIR}/include)
find_package (Mruby REQUIRED)
include_directories (${Mruby_INCLUDE_DIR})
find_package (CppUnit)
if (CPPUNIT_FOUND)
message(STATUS "CppUnit found, unit tests will be compiled")
message(STATUS "CPPUNIT_INCLUDE_DIR : ${CPPUNIT_INCLUDE_DIR}")
message(STATUS "CPPUNIT_LIBRARIES : ${CPPUNIT_LIBRARIES}")
include_directories(${CPPUNIT_INCLUDE_DIR})
enable_testing()
if(${ENABLE_TESTS})
add_subdirectory (test)
endif(${ENABLE_TESTS})
else (CPPUNIT_FOUND)
message(STATUS "CppUnit not found, unit tests will not be compiled")
endif (CPPUNIT_FOUND)
and the CMakeLists.txt in the test directory:
add_executable(interpreter_test main.cpp interpreter_test.cpp)
target_link_libraries(interpreter_test ${Mruby_LIBRARIES} ${CPPUNIT_LIBRARIES})
add_test(NAME interpreter_test COMMAND ./interpreter_test interpreter_test.xml)
add_executable(function_test main.cpp function_test.cpp)
target_link_libraries(function_test ${Mruby_LIBRARIES} ${CPPUNIT_LIBRARIES})
add_test(NAME function_test COMMAND ./function_test function_test.xml)
add_executable(module_test main.cpp module_test.cpp)
target_link_libraries(module_test ${Mruby_LIBRARIES} ${CPPUNIT_LIBRARIES})
add_test(NAME module_test COMMAND ./module_test module_test.xml)
What can I do to have cmake link correctly against cppunit?
I have a C++ project using CMake. The project is built on CentOS machine. I have configured CLion to build remotely from MacOS. I have unit tests for the project and I am trying to run them from CLion. I can run the tests from CentOS machine using CTest like below
ctest -r utCppProject -v
CLion tries to run the executable directly with gtest flags like below
./utCppProject --gtest_filter=* --gtest_color=no
Process finished with exit code 0
No tests are actually run.
How can I configure CLion to be able to use CTest for running unit tests?
Here is my CMakeLists.txt for the unit test project
cmake_minimum_required(VERSION 3.4.1)
include(../../cmake-dependencies/Boost.cmake)
include(../../cmake-dependencies/GoogleTest.cmake)
set(CMAKE_BINARY_DIR "${CMAKE_CURRENT_LIST_DIR}/../build")
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
include_directories(${GOOGLE_TEST_DIR}/googletest/include)
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/.)
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/../include)
set(TARGET utCppProject)
add_executable (
${TARGET}
utCppProject.cpp
)
target_link_libraries (
${TARGET}
CppProject
gtest
boost_system
pthread
)
set(CMAKE_CXX_FLAGS "-fPIC -DPIC -Wall -Werror -std=c++0x")
set(TEST_OUTPUT "${CMAKE_BINARY_DIR}/test_results/${TARGET}.xml")
add_test(${TARGET} ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/${TARGET})
set_tests_properties(${TARGET} PROPERTIES
ENVIRONMENT
"UT_FOLDER_PATH=${CMAKE_CURRENT_SOURCE_DIR};GTEST_OUTPUT=xml:${TEST_OUTPUT}")
Since CLion 2020.3 EAP CTest is supported from the box.
You can easily configure CLion to use CTest. Simply duplicate the default configuration for the test target and configure it to use the CTest executable and set the working directory to the build directory:
In detail:
Executable > Select other... > Find and select ctest (for me it is /usr/bin/ctest, on UNIX-like systems you can use which ctest to find it)
Set program arguments - -j sets the amount of threads to use, then the name of the executable to test, and --output-on-failure to get the test output when something went wrong - you could simply set it to -r utCppProject -v
Set working directory to cmake-build-debug under the project dir, the default build directory for CLion
There is currently no CTest support in CLion.
The feature request is here.
Since version CLion 2020.3 the CTest is supported from the box for CMake 3.14 and above.
I have a cmake build system gone wild. Before supporting IDEs, everything was ok.
I need to copy files (shaders in this case) to the build directory. They need to be copied when they've changed, regardless of whether the main target is built or not.
I had success before, as I could add a custom command with ${CMAKE_CURRENT_BINARY_DIR}, add dependencies later and everything was fine.
The problem is, when using a generator expression for the command output, it creates a dependency from my custom command to the main target. This means adding a dependency backwards (which is needed to trigger the copy) throws an error because of cyclic dependencies.
This is what I have so far, which doesn't work because the custom target (thus custom command) is not triggered when the main target doesn't need rebuilding.
set(SHADER_IN_DIR ${CMAKE_CURRENT_SOURCE_DIR}/src/shaders)
file(GLOB_RECURSE SHADERS "${SHADER_IN_DIR}/*.glsl")
add_custom_command(TARGET ${PROJECT_NAME} PRE_BUILD
COMMAND ${CMAKE_COMMAND} -E make_directory $<TARGET_FILE_DIR:${PROJECT_NAME}>/shaders/)
set(STAMP_DIR ${CMAKE_CURRENT_BINARY_DIR}/.stamps)
add_custom_command(TARGET ${PROJECT_NAME} PRE_BUILD
COMMAND ${CMAKE_COMMAND} -E make_directory ${STAMP_DIR})
set(STAMP_FILES "")
foreach(SHADER ${SHADERS})
get_filename_component(SHADER_FILENAME ${SHADER} NAME)
set(STAMP_FILE ${STAMP_DIR}/${SHADER_FILENAME}.stamp)
add_custom_command(
OUTPUT ${STAMP_FILE}
COMMAND ${CMAKE_COMMAND} -E touch ${STAMP_FILE}
COMMAND ${CMAKE_COMMAND} -E copy_if_different ${SHADER} $<TARGET_FILE_DIR:${PROJECT_NAME}>/shaders/${SHADER_FILENAME}
DEPENDS ${SHADER}
)
list(APPEND STAMP_FILES ${STAMP_FILE})
endforeach()
add_custom_target(Shaders
SOURCES ${SHADERS}
DEPENDS ${STAMP_FILES})
# Need to add dependency here! But I can't :(
So, is there any other way to get what output directory will be used in an IDE? All "solutions" I've read to force building a target have failed (they pretty much all rely on add_dependencies).
Thank you for saving my sanity.
In the end, instead of trying to find out where the IDE is going to output the binary, I forced output in a predictable bin/ dir.
set(BINARY_OUT_DIR ${CMAKE_CURRENT_BINARY_DIR}/bin)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${BINARY_OUT_DIR})
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${BINARY_OUT_DIR})
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${BINARY_OUT_DIR})
foreach(OUTPUTCONFIG ${CMAKE_CONFIGURATION_TYPES})
string(TOUPPER ${OUTPUTCONFIG} OUTPUTCONFIG)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY_${OUTPUTCONFIG} ${BINARY_OUT_DIR})
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY_${OUTPUTCONFIG} ${BINARY_OUT_DIR})
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY_${OUTPUTCONFIG} ${BINARY_OUT_DIR})
endforeach(OUTPUTCONFIG CMAKE_CONFIGURATION_TYPES)
I am trying to build QT based application with CMake and everything goes well enough. I followed this tutorial and I can build my application. Now I want to run npm run build before build using add_custom_command it does not seems to goes as expected.
The build process fails with
RCC: Error in 'tray-icon/systray.qrc': Cannot find file 'html/js/app.full.js'
AUTORCC: error: process for.build/Debug/x64/tray-icon/CMakeFiles/tray-icon.dir/qrc_systray.cpp failed:
RCC: Error in 'tray-icon/systray.qrc': Cannot find file 'html/js/app.full.js'
Can you tell me how to execute npm command before the build verification step on tray-icon/systray.qrc?
This is my cmake file
cmake_minimum_required(VERSION 2.8.11)
project(tray-icon)
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC ON)
set(CMAKE_INCLUDE_CURRENT_DIR ON)
find_package(Qt5Widgets 5.5)
find_package(Qt5Qml 5.5)
find_package(Qt5WebEngine 5.5)
find_package(Qt5WebEngineCore 5.5)
find_package(Qt5WebEngineWidgets 5.5)
# generate rules for building source files from the resources
set(SOURCES tray-icon.cpp window.cpp systray.qrc)
set(CMAKE_VERBOSE_MAKEFILE 1)
#adds target
add_executable(tray-icon ${SOURCES})
# custom build command for javascript part of the application
add_custom_command (
TARGET "tray-icon"
PRE_BUILD COMMAND npm run build
)
# Find the QtWidgets library
target_link_libraries(tray-icon
Qt5::Widgets
Qt5::WebEngine
Qt5::WebEngineWidgets)
install(TARGETS tray-icon DESTINATION .)
PS: My final solution looks like this
# custom build command for javascript part of the application
add_custom_target(
tray-icon_automoc
)
add_custom_target (
npm-target
COMMAND cd ${PROJECT_SOURCE_DIR} && cd html && npm install && npm run build
)
You could try add_custom_target and add_dependencies.
add_custom_target (
npm-target
COMMAND npm run build
)
add_dependencies(tray-icon npm-target)
To overcome the issue mentioned in the comments you should be able to add dependency using AUTOGEN_TARGET_DEPENDS target property. It can be set instead to a list of dependencies for the _automoc target.
I'd basically like to achieve the same as http://blog.alexrp.com/2013/09/26/clangs-static-analyzer-and-automake, but with CMake.
analyze_srcs = foo.c
analyze_plists = $(analyze_srcs:%.c=%.plist)
CLEANFILES = $(analyze_plists)
$(analyze_plists): %.plist: %.c
#echo " CCSA " $#
#$(COMPILE) --analyze $< -o $#
analyze: $(analyze_plists)
.PHONY: analyze
So you can run
make analyze
make clean
I guess I need to use add_custom_command/add_custom_target and somehow change the "object file" extension just for that target.
Afterwards get a list of the generated files to perhaps pass them to a script for combining them into 1 output file.
Can anyone point me in the right direction?
You can use scan-build when running cmake.
scan-build cmake /path/to/source
scan-build make
scan-build sets the CC and CXX environment variables which are picked up by cmake.
I found a way:
function(add_clang_static_analysis target)
get_target_property(SRCs ${target} SOURCES)
add_library(${target}_analyze OBJECT EXCLUDE_FROM_ALL ${SRCs})
set_target_properties(${target}_analyze PROPERTIES
COMPILE_OPTIONS "--analyze"
EXCLUDE_FROM_DEFAULT_BUILD true)
endfunction()
Combining clang's plist files (which get extension .o this way) into a report is still open ($<TARGET_OBJECTS:objlibtarget>?).
The following solution has the drawback of compiling and analyzing the entire project, not the specific targets.
set(on_side_build_path ${CMAKE_BINARY_DIR}/clang_static_analysis)
set(scan_build_path scan-build)
set(reports_path ${CMAKE_BINARY_DIR}/clang_static_analysis_reports)
# Creates clean directory where the analysis will be built.
add_custom_target(clang_static_analysis_prepare
COMMAND ${CMAKE_COMMAND} -E rm -rf ${on_side_build_path}
COMMAND ${CMAKE_COMMAND} -E make_directory ${on_side_build_path}
WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
)
# Runs the analysis from the path created specifically for that task. Use 'my own' project source directory as the source directory.
add_custom_target(clang_static_analysis
# scan-build wants Debug build, for better analysis.
COMMAND ${scan_build_path} ${CMAKE_COMMAND} ${CMAKE_SOURCE_DIR} -DCMAKE_BUILD_TYPE=Debug
COMMAND ${scan_build_path}
-v -v -o ${reports_path}
${CMAKE_COMMAND} --build .
WORKING_DIRECTORY ${on_side_build_path}
)
# Run the *_prepare target always before the analysis
add_dependencies(clang_static_analysis clang_static_analysis_prepare)
Invoke it with:
cmake --build . --target clang_static_analysis
Bonus #1: Allowing custom toolchains to work
scan-build injects the CC and CXX environment variables to specify the replaced compilers, which are ccc-analyzer and c++-analyzer. When defining CMAKE_C_COMPILER and CMAKE_CXX_COMPILER the CC and CXX variables will be ignored. What you need to do to support scan-build is to point CMAKE_C*_COMPILER variables to use the one from environment, if defined. So having that in your toolchain file:
set(CMAKE_C_COMPILER some/path/to/c_compiler)
set(CMAKE_Cxx_COMPILER some/path/to/cxx_compiler)
Replace it with:
if(DEFINED ENV{CC})
set(CMAKE_C_COMPILER $ENV{CC})
else()
set(CMAKE_C_COMPILER some/path/to/c_compiler)
endif()
if(DEFINED ENV{CXX})
set(CMAKE_CXX_COMPILER $ENV{CXX})
else()
set(CMAKE_CXX_COMPILER some/path/to/cxx_compiler)
endif()
Bonus #2: Using scan-build from the LLVM toolchain
If your custom toolchain is LLVM, then most probably you want to use the scan-build command from the toolchain. To enable it, simply define the path to toolchain path using cmake's cache variables:
set(LLVM_TOOLCHAIN_PATH "some/path/here" CACHE PATH "Path to the LLVM toolchain")
or from the command line:
cmake -DLLVM_TOOLCHAIN_PATH=some/path/here ...
and then reuse the path from the custom target:
set(scan_build_path ${LLVM_TOOLCHAIN_PATH}/bin/scan-build)
Bonus #3: Adding test command
Note: enable_testing shall be called before add_test. enable_testing() must be called from the project's top level CMakeLists.txt for ctest to resolve paths to tests.
add_test(
NAME clang_static_analysis
COMMAND ${CMAKE_COMMAND} --build . --target clang_static_analysis
)
Run it like that:
# Fire configure and generate stages
cmake ../source/dir
ctest -R clang_static_analysis --verbose