Trying to add Boost libraries to Cmake.txt (Clion Ide) - boost

I see a lot of questions that are similar to this though not the same.
I'm having an issue getting boost recognized/working in Cmake using Clion I've tried a handful of ways including the incboost template included with Clion, moving on.
Cmake version 3.4
Boost version 1.60.0
find_package(Boost)
if (Boost_FOUND)
include_directories(${Boost_INCLUDE_DIR})
endif()
That's the auto generated incboost I mentioned above, This produces no results.
Second attempt is the following
set(Boost_Dir "C:\\Program Files (x86)\\boost_1_60_0\\")
find_package(Boost 1.60.0 COMPONENTS filesystem)
include_directories(${Boost_Dir})
CmakeFiles\Svp.dir/objects.a(main.cpp.obj):
In function`_static_initialization_and_destruction_0':
C:/PROGRA~2/BOOST_~1/boost/system/error_code.hpp:221: undefined reference
to `boost::system::generic_category()'
C:/PROGRA~2/BOOST_~1/boost/system/error_code.hpp:222: undefined
reference to `boost::system::generic_category()'
C:/PROGRA~2/BOOST_~1/boost/system/error_code.hpp:223: undefined
reference to `boost::system::system_category()'
collect2.exe: error: ld returned 1 exit status
mingw32-make.exe[3]: *** [Svp.exe] Error 1
CMakeFiles\Svp.dir\build.make:96: recipe for target 'Svp.exe' failed
CMakeFiles\Makefile2:66: recipe for target 'CMakeFiles/Svp.dir/all' failed
mingw32-make.exe[2]: *** [CMakeFiles/Svp.dir/all] Error 2
CMakeFiles\Makefile2:78: recipe for target 'CMakeFiles/Svp.dir/rule' failed
mingw32-make.exe[1]: *** [CMakeFiles/Svp.dir/rule] Error 2
Makefile:117: recipe for target 'Svp' failed
mingw32-make.exe: *** [Svp] Error 2
My Third attempt produces this
set(boost "C:\\Program Files (x86)\\boost_1_60_0\\boost\\")
INCLUDE_DIRECTORIES(${boost})
fatal error: boost/filesystem/config.hpp: No such file or directory
# include <boost/filesystem/config.hpp>
^
compilation terminated.
mingw32-make.exe[3]: *** [CMakeFiles/Svp.dir/main.cpp.obj] Error 1
mingw32-make.exe[2]: *** [CMakeFiles/Svp.dir/all] Error 2
mingw32-make.exe[1]: *** [CMakeFiles/Svp.dir/rule] Error 2
And finally the solutions given here How do you add boost libraries in CMakeLists.txt have not changed my error output.

here's how I use it, in a portable way, in my projects:
if (WIN32)
set(BOOST_ROOT "C:/Program Files (x86)/boost_1_60_0/")
set(Boost_USE_STATIC_LIBS ON)
set(Boost_USE_MULTITHREAD ON)
include_directories(${BOOST_ROOT})
link_directories(${BOOST_ROOT}/stage/lib) # add this before add_executable()
endif()
# Here call your add_executable method
# add_executable(TARGET_NAME ... )
if(NOT MSVC)
find_package(Boost REQUIRED COMPONENTS date_time filesystem wserialization system serialization thread regex)
if (Boost_FOUND)
include_directories(${Boost_INCLUDE_DIRS})
target_link_libraries(TARGET_NAME ${Boost_LIBRARIES})
endif()
endif()
You sometime need to specify the Boost_USE_STATIC_LIBS and Boost_USE_MULTITHREAD variables to help find_package find boost.
Also please specify the correct path to boost library with set(BOOST_ROOT, $path). Moreover, specify the boost packages you need in find_package. For instance
find_package(Boost REQUIRED COMPONENTS date_time filesystem thread regex)
if you only need the date_time filesystem thread regex packages
let me know if it works or not for you.

Related

How to install static libz.dll.a,libssl.dll.a, etc on MinGW/MSYS2?

I'm getting this on my project's linking process (compiles with cmake):
[100%] Linking CXX executable roosek2.exe
/usr/lib/gcc/x86_64-pc-msys/9.1.0/../../../../x86_64-pc-msys/bin/ld: cannot find -lC:/msys64/mingw64/lib/libz.dll.a
/usr/lib/gcc/x86_64-pc-msys/9.1.0/../../../../x86_64-pc-msys/bin/ld: cannot find -lC:/msys64/mingw64/lib/libssl.dll.a
/usr/lib/gcc/x86_64-pc-msys/9.1.0/../../../../x86_64-pc-msys/bin/ld: cannot find -lC:/msys64/mingw64/lib/libcrypto.dll.a
/usr/lib/gcc/x86_64-pc-msys/9.1.0/../../../../x86_64-pc-msys/bin/ld: cannot find -lC:/msys64/mingw64/lib/libzstd.dll.a
collect2: error: ld returned 1 exit status
make[2]: *** [CMakeFiles/roosek2.dir/build.make:578: roosek2.exe] Error 1
make[2]: Leaving directory '/home/LZ/roosek_ocr2'
make[1]: *** [CMakeFiles/Makefile2:77: CMakeFiles/roosek2.dir/all] Error 2
make[1]: Leaving directory '/home/LZ/roosek_ocr2'
make: *** [Makefile:84: all] Error 2
This happens when I compile with static qt5 from MSYS2 in MinGW shell. Here's my CMakeLists.txt:
cmake_minimum_required(VERSION 3.5)
project(roosek2 LANGUAGES CXX)
set(CMAKE_INCLUDE_CURRENT_DIR ON)
set(CMAKE_AUTOUIC ON)
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC ON)
set(CMAKE_CXX_STANDARD 14)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
find_package(Qt5 COMPONENTS Core Quick REQUIRED)
add_executable(roosek2
main.cpp
qml.qrc
ImageStream.cpp
YuvUtils.cpp
CameraView.cpp
)
add_library(ctmedia_codec MODULE IMPORTED)
add_library(ctstream MODULE IMPORTED)
add_library(maincore MODULE IMPORTED)
#link_directories(${CMAKE_CURRENT_SOURCE_DIR}/win_bin/lib/x64)
set_target_properties(ctmedia_codec PROPERTIES IMPORTED_LOCATION ${CMAKE_CURRENT_SOURCE_DIR}/win_bin/lib/x64/CTMedia.lib)
set_target_properties(ctstream PROPERTIES IMPORTED_LOCATION ${CMAKE_CURRENT_SOURCE_DIR}/win_bin/lib/x64/CTStream.lib)
set_target_properties(maincore PROPERTIES IMPORTED_LOCATION ${CMAKE_CURRENT_SOURCE_DIR}/win_bin/lib/x64/WPMainCore.lib)
include_directories("include")
target_compile_definitions(roosek2
PRIVATE $<$<OR:$<CONFIG:Debug>,$<CONFIG:RelWithDebInfo>>:QT_QML_DEBUG>)
target_link_libraries(roosek2
PRIVATE Qt5::Core Qt5::Quick ctmedia_codec ctstream maincore)
How do I install these libraries? Why are they being required? Do I really need them?
Install these packages:
For X86_64 (64 bit):
pacman -S mingw-w64-x86_64-openssl mingw-w64-x86_64-zlib mingw-w64-x86_64-zstd
For X86 (32 bit):
pacman -S mingw-w64-i686-openssl mingw-w64-i686-zlib mingw-w64-i686-zstd

undefined reference to symbol '_ZN5boost6system15system_categoryEv' /

this is not the first time I meet this error, but [previous solution][1] doesn't make sense.
[100%] Linking CXX executable ../bin/qttest
/usr/bin/x86_64-linux-gnu-ld: CMakeFiles/qttest.dir/src/main.cpp.o:
undefined reference to symbol '_ZN5boost6system15system_categoryEv'
/usr/lib/x86_64-linux-gnu/libboost_system.so: error adding symbols:
DSO missing from command line collect2: error: ld returned 1 exit
status
here is my cmakelists
cmake_minimum_required(VERSION 2.4.6)
set(OpenCV_DIR "/usr/local/opencv-2.4.9/share/OpenCV")
include_directories("/usr/local/opencv-2.4.9/include")
include($ENV{ROS_ROOT}/core/rosbuild/rosbuild.cmake)
find_package(Qt4 COMPONENTS QtCore QtGui)
find_package(OpenCV 2.4.9 REQUIRED)
INCLUDE(${QT_USE_FILE})
ADD_DEFINITIONS(${QT_DEFINITIONS})
rosbuild_init()
rosbuild_genmsg()
find_package(Boost COMPONENTS system REQUIRED)
set(qt_srcs
src/mainwindow.cpp
src/listnerthr.cpp
src/ros_thr.cpp
src/CreatDataBuffer.cpp
src/CreatBuffer.cpp
src/pid_controller.cpp
src/low_pass_filter.cpp
src/plot_publisher.cpp)
set(qt_hdrs
src/mainwindow.h
src/listnerthr.h
src/ros_thr.h
src/CreatDataBuffer.h
src/HelperFunctions.h
src/CreatBuffer.h
src/pid_controller.h
src/low_pass_filter.h
src/plot_publisher.h)
qt4_automoc(${qt_srcs})
QT4_WRAP_CPP(qt_moc_srcs ${qt_hdrs})
QT4_WRAP_UI(uis_h src/mainwindow.ui)
include_directories(${CMAKE_CURRENT_BINARY_DIR})
set(EXECUTABLE_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/bin)
set(LIBRARY_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/lib)
rosbuild_add_executable(qttest src/main.cpp
${uis_h} ${qt_srcs} ${qt_moc_srcs})
target_link_libraries(qttest ${QT_LIBRARIES}${Boost_LIBARAIES }${OpenCV_LIBARAIES})
any clues would be appreciated.
I got another problem after changing the cmakelists
*** No rule to make target '/usr/lib/x86_64-linux-gnu/libboost_system.so/usr/lib/x86_64-linux-gnu/libboost_system.so', needed by '../bin/qttest'. Stop. CMakeFiles/Makefile2:425: recipe for target 'CMakeFiles/qttest.dir/all' failed –
You should pass boost libraries to the target_link_libraries command. The smallest change to your file will be as follows:
target_link_libraries(qttest ${QT_LIBRARIES} ${LIBS})
But since you are using find_package for Boost and you do not actually use your LIBS variable anywhere, you should stick with something like this:
find_package(Boost COMPONENTS system REQUIRED)
...
target_link_libraries(qttest ${QT_LIBRARIES} ${OpenCV_LIBS} ${Boost_LIBRARIES})
And remove LIBS altogether.

Caffe build gives GCC Link Error "can not be used when making shared object.; recompile with -fPIC"

I'm trying to install caffe, using CMake, but when I run make all (after running cmake .. from a build directory) I get the following error:
me#gimli:~/Downloads/caffe/build$ make all
[ 1%] Built target caffeproto
[ 1%] Linking CXX shared library ../../lib/libcaffe.so
/usr/bin/x86_64-linux-gnu-ld: /usr/local/lib/libleveldb.a(db_impl.cc.o): relocation R_X86_64_PC32 against symbol `_ZN7leveldb10EnvWrapper8ScheduleEPFvPvES1_' can not be used when making a shared object; recompile with -fPIC
/usr/bin/x86_64-linux-gnu-ld: final link failed: Bad value
collect2: error: ld returned 1 exit status
src/caffe/CMakeFiles/caffe.dir/build.make:40060: recipe for target 'lib/libcaffe.so.1.0.0' failed
make[2]: *** [lib/libcaffe.so.1.0.0] Error 1
CMakeFiles/Makefile2:267: recipe for target 'src/caffe/CMakeFiles/caffe.dir/all' failed
make[1]: *** [src/caffe/CMakeFiles/caffe.dir/all] Error 2
Makefile:129: recipe for target 'all' failed
make: *** [all] Error 2
I don't really understand CMake, but gather that somewhere I'm supposed to add -fPIC to a gcc command. But, I have no idea where I should make this change, or if there's somewhere in Cmake where I should tell it to construct the gcc command correctly.
How can I force CMake to create/use a gcc command with the -fPIC option, or is there something else entirely I should be doing?
The error is not from CMake but from the linker. It actually tells, that:
You cannot build shared library libcaffe.so with PIC (Position independent code) feature and link it with the static library libleveldb.a compiled without this feature.
Possible solutions are:
Get shared version of the static library (libleveldb.a in your case), so it will be compiled with PIC. This is what the error message suggests you.
Instead of building shared library (Caffe in your case), build static one, without using of PIC. Note, that in this case your will face with similar issues when trying to use resulted library in the future shared libraries.
For most CMake projects forcing the building static libraries can be performed with:
cmake -DBUILD_SHARED_LIBS=OFF <other parameters>
Strictly speaking, PIC feature is independent from the type (shared or static) of a library. So you may have a static library with PIC, or build a shared library without it.
For many CMake projects you may control PIC feature of the created libraries with
cmake -DPOSITION_INDEPENDENT_CODE=<ON|OFF> <other parameter>

Adding ffmpeg to clion project

I would liket to add ffmpeg to Clion but I have some problems with it.
My MakeLists.txt looks liek this:
cmake_minimum_required(VERSION 3.10)
project(ffmpeg)
set(CMAKE_CXX_STANDARD 11)
include_directories(libs/ffmpeg/)
include_directories(libs/ffmpeg/include/libavutil/)
include_directories(libs/ffmpeg/include/libaccodec/)
include_directories(libs/ffmpeg/include/libavdevice/)
include_directories(libs/ffmpeg/include/libavfilter/)
include_directories(libs/ffmpeg/include/libavformat/)
include_directories(libs/ffmpeg/include/)
link_directories(libs/ffmpeg/lib/)
set(SOURCE_FILES main.cpp)
add_executable(ffmpeg main.cpp)
target_link_libraries(
ffmpeg
avcodec
avdevice
avfilter
avformat
avresample
avutil
postproc
swresample
swscale
)
I am not sure if I added the libraries and includes in the right way, beacause in my simple main.cpp it can't resolve avcodec_configuration().
My project layout looks as follows:
ffmpeg
-libs
-include
-libavcodec
.
.
.
-lib
-avcodec.lib
-main.cpp
EDIT:
Now all includes are found by the compiler. BUt if I compile I get following error:
c:/mingw/bin/../lib/gcc/mingw32/6.3.0/../../../../mingw32/bin/ld.exe: cannot find -lavresample
collect2.exe: error: ld returned 1 exit status
mingw32-make.exe[2]: *** [ffmpeg.exe] Error 1
mingw32-make.exe[1]: *** [CMakeFiles/ffmpeg.dir/all] Error 2
mingw32-make.exe: *** [all] Error 2
CMakeFiles\ffmpeg.dir\build.make:96: recipe for target 'ffmpeg.exe' failed
CMakeFiles\Makefile2:66: recipe for target 'CMakeFiles/ffmpeg.dir/all' failed
Makefile:82: recipe for target 'all' failed
I don't recommend to hardcode paths to the headers/libraries in your CMakeLists.txt; this is not portable.
Search for a ready FindFFmpeg.cmake (maybe this one will work?),
add it to your project directory (e.g. into a cmake/ subdirectory),
and connect it to CMAKE_MODULE_PATH. E.g. list(APPEND CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/cmake").
Then refer to ${FFMPEG_LIBRARIES}, ${FFMPEG_INCLUDE_DIRS} and ${FFMPEG_DEFINITIONS}.
Sometimes you have to fix up the FindXXX.cmake modules.
I was able to include the precompiled ffmpeg library to cmake project by creating find cmake scripts and including them in main CMakeLists.txt.
Full example is here : https://github.com/tomkordic/Java-native-HTTP/tree/master/src/main/cpp
In my project a FFMPEG_INCLUDE_DIRECTORY and FFMPEG_LIB_DIRECTORY are passed by gradle build script you can set these manually or discover them in some other way.
FFMPEG_INCLUDE_DIRECTORY is a path to ffmpeg installation include directory, in my case:
/mnt/7fab2260-fb19-41a7-ac7c-816bab2f3b92/install/ffmpeg_build/include
FFMPEG_LIB_DIRECTORY is a path to ffmpeg installation lib directory, in my case:
/mnt/7fab2260-fb19-41a7-ac7c-816bab2f3b92/install/ffmpeg_build/lib
Make sure to set CMAKE_MODULE_PATH to the directory where your find cmake scripts are located.
Build system is for ubuntu 18.04.

No rule to make target makefile when using an external library

I have attempted to troubleshoot this issue, but eventually I gave up and I cannot figure it out.
I am using CLion and I need to import an external shared library (ts3client.so). Whatever I do, it fails in one way or another.
Currently, I have the following:
cmake_minimum_required(VERSION 3.8)
project(TSMusicBot)
set(CMAKE_CXX_STANDARD 11)
set(SOURCE_FILES src/main.cpp)
# Teamspeak Libraries
INCLUDE_DIRECTORIES(libs/ts3_sdk_3.0.4/include)
ADD_LIBRARY (libts3client SHARED IMPORTED GLOBAL)
SET_PROPERTY (
TARGET libts3client PROPERTY IMPORTED_LOCATION
libs/ts3_sdk_3.0.4/bin/linux/amd64/libts3client.so)
add_executable(TSMusicBot ${SOURCE_FILES})
target_link_libraries(TSMusicBot libts3client)
This gives me the following error:
make[2]: *** No rule to make target 'libs/ts3_sdk_3.0.4/bin/linux/amd64/libts3client.so', needed by 'TSMusicBot'. Stop.
CMakeFiles/Makefile2:67: recipe for target 'CMakeFiles/TSMusicBot.dir/all' failed
make[1]: *** [CMakeFiles/TSMusicBot.dir/all] Error 2
Makefile:83: recipe for target 'all' failed
make: *** [all] Error 2
My directory structure is the following:
src/main.cpp (the code)
libs/ts3_sdk_3.0.4/include/teamspeak/ (which contains headers for the library)
libs/ts3_sdk_3.0.4/bin/linux/amd64/libts3client.so (which is the library I cannot import).
The problem is here:
SET_PROPERTY (
TARGET libts3client PROPERTY IMPORTED_LOCATION
libs/ts3_sdk_3.0.4/bin/linux/amd64/libts3client.so)
You have a plain relative path there, which often will cause problems with CMake because it will run your commands in different directories than the one where CMakeLists.txt exists.
As we discovered in a comment, changing the IMPORTED_LOCATION to an absolute path fixes it. However, the proper solution is to use a relative path with a known base:
SET_PROPERTY (
TARGET libts3client PROPERTY IMPORTED_LOCATION
${CMAKE_CURRENT_SOURCE_DIR}/libs/ts3_sdk_3.0.4/bin/linux/amd64/libts3client.so)
This says explicitly that the libs directory is under the directory where this CMakeLists.txt file is.

Resources