CMake linking error, finding library but "undefined reference" - c++

I am trying to build a project with libtorch and opencv as dependencies. I am using cmake as my build system due to the fact that it is recommended for both these libraries. I am currently stuck, I am trying to get a minimal program to compile, using both libtorch and opencv.
My program looks like this
#include <opencv2/opencv.hpp>
#include <torch/torch.h>
void showImage(cv::Mat);
at::Tensor imgToTensor(std::string img_path);
using namespace cv;
using std::cout;
using std::endl;
int main() {
std::string img_path = "./images/01 HEAVENLY STAR080.png";
auto tensor = imgToTensor(img_path);
cout << tensor << endl;
}
at::Tensor imgToTensor(std::string img_path){
Mat origImage;
Mat normalizedImage;
Mat sizedImage(500, 200, CV_32FC3);
origImage = imread(img_path, 1);
origImage.convertTo(normalizedImage, CV_32FC3);
resize(normalizedImage, sizedImage, sizedImage.size(), 0, 0, INTER_LINEAR);
auto input = torch::from_blob(sizedImage.data, {sizedImage.rows, sizedImage.cols, 3});
return input;
}
void showImage(Mat image){
namedWindow("Display window", WINDOW_AUTOSIZE);
imshow("Display window", image);
waitKey(0);
}
This is my CMakeLists.txt:
cmake_minimum_required(VERSION 2.8 FATAL_ERROR)
project(ConvNet)
set(Torch_DIR /usr/local/libtorch/share/cmake/Torch)
find_package(OpenCV REQUIRED)
find_package(Torch REQUIRED)
include_directories( ${OpenCV_INCLUDE_DIRS} )
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${TORCH_CXX_FLAGS}")
add_executable(main main.cpp)
target_link_libraries(main "${OpenCV_LIBS}" "${TORCH_LIBRARIES}")
This is the output of cmake, so i know that the libraries are found:
-- The C compiler identification is GNU 9.3.0
-- The CXX compiler identification is GNU 9.3.0
-- Check for working C compiler: /usr/bin/cc
-- Check for working C compiler: /usr/bin/cc - works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Check for working CXX compiler: /usr/bin/c++
-- Check for working CXX compiler: /usr/bin/c++ - works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Found OpenCV: /usr/local (found version "4.3.0")
-- Looking for pthread.h
-- Looking for pthread.h - found
-- Performing Test CMAKE_HAVE_LIBC_PTHREAD
-- Performing Test CMAKE_HAVE_LIBC_PTHREAD - Failed
-- Looking for pthread_create in pthreads
-- Looking for pthread_create in pthreads - not found
-- Looking for pthread_create in pthread
-- Looking for pthread_create in pthread - found
-- Found Threads: TRUE
-- Found Torch: /usr/local/libtorch/lib/libtorch.so
-- Configuring done
-- Generating done
-- Build files have been written to: /home/jacob/Documents/KTH/KEX/codeEnvironment/ML_Classification_Toolkit/ML_tool/ConvNet/build
and this is the error i get:
/usr/bin/ld: CMakeFiles/main.dir/main.cpp.o: in function `imgToTensor(std::string)':
main.cpp:(.text+0x8d9): undefined reference to `cv::imread(std::string const&, int)'
/usr/bin/ld: CMakeFiles/main.dir/main.cpp.o: in function `showImage(cv::Mat)':
main.cpp:(.text+0xbac): undefined reference to `cv::namedWindow(std::string const&, int)'
/usr/bin/ld: main.cpp:(.text+0xc0d): undefined reference to `cv::imshow(std::string const&, cv::_InputArray const&)'
collect2: error: ld returned 1 exit status
make[2]: *** [CMakeFiles/main.dir/build.make:122: main] Error 1
make[1]: *** [CMakeFiles/Makefile2:96: CMakeFiles/main.dir/all] Error 2
make: *** [Makefile:104: all] Error 2
Any help would be greatly appreciated!

The version of libtorch I downloaded did not support cxx11 abi, and was therefore not compatible with opencv. Fixed by changing version of libtorch used.
The version I was using was the pre-cxx11 abi from here:
https://pytorch.org/get-started/locally/
I switched to the cxx11 abi.

Don't use that legacy cmake stuff
CMake simplifies a lot the process of building and linking libraries together.
Instead of manually tell the library path and linker options you can just create a dependency of your target with the library.
Following a short snippet on how it should it look like:
cmake_minimum_required(VERSION 3.10 FATAL_ERROR)
project(ConvNet)
find_package(OpenCV REQUIRED COMPONENTS opencv_highgui)
find_package(Torch REQUIRED)
add_executable(main main.cpp)
target_link_libraries(main PRIVATE opencv_highgui torch)
Observations
There is no include directory! This is because the include directory of the library is something that the library itself knows!
target_link_libraries even if it seems the same there's a lot of difference here! Because now we're not telling the compiler what to do but linking the target main to the targets opencv_highgui and torch.
opencv_highgui and torch are targets like main. Targets are created using add_library and add_executable.
A target has a public and a private interface. To set the target properties we call the function target_* (for instance target_compile_features(mytarget PUBLIC cxx_std_20) says that the target mytarget will use C++20 features and if someone links to us he will use the same compiler option automatically`

Related

Cmake is finding GSL but I can't include gsl in my C++ script

Why does Cmake find the library but I can't include it?
I am trying to use the gsl library in C++ but I get gsl/gsl_sf_bessel.h: No such file or directory when I run cmake --build even though it says -- Found GSL: /usr/include (found version "2.7.1") in the terminal after running cmake ...
I am using Ubuntu 22.04
I installed gsl with sudo apt-get install libgsl-dev and it is in /usr/include.
I think it might be a problem with the compiler I am using but I'm not sure how to check.
My CMakeLists.txt file
cmake_minimum_required(VERSION 3.5.1)
project(mujoco_gym)
set(CMAKE_CXX_STANDARD 14)
# It prevents the decay to C++98 when the compiler does not support C++14
set(CMAKE_CXX_STANDARD_REQUIRED ON)
# It disables the use of compiler-specific extensions
# e.g. -std=c++14 rather than -std=gnu++14
set(CMAKE_CXX_EXTENSIONS OFF)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall")
find_package(GSL REQUIRED)
link_libraries(GSL::gsl)
include_directories(${PROJECT_SOURCE_DIR})
file(GLOB SOURCE_FILES mujoco_gym.cpp)
add_executable(${CMAKE_PROJECT_NAME} ${SOURCE_FILES})
target_link_libraries (
${CMAKE_PROJECT_NAME}
${GSL_LIBRARIES}
)
My .cpp file.
#include<stdbool.h> //for bool
#include "stdlib.h"
#include "string.h"
#include <iostream>
#include <gsl/gsl_sf_bessel.h>
int main()
{
double x = 5.0;
std::cout << gsl_sf_bessel_j0(x);
return 0;
}
Cmake and build in the terminal
$ cmake ..
-- The C compiler identification is GNU 9.4.0
-- The CXX compiler identification is GNU 9.4.0
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working C compiler: /home/iii/miniconda3/envs/tf/bin/x86_64-conda-linux-gnu-cc - skipped
-- Detecting C compile features
-- Detecting C compile features - done
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: /home/iii/miniconda3/envs/tf/bin/x86_64-conda-linux-gnu-c++ - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Found PkgConfig: /usr/bin/pkg-config (found version "0.29.2")
-- Found GSL: /usr/include (found version "2.7.1")
-- Configuring done
-- Generating done
-- Build files have been written to: /home/iii/.mujoco/mujoco210/sample/build
$ cmake --build . --config Release
[ 50%] Building CXX object CMakeFiles/mujoco_gym.dir/mujoco_gym.cpp.o
/home/iii/.mujoco/mujoco210/sample/mujoco_gym.cpp:21:10: fatal error: gsl/gsl_sf_bessel.h: No such file or directory
21 | #include <gsl/gsl_sf_bessel.h>
| ^~~~~~~~~~~~~~~~~~~~~
compilation terminated.
gmake[2]: *** [CMakeFiles/mujoco_gym.dir/build.make:76: CMakeFiles/mujoco_gym.dir/mujoco_gym.cpp.o] Error 1
gmake[1]: *** [CMakeFiles/Makefile2:83: CMakeFiles/mujoco_gym.dir/all] Error 2
gmake: *** [Makefile:91: all] Error 2
$

Build errors while trying to add boost::stacktrace to CMake project

I'm trying to add boost::stacktrace library to my project via CMake. CMake finds all required libs well, but when I call boost::stacktrace::stacktrace() to print stack information into std::cout - it gives me the following error:
undefined reference to 'dladdr'
I've already tried to add the -ldl compile flag to the CMAKE_CXX_FLAGS compile flags to see if it can help, but it doesn't work. I understand that I missed some required packages, but I don't know which. Maybe you could advise me.
Gcc version 7.4.0 (Ubuntu 7.4.0-1ubuntu1~18.04)
Boost version 1.68.0
Here is some code:
CMakeLists.txt
#-------------------------------------------------------------------#
# cmake version & project name
cmake_minimum_required(VERSION 3.10)
project(stack_trace_exmpl)
#-------------------------------------------------------------------#
# project flags config
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_C_STANDARD 11)
set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -g3 -pthread")
set(CMAKE_C_FLAGS_DEBUG "-g3")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -pthread -ldl")
set(CMAKE_C_FLAGS "-Wall")
set(BOOST_ROOT "/opt/Boost_1_68_0/")
#-------------------------------------------------------------------#
# find Boost libs
set(Boost_USE_STATIC_LIBS OFF)
set(Boost_USE_MULTITHREADED ON)
set(Boost_USE_STATIC_RUNTIME OFF)
find_package(
Boost 1.68.0
COMPONENTS REQUIRED
stacktrace_basic
stacktrace_backtrace
stacktrace_addr2line
stacktrace_noop
)
#-------------------------------------------------------------------#
# project sources & project's type
file(GLOB SRCS "*.cpp")
file(GLOB HDRS "*.hpp")
add_executable(${PROJECT_NAME} ${HDRS} ${SRCS})
#-------------------------------------------------------------------#
# link libraries
target_link_libraries(${PROJECT_NAME}
PUBLIC ${Boost_STACKTRACE_BASIC_LIBRARY}
PUBLIC ${Boost_STACKTRACE_BACKTRACE_LIBRARY}
PUBLIC ${Boost_STACKTRACE_ADDR2LINE_LIBRARY}
PUBLIC ${Boost_STACKTRACE_NOOP_LIBRARY}
)
#-------------------------------------------------------------------#
# include directories
target_include_directories(${PROJECT_NAME}
PUBLIC ${Boost_INCLUDE_DIRS}
)
#-------------------------------------------------------------------#
main.cpp
#include <iostream>
#include <boost/stacktrace.hpp>
void tracesToCout( int _n )
{
std::cout << boost::stacktrace::stacktrace();
if( _n > 0 )
{
tracesToCout( --_n );
}
}
int main()
{
tracesToCout( 5 );
return 0;
}
CMake output:
-- The C compiler identification is GNU 7.4.0
-- The CXX compiler identification is GNU 7.4.0
-- Check for working C compiler: /usr/bin/cc
-- Check for working C compiler: /usr/bin/cc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Check for working CXX compiler: /usr/bin/c++
-- Check for working CXX compiler: /usr/bin/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Found Boost: /opt/Boost_1_68_0/include (found suitable version "1.68.0", minimum required is "1.68.0") found components: stacktrace_basic stacktrace_backtrace stacktrace_addr2line stacktrace_noop
-- Configuring done
-- Generating done
-- Build files have been written to: /home/user/Work/boost_stacktrace/Debug64
build output:
CMakeFiles/stack_trace_exmpl.dir/main.cpp.o: In function `boost::stacktrace::detail::location_from_symbol::location_from_symbol(void const*)':
main.cpp:(.text._ZN5boost10stacktrace6detail20location_from_symbolC2EPKv[_ZN5boost10stacktrace6detail20location_from_symbolC5EPKv]+0x4e): undefined reference to `dladdr'
CMakeFiles/stack_trace_exmpl.dir/main.cpp.o: In function `boost::stacktrace::frame::name[abi:cxx11]() const':
main.cpp:(.text._ZNK5boost10stacktrace5frame4nameB5cxx11Ev[_ZNK5boost10stacktrace5frame4nameB5cxx11Ev]+0x31): undefined reference to `dladdr'
collect2: error: ld returned 1 exit status
CMakeFiles/stack_trace_exmpl.dir/build.make:87: recipe for target 'stack_trace_exmpl' failed
make[2]: *** [stack_trace_exmpl] Error 1
CMakeFiles/Makefile2:75: recipe for target 'CMakeFiles/stack_trace_exmpl.dir/all' failed
make[1]: *** [CMakeFiles/stack_trace_exmpl.dir/all] Error 2
Makefile:83: recipe for target 'all' failed
make: *** [all] Error 2
Turning my comment into an answer:
Because the dl library is needed by boost::stacktrace (see the Boost requirements), the dl library should be linked after the Boost stacktrace libraries are linked. To ensure this ordering, link the dl library (and perhaps pthread as well) at the end of the target_link_libraries() call instead:
target_link_libraries(${PROJECT_NAME}
PUBLIC ${Boost_STACKTRACE_BASIC_LIBRARY}
PUBLIC ${Boost_STACKTRACE_BACKTRACE_LIBRARY}
PUBLIC ${Boost_STACKTRACE_ADDR2LINE_LIBRARY}
PUBLIC ${Boost_STACKTRACE_NOOP_LIBRARY}
PUBLIC pthread dl
)

Compiling and linking against OpenMP with AppleClang on Mac OS X Mojave

I've been trying to compile a simple OpenMP program using AppleClang on Mac OS X 10.14.5 Mojave with the CLion IDE.
main.cpp:
#include <omp.h>
#include <iostream>
int main() {
std::cout << omp_get_max_threads() << std::endl;
return 1;
}
CMakeLists.txt:
cmake_minimum_required(VERSION 3.14)
project(OpenMPTest)
set(CMAKE_CXX_STANDARD 17)
add_executable(OpenMPTest main.cpp)
find_package(OpenMP)
if (OPENMP_FOUND)
set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${OpenMP_C_FLAGS}")
set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${OpenMP_CXX_FLAGS}")
set (CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${OpenMP_EXE_LINKER_FLAGS}")
endif()
CMake output:
/Applications/CLion.app/Contents/bin/cmake/mac/bin/cmake -DCMAKE_BUILD_TYPE=Debug -G "CodeBlocks - Unix Makefiles" /Users/bully/CLionProjects/OpenMPTest
-- The C compiler identification is AppleClang 10.0.1.10010046
-- The CXX compiler identification is AppleClang 10.0.1.10010046
-- Check for working C compiler: /Library/Developer/CommandLineTools/usr/bin/cc
-- Check for working C compiler: /Library/Developer/CommandLineTools/usr/bin/cc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Check for working CXX compiler: /Library/Developer/CommandLineTools/usr/bin/c++
-- Check for working CXX compiler: /Library/Developer/CommandLineTools/usr/bin/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Found OpenMP_C: -Xclang -fopenmp (found version "3.1")
-- Found OpenMP_CXX: -Xclang -fopenmp (found version "3.1")
-- Found OpenMP: TRUE (found version "3.1")
-- Configuring done
-- Generating done
-- Build files have been written to: /Users/bully/CLionProjects/OpenMPTest/cmake-build-debug
When I build the project, I receive a linker error:
====================[ Build | all | Debug ]=====================================
/Applications/CLion.app/Contents/bin/cmake/mac/bin/cmake --build /Users/bully/CLionProjects/OpenMPTest/cmake-build-debug --target all -- -j 2
Scanning dependencies of target OpenMPTest
[ 50%] Building CXX object CMakeFiles/OpenMPTest.dir/main.cpp.o
[100%] Linking CXX executable OpenMPTest
Undefined symbols for architecture x86_64:
"_omp_get_max_threads", referenced from:
_main in main.cpp.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make[2]: *** [OpenMPTest] Error 1
make[1]: *** [CMakeFiles/OpenMPTest.dir/all] Error 2
make: *** [all] Error 2
How come this does not work? From the command line I can run clang++ main.cpp -lomp successfully after installing the library headers with open /Library/Developer/CommandLineTools/Packages/macOS_SDK_headers_for_macOS_10.14.pkg.
message(STATUS "Linker flags:" "${OpenMP_EXE_LINKER_FLAGS}") prints:
-- Linker flags:
If I replace the CMAKE_EXE_LINKER_FLAGS setter with set (CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -lomp") compilation with linking works. Why do I have to specify this manually? What's the preferred way of getting this to work in a platform-independent way? -fopenmp yields clang: error: unsupported option '-fopenmp'. gcc and MSVC on Linux and Windows respectively played nice with the CMake OpenMP configuration but Mac OS X does not.
As commented by Tsyvarev, the solution is to use the "updated" way of including OpenMP in CMake:
cmake_minimum_required(VERSION 3.14)
project(OpenMPTest)
set(CMAKE_CXX_STANDARD 17)
add_executable(OpenMPTest main.cpp)
find_package(OpenMP REQUIRED) # Find the package
target_link_libraries(${PROJECT_NAME} ${OpenMP_CXX_LIBRARIES}) # Link against it for C++
This compiled on Windows, Ubuntu and Mac OS X using their platform's default compilers respectively.
The accepted answer here is not recommended even though it also has the most upvotes.

problems linking boost with cmake

I would like to compile & link the following demo application using boost::logger but I get the following output:
d$ rm -rf *; cmake ..;make
-- The C compiler identification is GNU 5.4.0
-- The CXX compiler identification is GNU 5.4.0
-- Check for working C compiler: /usr/bin/cc
-- Check for working C compiler: /usr/bin/cc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Check for working CXX compiler: /usr/bin/c++
-- Check for working CXX compiler: /usr/bin/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Boost version: 1.67.0
-- Boost_LIBRARIES:
--
-- BOOST_INCLUDEDIR:
-- /path/to/env/include
-- Configuring done
-- Generating done
-- Build files have been written to: /path/to/src/tmp/logger/build
Scanning dependencies of target logger
[ 50%] Building CXX object CMakeFiles/logger.dir/logger.cpp.o
[100%] Linking CXX executable logger
CMakeFiles/logger.dir/logger.cpp.o: In function `init()':
/path/to/src/tmp/logger/logger.cpp:21: undefined reference to `boost::log::v2_mt_posix::core::get()'
/path/to/src/tmp/logger/logger.cpp:24: undefined reference to `boost::log::v2_mt_posix::core::set_filter(boost::log::v2_mt_posix::filter const&)'
CMakeFiles/logger.dir/logger.cpp.o: In function `boost::log::v2_mt_posix::attribute_name::attribute_name(char const*)':
/path/to/env/include/boost/log/attributes/attribute_name.hpp:80: undefined reference to `boost::log::v2_mt_posix::attribute_name::get_id_from_string(char const*)'
CMakeFiles/logger.dir/logger.cpp.o: In function `boost::log::v2_mt_posix::aux::light_rw_mutex::light_rw_mutex()':
/path/to/env/include/boost/log/detail/light_rw_mutex.hpp:103: undefined reference to `pthread_rwlock_init'
CMakeFiles/logger.dir/logger.cpp.o: In function `boost::log::v2_mt_posix::aux::light_rw_mutex::~light_rw_mutex()':
/path/to/env/include/boost/log/detail/light_rw_mutex.hpp:107: undefined reference to `pthread_rwlock_destroy'
CMakeFiles/logger.dir/logger.cpp.o: In function `boost::log::v2_mt_posix::aux::light_rw_mutex::lock_shared()':
/path/to/env/include/boost/log/detail/light_rw_mutex.hpp:111: undefined reference to `pthread_rwlock_rdlock'
CMakeFiles/logger.dir/logger.cpp.o: In function `boost::log::v2_mt_posix::aux::light_rw_mutex::unlock_shared()':
/path/to/env/include/boost/log/detail/light_rw_mutex.hpp:115: undefined reference to `pthread_rwlock_unlock'
CMakeFiles/logger.dir/logger.cpp.o: In function `boost::log::v2_mt_posix::aux::once_block_sentry::~once_block_sentry()':
...
...
now my CMakeLists.txt looks like:
cmake_minimum_required(VERSION 2.6)
project(LOGGER)
set(BOOST_INCLUDEDIR "/path/to/env/include")
set(BOOST_ROOT "/path/to/env/include")
set(Boost_NO_SYSTEM_PATHS on CACHE BOOL "Do not search system for Boost")
find_package(Boost REQUIRED)
message(STATUS Boost_LIBRARIES:)
message (STATUS ${Boost_LIBRARIES})
message(STATUS BOOST_INCLUDEDIR:)
message(STATUS ${BOOST_INCLUDEDIR})
ADD_EXECUTABLE(logger logger.cpp)
target_include_directories(logger PUBLIC ${BOOST_INCLUDEDIR})
set (CMAKE_CXX_FLAGS "-g -Wall -DBOOST_LOG_DYN_LINK")
and logger.cpp:
#include <iostream>
#include <boost/fusion/iterator/equal_to.hpp>
#include <boost/log/core.hpp>
#include <boost/log/trivial.hpp>
#include <boost/log/expressions.hpp>
#include <boost/log/utility/setup/file.hpp>
namespace logging = boost::log;
namespace src = boost::log::sources;
namespace sinks = boost::log::sinks;
namespace keywords = boost::log::keywords;
namespace expr = boost::log::expressions;
void init()
{
logging::add_file_log("sample.log");
logging::core::get()->set_filter
(
logging::trivial::severity >= logging::trivial::info
);
}
int main(void) {
init();
std::cout <<"Hello World!";
As you can see in the cmake output, ${Boost_LIBRARIES} did not return anything, I suspect this to be the culprit even though, the compiler found the boost includes at the non-standard path.
There are a number of issues with your CMakeLists.
Linking
You haven't linked your executable against Boost, so although you will have access to the declarations of Boost functions from the headers found at ${Boost_INCLUDEDIR}, you'll see undefined references to their definitions. Hence, you need to:
target_link_libraries(logger PRIVATE ${Boost_LIBRARIES})
This will resolve the linking issue.
Compile Definitions
Less problematically, your usage of CMAKE_CXX_FLAGS has some issues. Using set() for CMAKE_CXX_FLAGS is deprecated, but the correct usage is:
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} <value>")
Because set() overwrites. However, the more modern way to do this would be:
target_compile_options(logger PRIVATE -g -Wall)
target_compile_definitions(logger PRIVATE BOOST_LOG_DYN_LINK=true)
This will ensure that these definitions are used only for this exectuable. Note that this step will require a relatively recent version of CMake.
Include Directories
The findBoost module exports a number of variables, one of which is ${Boost_INCLUDE_DIRS}. This variable represents the directories in which boost files are stored. Boost also looks for a variable, ${BOOST_INCLUDEDIR}, which is the path to the top-level directory of the boost headers. Currently, you're using the latter to represent the former, which should be corrected.

Qt Project using cmake in CLion C++

To be honest im not a fan of Qt Creator. So I decided to use another IDE like CLion. I researched and found out that Clion doesnt support qmake, but it is possible to get a Qt-Project to run with cmake.
Thats my CMakeList. CLion don't give me any error when i save this make-file.
cmake_minimum_required(VERSION 3.8)
project(Qt_CmakeTest)
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
set(SOURCE_FILES main.cpp)
find_package(Qt5Core REQUIRED)
find_package(Qt5Widgets REQUIRED)
add_executable(Qt_CmakeTest ${SOURCE_FILES})
target_link_libraries(Qt_CmakeTest Qt5::Core Qt5::Widgets)
to test if it worked I created a simple cpp.
#include <iostream>
#include <QApplication>
using namespace std;
int main(int argc, char **argv)
{
QApplication app(argc,argv);
return app.exec();
}
I tried to implement it following these instructions
How to configure CLion IDE for Qt Framework?
I have to use the cmake binary that qt provides. So I set the Cmake Option to:
-DCMAKE_PREFIX_PATH=C:\Qt\Qt5.9.1\5.9.1\mingw53_32\lib\cmake
But if I try to compile it it gives me this error:
"C:\Program Files\JetBrains\CLion 2017.2.2\bin\cmake\bin\cmake.exe" --build
C:\Users\Marcel\Desktop\Projekte\Qt\Qt-CmakeTest\cmake-build-debug --target Qt_CmakeTest -- -j 2
[ 50%] Linking CXX executable Qt_CmakeTest.exe
CMakeFiles\Qt_CmakeTest.dir/objects.a(main.cpp.obj): In function `main':
C:/Users/Marcel/Desktop/Projekte/Qt/Qt-CmakeTest/main.cpp:7: undefined reference to `_imp___ZN12QApplicationC1ERiPPci'
C:/Users/Marcel/Desktop/Projekte/Qt/Qt-CmakeTest/main.cpp:8: undefined reference to `_imp___ZN12QApplication4execEv'
C:/Users/Marcel/Desktop/Projekte/Qt/Qt-CmakeTest/main.cpp:7: undefined reference to `_imp___ZN12QApplicationD1Ev'
C:/Users/Marcel/Desktop/Projekte/Qt/Qt-CmakeTest/main.cpp:7: undefined reference to `_imp___ZN12QApplicationD1Ev'
collect2.exe: error: ld returned 1 exit status
mingw32-make.exe[3]: *** [Qt_CmakeTest.exe] Error 1
CMakeFiles\Qt_CmakeTest.dir\build.make:99: recipe for target 'Qt_CmakeTest.exe' failed
CMakeFiles\Makefile2:66: recipe for target 'CMakeFiles/Qt_CmakeTest.dir/all' failed
CMakeFiles\Makefile2:78: recipe for target 'CMakeFiles/Qt_CmakeTest.dir/rule' failed
Makefile:117: recipe for target 'Qt_CmakeTest' failed
mingw32-make.exe[2]: *** [CMakeFiles/Qt_CmakeTest.dir/all] Error 2
mingw32-make.exe[1]: *** [CMakeFiles/Qt_CmakeTest.dir/rule] Error 2
mingw32-make.exe: *** [Qt_CmakeTest] Error 2
I downloaded different versions of Qt because Qt 5.9 uses Mingw 5.3 and CLion says I'm using Mingw 5.0 and at the beginning I used Qt 5.6 and it used Mingw 4.9. I can't really figure out why it is not working.
A clean CMake Build looks like this
"C:\Program Files\JetBrains\CLion 2017.2.2\bin\cmake\bin\cmake.exe" -DCMAKE_BUILD_TYPE=Debug -G "CodeBlocks - MinGW Makefiles" C:\Users\Marcel\Desktop\Projekte\Qt\Qt-CmakeTest
-- The C compiler identification is GNU 6.3.0
-- The CXX compiler identification is GNU 6.3.0
-- Check for working C compiler: C:/MinGW/bin/gcc.exe
-- Check for working C compiler: C:/MinGW/bin/gcc.exe -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Check for working CXX compiler: C:/MinGW/bin/g++.exe
-- Check for working CXX compiler: C:/MinGW/bin/g++.exe -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Configuring done
-- Generating done
-- Build files have been written to: C:/Users/Marcel/Desktop/Projekte/Qt/Qt-CmakeTest/cmake-build-debug
[Finished]
Somehow there was no PATH to the Qt binary. I have added the PATH and rebuild the cmake Project and it worked. So it was just a matter of the path and of the versions.