Adding libconfig++ to CMake - cmake

I'm trying to add libconfig++ to my CMake project, but it still complains
Libconfig library is config++
...
CMakeFiles/CustomCamReaderTask.dir/acA2440-20gc_ConfigUtility.cc.o: undefined reference to symbol '_ZN13ConfigSection3getERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEERl'
.../lib/libconfig.so: error adding symbols: DSO missing from command line
collect2: error: ld returned 1 exit status
Here is my CMakeLists.txt
find_package(PkgConfig)
pkg_check_modules(LIBCONFIG_PLUSPLUS libconfig++ REQUIRED)
add_executable(CustomCamReaderTask
CustomCamReaderTask.cc
acA2440-20gc_ConfigUtility.cc)
target_include_directories(CustomCamReaderTask
PRIVATE ${CMAKE_SOURCE_DIR}/src/include/Pylon5
/opt/pylon5/include)
message("Libconfig library is ${LIBCONFIG_PLUSPLUS_LIBRARIES}")
target_link_libraries(CustomCamReaderTask
${LIBCONFIG_PLUSPLUS_LIBRARIES}
${PYLON_LIBS}
task
imageUtility
${OPENCV_LIBRARIES})
link_directories(/opt/pylon5/lib64)
link_libraries(${LIBCONFIG_PLUSPLUS_LIBRARIES})
It seems to find config++, I linked the directory, linked the library, not sure what else is wrong..

Related

What causes the following linking error with CMake and Imagemagick library?

I have a main.cpp file that includes a header file from the same directory where I have used the boost library, magickimage c++ library, and eigen library. Here is my CMakeLists.txt file where I have linked the .lib and .a files to my target and set the includer paths.
cmake_minimum_required(VERSION 3.20.2)
project(blackjackAI)
add_executable(blackjackAI main.cpp)
target_link_libraries(blackjackAI PRIVATE C:/Users/Ejer/Desktop/c++/c++Dep/boost_1_77_0/stage/lib/libboost_filesystem-gcc10-mt-x64-1_77.a)
target_link_libraries(blackjackAI PRIVATE C:/Users/Ejer/Desktop/c++/c++Dep/ImageMagick-7.1.0-Q16-HDRI/lib/CORE_RL_Magick++_.lib)
target_link_libraries(blackjackAI PRIVATE C:/Users/Ejer/Desktop/c++/c++Dep/ImageMagick-7.1.0-Q16-HDRI/lib/CORE_RL_MagickCore_.lib)
target_link_libraries(blackjackAI PRIVATE C:/Users/Ejer/Desktop/c++/c++Dep/ImageMagick-7.1.0-Q16-HDRI/lib/CORE_RL_MagickWand_.lib)
target_include_directories(blackjackAI PRIVATE C:/Users/Ejer/Desktop/c++/c++Dep/ImageMagick-7.1.0-Q16-HDRI/include)
target_include_directories(blackjackAI PRIVATE C:/Users/Ejer/Desktop/c++/c++Dep/eigen-3.4.0)
target_include_directories(blackjackAI PRIVATE C:/Users/Ejer/Desktop/c++/c++Dep/boost_1_77_0)
target_include_directories(blackjackAI PRIVATE C:/Users/Ejer/Desktop/c++/blackjackAI)
For some reason when I compile my CMakeLists.txt file with the "MinGW Makefiles" generator and then use the makefile I get the following linker error:
/usr/lib/gcc/x86_64-pc-cygwin/10/../../../../x86_64-pc-cygwin/bin/ld: CMakeFiles\blackjackAI.dir/objects.a(main.cpp.obj):C:\Users\Ejer
\Desktop\c++\blackjackAI\main.cpp:(.text$_ZN6CnnNetC1ESs[_ZN6CnnNetC1ESs]+0x3dd): undefined reference to `Magick::Image::Image()'
/usr/lib/gcc/x86_64-pc-cygwin/10/../../../../x86_64-pc-cygwin/bin/ld: CMakeFiles\blackjackAI.dir/objects.a(main.cpp.obj):C:\Users\Ejer
\Desktop\c++\blackjackAI\main.cpp:(.text$_ZN6CnnNetC1ESs[_ZN6CnnNetC1ESs]+0x413): undefined reference to `Magick::Image::read(std::str
ing const&)'
/usr/lib/gcc/x86_64-pc-cygwin/10/../../../../x86_64-pc-cygwin/bin/ld: CMakeFiles\blackjackAI.dir/objects.a(main.cpp.obj):C:\Users\Ejer
\Desktop\c++\blackjackAI\main.cpp:(.text$_ZN6CnnNetC1ESs[_ZN6CnnNetC1ESs]+0x436): undefined reference to `Magick::Image::type(MagickCo
re::ImageType)'
/usr/lib/gcc/x86_64-pc-cygwin/10/../../../../x86_64-pc-cygwin/bin/ld: CMakeFiles\blackjackAI.dir/objects.a(main.cpp.obj):C:\Users\Ejer
\Desktop\c++\blackjackAI\main.cpp:(.text$_ZN6CnnNetC1ESs[_ZN6CnnNetC1ESs]+0x45b): undefined reference to `Magick::Image::~Image()'
/usr/lib/gcc/x86_64-pc-cygwin/10/../../../../x86_64-pc-cygwin/bin/ld: CMakeFiles\blackjackAI.dir/objects.a(main.cpp.obj):C:\Users\Ejer
\Desktop\c++\blackjackAI\main.cpp:(.text$_ZN6CnnNetC1ESs[_ZN6CnnNetC1ESs]+0x539): undefined reference to `Magick::Image::~Image()'
/usr/lib/gcc/x86_64-pc-cygwin/10/../../../../x86_64-pc-cygwin/bin/ld: CMakeFiles\blackjackAI.dir/objects.a(main.cpp.obj):C:\Users\Ejer
\Desktop\c++\blackjackAI\main.cpp:(.text$_ZN9__gnu_cxx13new_allocatorIN6Magick5ImageEE9constructIS2_JRKS2_EEEvPT_DpOT0_[_ZN9__gnu_cxx1
3new_allocatorIN6Magick5ImageEE9constructIS2_JRKS2_EEEvPT_DpOT0_]+0x46): undefined reference to `Magick::Image::Image(Magick::Image co
nst&)'
/usr/lib/gcc/x86_64-pc-cygwin/10/../../../../x86_64-pc-cygwin/bin/ld: CMakeFiles\blackjackAI.dir/objects.a(main.cpp.obj):C:\Users\Ejer
\Desktop\c++\blackjackAI\main.cpp:(.text$_ZSt10_ConstructIN6Magick5ImageEJRKS1_EEvPT_DpOT0_[_ZSt10_ConstructIN6Magick5ImageEJRKS1_EEvP
T_DpOT0_]+0x42): undefined reference to `Magick::Image::Image(Magick::Image const&)'
collect2: error: ld returned 1 exit status
CMakeFiles\blackjackAI.dir\build.make:102: recipe for target 'blackjackAI.exe' failed
mingw32-make[2]: *** [blackjackAI.exe] Error 1
CMakeFiles\Makefile2:81: recipe for target 'CMakeFiles/blackjackAI.dir/all' failed
mingw32-make[1]: *** [CMakeFiles/blackjackAI.dir/all] Error 2
Makefile:89: recipe for target 'all' failed
mingw32-make: *** [all] Error 2
It says that I have undefined references to the use of the Image class in my header file, even though I have linked my includer path and .lib files in CMakeLists.txt file and my main.cpp file to the imagemagick library. What is causing the problem?

CMake Cross Compilation secondary dependencies

I have a program that compiles on my target machine (NVIDIA Jetson). Now I am trying to cross compile the same program on my laptop. I have a cross compiler and cloned the Jetson to use as a sysroot. I was following this guide from NVIDIA. My program compiles, but fails at a linking step. I'm linking against a library (protobuf) and the linker can't find the dependencies of the library. I have produced a minimal example that demonstrates this.
main.cpp:
#include <google/protobuf/util/json_util.h>
int main(int argc, char* argv[])
{
google::protobuf::util::JsonPrintOptions jsonOptions;
jsonOptions.always_print_enums_as_ints = true;
return 0;
}
CMakeLists.txt:
cmake_minimum_required(VERSION 3.20)
project(Test)
find_package(Protobuf REQUIRED)
add_executable(Test main.cpp)
target_include_directories(Test PRIVATE ${PROTOBUF_INCLUDE_DIRS})
target_link_libraries(Test PRIVATE ${PROTOBUF_LIBRARIES})
On my target computer CMake generates and builds this fine, but when cross compiling on my laptop CMake generates the makefiles, and the build step fails with the error:
[ 50%] Building CXX object CMakeFiles/Test.dir/main.cpp.o
[100%] Linking CXX executable Test
/home/lhahn/Documents/CrossCompilation/gcc-linaro-7.3.1-2018.05-x86_64_aarch64-linux-gnu/bin/../lib/gcc/aarch64-linux-gnu/7.3.1/../../../../aarch64-linux-gnu/bin/ld: warning: libz.so.1, needed by /home/lhahn/Documents/CrossCompilation/Images/jetson/usr/local/lib/libprotobuf.so, not found (try using -rpath or -rpath-link)
/home/lhahn/Documents/CrossCompilation/gcc-linaro-7.3.1-2018.05-x86_64_aarch64-linux-gnu/bin/../lib/gcc/aarch64-linux-gnu/7.3.1/../../../../aarch64-linux-gnu/bin/ld: warning: libm.so.6, needed by /home/lhahn/Documents/CrossCompilation/gcc-linaro-7.3.1-2018.05-x86_64_aarch64-linux-gnu/bin/../lib/gcc/aarch64-linux-gnu/7.3.1/../../../../aarch64-linux-gnu/lib/../lib64/libstdc++.so, not found (try using -rpath or -rpath-link)
/home/lhahn/Documents/CrossCompilation/Images/jetson/usr/local/lib/libprotobuf.so: undefined reference to `deflateInit2_'
/home/lhahn/Documents/CrossCompilation/Images/jetson/usr/local/lib/libprotobuf.so: undefined reference to `deflate'
/home/lhahn/Documents/CrossCompilation/Images/jetson/usr/local/lib/libprotobuf.so: undefined reference to `deflateEnd'
/home/lhahn/Documents/CrossCompilation/Images/jetson/usr/local/lib/libprotobuf.so: undefined reference to `inflate'
/home/lhahn/Documents/CrossCompilation/Images/jetson/usr/local/lib/libprotobuf.so: undefined reference to `inflateInit2_'
/home/lhahn/Documents/CrossCompilation/Images/jetson/usr/local/lib/libprotobuf.so: undefined reference to `inflateEnd'
collect2: error: ld returned 1 exit status
make[2]: *** [CMakeFiles/Test.dir/build.make:98: Test] Error 1
make[1]: *** [CMakeFiles/Makefile2:83: CMakeFiles/Test.dir/all] Error 2
make: *** [Makefile:91: all] Error 2
The required library (ZLIB) exists in my sysroot (/home/lhahn/Documents/CrossCompilation/Images/jetson/usr/lib/aarch64-linux-gnu/libz.so), but the linker can't find it.
My toolchain file is
set(CMAKE_SYSTEM_NAME Linux)
set(CMAKE_SYSTEM_PROCESSOR aarch64)
set(CMAKE_SYSROOT /home/lhahn/Documents/CrossCompilation/Images/jetson)
set(CMAKE_C_COMPILER /home/lhahn/Documents/CrossCompilation/gcc-linaro-7.3.1-2018.05-x86_64_aarch64-linux-gnu/bin/aarch64-linux-gnu-gcc)
set(CMAKE_CXX_COMPILER /home/lhahn/Documents/CrossCompilation/gcc-linaro-7.3.1-2018.05-x86_64_aarch64-linux-gnu/bin/aarch64-linux-gnu-g++)
set(CMAKE_FIND_ROOT_PATH /home/lhahn/Documents/CrossCompilation/Images/jetson)
set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)
set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)
set(CMAKE_FIND_ROOT_PATH_MODE_PACKAGE ONLY)
What do I need to tell CMake so that the linker can find these secondary dependencies?
I figured out my problem. When I cloned the image and mounted it on my laptop I broke all of the symlinks in the cloned image, such as libz.so which is why cmake couldn't find them. When I fixed the symlink cmake went on to complain about the next broken symlink in the same manner. I think there are too many symlinks to fix them manually, but at least now I know what went wrong.

cmake on windows. Link libraries in folder

Disclaimer: I'm new to cmake so i have no idea what I'm doing.
All guides and tutorials i find seem to think I'm running a 20 man team that needs to work together.
All I'm trying to do is put all my libraries in an include folder and lib folder.
In visual studio and code::blocks i just set up the linker in the IDE, but cmake is a little hard for me to wrap my head around.
I've set include and link directories with
include_directories(${PROJECT_SOURCE_DIR}/include)
link_directories(${PROJECT_SOURCE_DIR}/lib)
Then i try to
target_link_libraries(${PROJECT_NAME} jsoncpp)
This however produces undefined references. Is there a simple way to add and link libraries without me having to google and add a cmake module for each one?
Edit:
attempts:
target_link_libraries(${PROJECT_NAME} libjsoncpp.a)
error: undefined reference
target_link_libraries(${PROJECT_NAME} libjsoncpp)
error: cannot find -llibjsoncpp
target_link_libraries(${PROJECT_NAME} jsoncpp)
error: undefined reference
target_link_libraries(${PROJECT_NAME} C:/Repos/prosjekt-bad-racoon/lib/libjsoncpp.a)
error: undefined reference
target_link_libraries(${PROJECT_NAME} ${PROJECT_SOURCE_DIR}/lib/libjsoncpp.a)
error: undefined reference
Edit:
Full cmake file
cmake_minimum_required(VERSION 3.8)
project(prosjekt_bad_racoon)
set(CMAKE_CXX_STANDARD 11)
include_directories(${PROJECT_SOURCE_DIR}/include)
link_directories(${PROJECT_SOURCE_DIR}/lib)
set(SOURCE_FILES main.cpp Core.cpp Core.h Collider.cpp Collider.h Player.cpp Player.h Texture.cpp Texture.h AudioController.cpp AudioController.h)
add_executable(prosjekt_bad_racoon ${SOURCE_FILES})
set(EXECUTABLE_NAME ${PROJECT_NAME})
set(CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/cmake_modules" ${CMAKE_MODULE_PATH})
target_link_libraries(${PROJECT_NAME} libjsoncpp.a)
find_package(SFML 2 REQUIRED system window graphics network audio)
if(SFML_FOUND)
include_directories(${SFML_INCLUDE_DIR})
target_link_libraries(${EXECUTABLE_NAME} ${SFML_LIBRARIES})
link_directories("${PROJECT_SOURCE_DIR}")
endif()
"C:\Program Files\JetBrains\CLion 2017.2.2\bin\cmake\bin\cmake.exe" --build C:\Repos\prosjekt-bad-racoon\cmake-build-debug --target prosjekt_bad_racoon -- -j 2
[ 14%] Linking CXX executable prosjekt_bad_racoon.exe
CMakeFiles\prosjekt_bad_racoon.dir/objects.a(Core.cpp.obj): In function `ZN4CoreC2Ev':
C:/Repos/prosjekt-bad-racoon/Core.cpp:3: undefined reference to `Json::Value::Value(char const*)'
CMakeFiles\prosjekt_bad_racoon.dir/objects.a(Core.cpp.obj): In function `ZN4CoreD2Ev':
C:/Repos/prosjekt-bad-racoon/Core.cpp:8: undefined reference to `Json::Value::~Value()'
collect2.exe: error: ld returned 1 exit status
CMakeFiles\prosjekt_bad_racoon.dir\build.make:236: recipe for target 'prosjekt_bad_racoon.exe' failed
mingw32-make.exe[3]: *** [prosjekt_bad_racoon.exe] Error 1
CMakeFiles\Makefile2:66: recipe for target 'CMakeFiles/prosjekt_bad_racoon.dir/all' failed
mingw32-make.exe[2]: *** [CMakeFiles/prosjekt_bad_racoon.dir/all] Error 2
CMakeFiles\Makefile2:78: recipe for target 'CMakeFiles/prosjekt_bad_racoon.dir/rule' failed
mingw32-make.exe[1]: *** [CMakeFiles/prosjekt_bad_racoon.dir/rule] Error 2
Makefile:117: recipe for target 'prosjekt_bad_racoon' failed
mingw32-make.exe: *** [prosjekt_bad_racoon] Error 2

CMake, SDL on Mac OS X, "can't find -lSDLmain"

Following the instructions here, I've set up a CMakeLists.txt:
Find_Package (SDL REQUIRED)
Find_Package (SDL_image REQUIRED)
link_libraries (
${SDL_LIBRARY}
${SDLIMAGE_LIBRARY}
SDLmain
)
When running cmake, I get the following error:
ld: library not found for -lSDLmain
collect2: error: ld returned 1 exit status
make[2]: *** [src/GameOfLife] Error 1
Running g++ by hand gives the same error:
$ g++-4.7 -std=c++0x ../src/*.cpp -lSDLmain
ld: library not found for -lSDLmain
How do I fix this?
make doesn't know where to find SDLmain; I need to link to the directory using link_directory in `CMakeLists.txt.
Running
$ g++-4.7 -std=c++0x ../src/*.cpp `sdl-config --libs`
works fine, so I've clearly got SDL installed correctly. Checking the output of sdl-config --libs:
$ sdl-config --libs
-L/opt/local/lib -lSDLmain -lSDL -Wl,-framework,Cocoa
So the thing that's not in the CMakeLists.txt is the -L/opt/local/lib. That should be added into the CMakeLists.txt using link_directory:
link_directories( /opt/local/lib )
And then cmake runs fine.

(Linux) Unable to link archives via cmake

On the command line, the following produces an executable:
g++ -o a.out main.cpp class1.cc class2.cc /usr/lib/libgsl.a /usr/lib/libgslcblas.a
However I am unsure how to get cmake to work properly.
When I add a line like
include_directories(/usr/lib/)
link_libraries(usr/lib/libgsl.a usr/libgslcblas.a)
the configuring seems to work but building fails:
CMakeFiles/kmv.dir/main.o: In function `main':
main.cpp:27: undefined reference to `gsl_matrix_alloc'
main.cpp:35: undefined reference to `gsl_matrix_fscanf'
collect2: ld returned 1 exit status
make[2]: *** [kmv] Error 1
make[1]: *** [CMakeFiles/kmv.dir/all] Error 2
make: *** [all] Error 2
*** Failed ***
Seems to be a synthax problem. Any hint is welcome. Thank you.
Instead of
include_directories(/usr/lib)
link_libraries(usr/lib/libgsl.a usr/libgslcblas.a)
try
add_executable (targetName main.cpp class1.cc class2.cc)
target_link_libraries(targetName gsl gslcblas)
Where targetName is the name of the output binary you intend to create. The path /usr/lib should already be in the default library search path for CMake, so you shouldn't have to specify that, but if you did have to specify a custom library path, you would do it like so
link_directories(/some/custom/library/path)
The include_directories CMake directive is used for adding header search paths, not library search paths...
Probably, link_libraries is deprecated
http://www.cmake.org/pipermail/cmake/2009-April/028439.html
Try using target_link_libraries instead.