I'm new to cmake, and could't figure out how to achieve this after a few days.
I'm trying to build a C++ project that depends on OpenCV using cmake, but I want cmake to clone and install it like this. I found a project's CMakeFile that I'm using as reference to accomplish this.So I have this:
main.cpp:
#include <iostream>
#include <opencv2/opencv.hpp>
int main() {
std::cout << "OpenCV Version: " + cv::getVersionString() << std::endl;
return 0;
}
CMakeLists.txt:
cmake_minimum_required(VERSION 3.12)
project(cv_playground)
set(CMAKE_CXX_STANDARD 14)
find_package(Git REQUIRED)
include(ExternalProject)
# OpenCV
set(OPENCV_INSTALL_LOCATION ${CMAKE_BINARY_DIR}/opencv)
ExternalProject_Add(opencv
GIT_REPOSITORY https://github.com/opencv/opencv
GIT_TAG 68942affdbc4677aa845bc4307d4752182324a0e # 4.0.0-alpha
SOURCE_DIR opencv
BINARY_DIR opencv-build
CMAKE_ARGS
-DCMAKE_INSTALL_PREFIX=${OPENCV_INSTALL_LOCATION}
)
include_directories(${OPENCV_INSTALL_LOCATION}/include/opencv4)
link_directories(${OPENCV_INSTALL_LOCATION}/lib)
add_executable(cv_playground main.cpp)
add_dependencies(cv_playground opencv)
target_link_libraries(cv_playground opencv_core opencv_dnn opencv_features2d opencv_flann opencv_highgui opencv_imgcodecs)
But when building the project I get a lot of undefined references (pthread, gz, dlopen/dlclose, itt_domain_create_ptr, etc)
I would like to know how to fix those undef refs, I already installed pthread, zlib, etc but I don't know how to make cmake to use them. I tried to add them in the target_link_libraries
for example but still gives me the same errors:
CMakeLists.txt (note the CMAKE_DL_LIBS in target_link_libraries):
cmake_minimum_required(VERSION 3.12)
project(cv_playground)
set(CMAKE_CXX_STANDARD 14)
set(THREADS_PREFER_PTHREAD_FLAG ON)
find_package(Threads REQUIRED)
find_package(Git REQUIRED)
include(ExternalProject)
# OpenCV
set(OPENCV_INSTALL_LOCATION ${CMAKE_BINARY_DIR}/opencv)
ExternalProject_Add(opencv
GIT_REPOSITORY https://github.com/opencv/opencv
GIT_TAG 68942affdbc4677aa845bc4307d4752182324a0e # 4.0.0-alpha
SOURCE_DIR opencv
BINARY_DIR opencv-build
CMAKE_ARGS
-DWITH_OPENGL=OFF
-DCMAKE_INSTALL_PREFIX=${OPENCV_INSTALL_LOCATION}
)
include_directories(${OPENCV_INSTALL_LOCATION}/include/opencv4)
link_directories(${OPENCV_INSTALL_LOCATION}/lib)
add_executable(cv_playground main.cpp)
add_dependencies(cv_playground opencv)
target_link_libraries(cv_playground Threads::Threads ${CMAKE_DL_LIBS} opencv_core opencv_dnn opencv_features2d opencv_flann opencv_highgui opencv_imgcodecs)
Error message (still get undefined reference to dlopen, dlclose, ...)
[ 90%] Linking CXX executable cv_playground
/home/syonekura/CLionProjects/cv_playground/cmake-build-debug/opencv/lib/libopencv_core.a(system.cpp.o): In function `cv::TLSData<cv::(anonymous namespace)::ThreadID>::createDataInstance() const':
system.cpp:(.text._ZNK2cv7TLSDataINS_12_GLOBAL__N_18ThreadIDEE18createDataInstanceEv+0x37): undefined reference to `__itt_thread_set_name_ptr__3_0'
/home/syonekura/CLionProjects/cv_playground/cmake-build-debug/opencv/lib/libopencv_core.a(opencl_core.cpp.o): In function `GetHandle(char const*)':
opencl_core.cpp:(.text._ZL9GetHandlePKc+0x7): undefined reference to `dlopen'
opencl_core.cpp:(.text._ZL9GetHandlePKc+0x1e): undefined reference to `dlsym'
opencl_core.cpp:(.text._ZL9GetHandlePKc+0x53): undefined reference to `dlclose'
/home/syonekura/CLionProjects/cv_playground/cmake-build-debug/opencv/lib/libopencv_core.a(opencl_core.cpp.o): In function `opencl_check_fn(int)':
opencl_core.cpp:(.text._ZL15opencl_check_fni+0x3f): undefined reference to `dlsym'