I have a CMake project that works pretty good in Linux, but it doesn't under Windows.
The first issue comes by final linking:
[100%] Linking CXX shared library libProjlib.dll
CMakeFiles\Projlib.dir/objects.a(configserver.cpp.obj): In function `ConfigServer::findAdapter(unsigned long*)':
C:/Users/jose/Documents/git-Projects/Proj/Projlib/configserver.cpp:859: undefined reference to `GetIpAddrTable'
Error 1
findAdapter belongs to C:\Qt\Tools\mingw810_64\x86_64-w64-mingw32\include\iphlpapi.h, but doesn't link automatic
My project is missconfigured, I think. I would like to select the MinGW compiler, automatic through CMakeLists.txt (For now, I configure the compiler means cmake-gui.exe ..), and naturally fix the issue, but I don't know how. (Google didn't gave me a solution)
Please, any help or suggestion, link.. etc. will be wellcomed.
Here is my CMakeLists.txt
cmake_minimum_required(VERSION 3.13)
set(CMAKE_PROJECT_NAME "testProject")
project(${CMAKE_PROJECT_NAME})
set(CMAKE_BUILD_TYPE Debug)
if("Windows" STREQUAL "${CMAKE_SYSTEM_NAME}")
message(STATUS "_______________________________________Compiling on Windows")
elseif("Linux" STREQUAL "${CMAKE_SYSTEM_NAME}")
message(STATUS "_______________________________________Compiling on GNU/Linux :-)")
endif()
set(CMAKE_BUILD_PARALLEL_LEVEL 8)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
#list(APPEND CMAKE_PREFIX_PATH "/home/enigma/Qt/5.15.2/gcc_64") #linux
list(APPEND CMAKE_PREFIX_PATH "C:\\Qt\\Tools\\mingw810_64")
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTOUIC ON)
set(CMAKE_AUTORCC ON)
set(CMAKE_INCLUDE_CURRENT_DIR ON)
message(STATUS "___________________________________________________${CMAKE_BUILD_TYPE}")
if(CMAKE_BUILD_TYPE STREQUAL "Debug")
set(binPath "binDebugProj")
elseif()
set(binPath "binProj")
endif()
set(TMP_BUILD "tmpBuild")
set(Proj_LIB "Projlib")
add_subdirectory(${Proj_LIB} "${CMAKE_SOURCE_DIR}/../${binPath}/${TMP_BUILD}/${Proj_LIB}")
include_directories(${Proj_LIB})
The Solution is:
target_link_libraries(${CARDEA_LIB} PUBLIC Qt5::Core Qt5::Network iphlpapi)
At the end did work. (I was savin in anoter CMakeLists.txt AGGHH, too musch hours today)
Related
I have an undefined reference while trying to compile my Qt5 project.
I am usually pretty confident with using CMake but this time I have a strange error that I can't figure out:
undefined reference to boost::system::generic_category()
/usr/local/include/boost/system/error_code.hpp:223: undefined reference to 'boost::system::generic_category()'
My configurations are:
gcc (Ubuntu 9.3.0-17ubuntu1~20.04) 9.3.0
Qt5.15
cmake version 3.16.3
After typing whereis boost the outcome wasboost: /usr/include/boost and after applying the great power of dpkg -s libboost-dev | grep 'Version' :) the version is Version: 1.71.0.0ubuntu2
I don't understand what is happening, below an example of how my CMakeLists.txt is structured:
cmake_minimum_required (VERSION 3.1)
project(projectA)
set (OpenCV_DIR /home/to/opencv/build)
find_package( OpenCV REQUIRED )
find_package( Boost COMPONENTS system thread filesystem REQUIRED)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
set(CMAKE_INCLUDE_CURRENT_DIR ON)
INCLUDE_DIRECTORIES(${Boost_INCLUDE_DIR})
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC ON)
find_package(Qt5Widgets)
find_package(Qt5PrintSupport)
#make them into headers
qt5_wrap_ui (UI_HDRS ${UI})
add_executable(projectA main/main.cpp ui/qdarkstyle/style.qrc ${SRCS} ${UI_HDRS} ${UI_SRCS})
target_link_libraries (projectA Qt5::Widgets ${Boost_LIBRARIES} ${OpenCV_LIBS} Qt5::PrintSupport)
add_library(projectA_lib SHARED ${SRCS} ${UI_HDRS})
target_include_directories (projectA_lib PUBLIC "src/" "ui/")
link_directories(${Boost_LIBRARY_DIRS})
target_link_libraries (projectA_lib Qt5::Widgets ${Boost_LIBRARIES} ${OpenCV_LIBS})
I have searched and applied solutions I saw on all possible sources I was able to find such as:
This source but that didn't work.
Also from here it seems that this solution shall be applied:
set(Boost_USE_STATIC_LIBS ON)
set(Boost_USE_MULTITHREADED ON)
set(Boost_USE_STATIC_RUNTIME OFF)
find_package(Boost REQUIRED COMPONENTS system)
# the call to include_directories is now useless:
# the Boost::system imported target used below
# embeds the include directories
project(APP C CXX)
add_executable(APP src.cpp)
target_link_libraries(APP Boost::system)
However that also didn't do any specific benefits to finding the solution.
Other posts I consulted were this, this one but no answer was provided.
This post was useful but that also didn't provide any advice that didn't already know.
Thanks for pointing to the right direction and trying to find a solution.
I have a project containing both "normal" code and CUDA code. The whole project is managed by cmake. Now, depending on the availability of CUDA on the target machine I would like to be able to switch the CUDA-files on and off. For that, I intended to use the CMake-command "check_language", and implemented it in the following CMakeLists-file:
cmake_minimum_required(VERSION 3.17)
set(PROJECT_NAME "Hello_World_with_CUDA")
set(PROJECT_VERSION 1.0.0)
project(${PROJECT_NAME} VERSION ${PROJECT_VERSION} DESCRIPTION "UPPE pulse propagation library" LANGUAGES CXX)
include(GNUInstallDirs)
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/cmake/Modules/")
set(EXECUTABLE_OUTPUT_PATH ${CMAKE_SOURCE_DIR}/bin)
set(LIBRARY_OUTPUT_PATH ${CMAKE_SOURCE_DIR}/lib)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
check_language(CUDA)
if(CMAKE_CUDA_COMPILER)
enable_language(CUDA)
set(CMAKE_CUDA_STANDARD 14)
set(CMAKE_CUDA_STANDARD_REQUIRED ON)
add_compile_definitions(USE_CUDA)
else(CMAKE_CUDA_COMPILER)
message(STATUS "No CUDA support")
remove_definitions(USE_CUDA)
endif(CMAKE_CUDA_COMPILER)
cmake_policy (SET CMP0074 NEW)
set(PROJECT_SRC source/main.cpp source/test.cu)
set(PROJECT_INC include/test.hpp)
set(PROJECT_SRC ${PROJECT_SRC}
${PROJECT_INC})
add_executable(${PROJECT_NAME}
${PROJECT_SRC})
target_include_directories(${PROJECT_NAME} PRIVATE
include)
Nevertheless, I get the error
CMake Error at CMakeLists.txt:17 (check_language):
Unknown CMake command "check_language".
even though I have cmake in the version 3.17.2. Why that, and how can I still use that check? Or do I have to resort to different solutions?
Of course, if I switch out the block
check_language(CUDA)
if(CMAKE_CUDA_COMPILER)
enable_language(CUDA)
set(CMAKE_CUDA_STANDARD 14)
set(CMAKE_CUDA_STANDARD_REQUIRED ON)
add_compile_definitions(USE_CUDA)
else(CMAKE_CUDA_COMPILER)
message(STATUS "No CUDA support")
remove_definitions(USE_CUDA)
endif(CMAKE_CUDA_COMPILER)
with
find_package(CUDA QUIET)
if(CUDA_FOUND)
enable_language(CUDA)
set(CMAKE_CUDA_STANDARD 14)
set(CMAKE_CUDA_STANDARD_REQUIRED ON)
add_compile_definitions(USE_CUDA)
else(CUDA_FOUND)
message(STATUS "No CUDA support")
remove_definitions(USE_CUDA)
endif(CUDA_FOUND)
it works, but I intended to use the suggested approach, not the deprecated version.
Add the CheckLanguage module to the CMakeLists.txt file to use enable_language with include(CheckLanguage).
As Tsyvarev posted here
I want to make a Qt Quick project using CMake with compiled qml files. I put my QML files into a separate qrc/rcc file in a cmake projekt:
qt5_add_binary_resources("res" "qml.qrc" DESTINATION "qml.rcc")
This works fine so far. Now I wanted to compile them. According to the documentation I only need to change
qt5_add_resources(RESOURCES example.qrc)
add_executable(myapp ${SRC_LIST} ${RESOURCES)
to
find_package(Qt5QuickCompiler)
qtquick_compiler_add_resources(RESOURCES example.qrc)
add_executable(myapp ${SRC_LIST} ${RESOURCES)
in a project. So I changed my CMakeLists.txt to
find_package(Qt5QuickCompiler)
qtquick_compiler_add_resources("QML_RESOURCE" "qml.qrc")
qt5_add_binary_resources("res" ${QML_RESOURCE} DESTINATION "qml.rcc")
But now I get
FAILED: qml.rcc cmd.exe /C "cd /D "D:\buildPath" && C:\Qt\5.12.3\msvc2017_64\bin\rcc.exe --binary --name res --output qml.rcc "D:/buildPath/main_qml.cpp" "D:/buildPath/qmlcache_loader.cpp""
RCC Parse Error: 'D:/buildPath/main_qml.cpp' Line: 1 Column: 1 [Start tag expected.] [2/4 30.3/sec] Automatic MOC for target PROJECT_NAME ninja: build stopped: subcommand failed.
17:10:39: The process "C:\Program Files\CMake\bin\cmake.exe" exited with code 1. Error while building/deploying project PROJECT_NAME (kit: Desktop Qt 5.12.3 MSVC2017 64bit) When executing step "Build with CMake"
There isn't much information about Qt and CMake, so I don't know what to do about this. I hope someone can give me some advice.
The full working CMakeLists is
cmake_minimum_required(VERSION 3.1)
project(QtQuickCompiler LANGUAGES CXX)
set(CMAKE_INCLUDE_CURRENT_DIR ON)
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC ON)
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
find_package(Qt5 COMPONENTS Core Quick REQUIRED)
find_package(Qt5QuickCompiler)
qtquick_compiler_add_resources(RESOURCES qml.qrc)
add_executable(${PROJECT_NAME} "main.cpp" ${RESOURCES})
target_compile_definitions(${PROJECT_NAME} PRIVATE $<$<OR:$<CONFIG:Debug>,$<CONFIG:RelWithDebInfo>>:QT_QML_DEBUG>)
target_link_libraries(${PROJECT_NAME} PRIVATE Qt5::Core Qt5::Quick)
The full not working file is
cmake_minimum_required(VERSION 3.1)
project(QtQuickCompiler LANGUAGES CXX)
set(CMAKE_INCLUDE_CURRENT_DIR ON)
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC ON)
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
find_package(Qt5 COMPONENTS Core Quick REQUIRED)
find_package(Qt5QuickCompiler)
qtquick_compiler_add_resources(RESOURCES qml.qrc)
qt5_add_binary_resources(qml ${RESOURCES} DESTINATION "qml.rcc")
add_executable(${PROJECT_NAME} "main.cpp")
target_compile_definitions(${PROJECT_NAME} PRIVATE $<$<OR:$<CONFIG:Debug>,$<CONFIG:RelWithDebInfo>>:QT_QML_DEBUG>)
target_link_libraries(${PROJECT_NAME} PRIVATE Qt5::Core Qt5::Quick)
This was created using the template projects provided by QtCreator.
I'm trying to run travis test,but I get the following error.
I checked my files and directories, all files are exist.
I think my mistake in cmake file, but I can't find it.
Maybe someone can help me?
make[1]: *** No rule to make target CMakeFiles/test.dir/all', needed byCMakeFiles/Examples.dir/all'. Stop.
cmake_minimum_required(VERSION 2.8.7)
project(test)
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fPIC")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
set(Boost_USE_STATIC_LIBS ON)
set(Boost_USE_MULTITHREADED ON)
set(Boost_USE_STATIC_RUNTIME OFF)
option(BUILD_SHARED_LIBS "Build shared instead of static library" OFF)
option(BUILD_TESTS "Build tests for webdavclient" ON)
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/tests/ /sources)
add_library(test STATIC ${CMAKE_CURRENT_SOURCE_DIR}/sources/client.cpp tests/main.cpp)
find_package(OpenSSL REQUIRED)
find_package(Boost REQUIRED)
find_package(CURL REQUIRED)
include_directories(${CURL_INCLUDE_DIR})
if(Boost_FOUND)
include_directories(${Boost_INCLUDE_DIRS})
target_link_libraries(test ${Boost_LIBRARIES})
endif()
if(UNIX)
set(DEPENDS_LIBRARIES ${OPENSSL_LIBRARIES} ${CURL_LIBRARY} ${Boost_LIBRARIES})
elseif(MSVC)
set(DEPENDS_LIBRARIES ${OPENSSL_LIBRARIES} ${CURL_LIBRARY} ${Boost_LIBRARIES} ws2_32)
endif()
add_executable(Examples ${CMAKE_CURRENT_SOURCE_DIR}/examples/main.cpp)
target_link_libraries(Examples ${PROJECT_NAME} ${DEPENDS_LIBRARIES})
if(BUILD_TESTS)
enable_testing()
file(GLOB ${PROJECT_NAME}_TEST_SOURCES tests/*.cpp)
add_executable(init ${${PROJECT_NAME}_TEST_SOURCES})
target_link_libraries(init ${PROJECT_NAME} ${DEPENDS_LIBRARIES})
add_test(NAME init COMMAND init "-s" "-r" "compact" "--use-colour" "yes")
endif()
Command enable_testing() among other things creates target test. But you already have library target with the same name. This confuses CMake. For avoid this, rename library target.
Also, it is not recommended to have target name same as the project. So, rename project too.
I have referred to this link and made a CMakeLists.txt with following data:
cmake_minimum_required(VERSION 2.8)
SET(TARGET integrity_scanner)
message("\nBuilding ${TARGET}")
project (${TARGET})
if (UNIX)
message(STATUS "Setting GCC flags")
set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -g3 -Wall -O0")
else()
message(STATUS "Setting MSVC flags")
#set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /EHc-")
endif ()
include_directories ("${PROJECT_SOURCE_DIR}")
set(Boost_USE_STATIC_LIBS ON)
set(Boost_USE_MULTITHREADED ON)
set(Boost_USE_STATIC_RUNTIME OFF)
set(BOOST_ROOT C:/boost_1_55_0_dyn)
find_package(Boost 1.55.0 COMPONENTS thread)
SET(SOURCE
IntegrityScanner.cpp
)
SET(HEADERS
IntegrityScanner.h ../BaseApplication.hpp
)
if(Boost_FOUND)
add_definitions(-DDLL_EXPORTS)
add_definitions(-DBOOST_ALL_DYN_LINK)
include_directories("..\\..\\..\\ext_library\\zmq\\zeromq-4.0.3\\include")
include_directories("..\\..\\..\\ext_library\\zmq\\czmq\\czmq-2.1.0\\include")
link_directories("..\\..\\..\\ext_library\\zmq\\zeromq-4.0.3\\lib\\Win32\\Debug")
link_directories("..\\..\\..\\ext_library\\zmq\\czmq\\czmq-2.1.0\\lib\\Win32\\DebugDLL")
include_directories(${Boost_INCLUDE_DIRS})
LINK_DIRECTORIES(${Boost_LIBRARY_DIRS})
add_library(${TARGET} SHARED ${SOURCE} ${HEADERS})
target_link_libraries(${TARGET} ${Boost_LIBRARIES} czmq libzmq)
else()
message(STATUS "Boost_FOUND False")
endif()
Even though I have set BOOST_ROOT , it fails and gives the Boost Found False Message. What am I doing wrong here?
Edit: I have found that setting Boost_USE_STATIC_LIBS to OFF is resolving the issue. But I have to have it ON. What could be wrong here?
Your directory structure needs to look like this:
c:/boost/boost_1_55_0
and BOOST_ROOT is an environment variable set to c:/boost
I have found the reason why this code is not working. As I am giving value ON to Boost_USE_STATIC_LIBS, the result is that find_package will look for libboost_thread-vc100-mt-1_55 , which it will not find because building boost will give shared libraries as stated in this link. Refer to pic below: