I recently changed to Linux Mint Debian Edition and cannot include boost to Clion using cmake. Yesterday I tried every option I could find, including on stackoverflow, but nothing has worked yet.
Boost has been installed using: sudo apt -y install libboost-filesystem-dev
This is a version I found in this thread: How to include external library (boost) into CLion C++ project with CMake?
which supposedly worked, but it didn't.
cmake_minimum_required(VERSION 3.22)
project(boost_project)
set(CMAKE_CXX_STANDARD 14)
find_package(Boost COMPONENTS system filesystem REQUIRED)
if(Boost_FOUND)
message(STATUS "Boost_INCLUDE_DIRS: ${Boost_INCLUDE_DIRS}")
message(STATUS "Boost_LIBRARIES: ${Boost_LIBRARIES}")
message(STATUS "Boost_VERSION: ${Boost_VERSION}")
include_directories(${Boost_INCLUDE_DIRS})
endif()
add_executable(boost_project main.cpp)
if(Boost_FOUND)
target_link_libraries(boost_project ${Boost_LIBRARIES})
endif()
The Cmake output message is:
CMake Error at /app/extra/clion/bin/cmake/linux/share/cmake-3.22/Modules/FindPackageHandleStandardArgs.cmake:230 (message):
Could NOT find Boost (missing: Boost_INCLUDE_DIR system filesystem)
I've gone through dozens of threads but so far nothing has worked. Has anyone suggestions of what's wrong?
EDIT:
So I've de-installed Clion and re-installed it with snap, since I read in another thread that it was an issue with installing Clion through the software center. Now I can include boost with no problem, but building it reveals the compiler still doesn't like using it.
#include <iostream>
#include "boost/asio.hpp"
boost::asio::io_service io_s;
int main() {
std::cout << "Hello, World!" << std::endl;
return 0;
}
: && /usr/bin/c++ -g CMakeFiles/BoostTest.dir/main.cpp.o -o BoostTest && :
/usr/bin/ld: CMakeFiles/BoostTest.dir/main.cpp.o: in function boost::asio::detail::posix_event::posix_event()': /usr/include/boost/asio/detail/impl/posix_event.ipp:42: undefined reference to pthread_condattr_setclock'
/usr/bin/ld: CMakeFiles/BoostTest.dir/main.cpp.o: in function boost::asio::detail::posix_thread::~posix_thread()': /usr/include/boost/asio/detail/impl/posix_thread.ipp:35: undefined reference to pthread_detach'
/usr/bin/ld: CMakeFiles/BoostTest.dir/main.cpp.o: in function boost::asio::detail::posix_thread::join()': /usr/include/boost/asio/detail/impl/posix_thread.ipp:42: undefined reference to pthread_join'
/usr/bin/ld: CMakeFiles/BoostTest.dir/main.cpp.o: in function boost::asio::detail::posix_thread::start_thread(boost::asio::detail::posix_thread::func_base*)': /usr/include/boost/asio/detail/impl/posix_thread.ipp:59: undefined reference to pthread_create'
/usr/bin/ld: CMakeFiles/BoostTest.dir/main.cpp.o: in function boost::asio::detail::posix_signal_blocker::posix_signal_blocker()': /usr/include/boost/asio/detail/posix_signal_blocker.hpp:43: undefined reference to pthread_sigmask'
/usr/bin/ld: CMakeFiles/BoostTest.dir/main.cpp.o: in function boost::asio::detail::posix_signal_blocker::~posix_signal_blocker()': /usr/include/boost/asio/detail/posix_signal_blocker.hpp:50: undefined reference to pthread_sigmask'
collect2: error: ld returned 1 exit status
ninja: build stopped: subcommand failed.
The solution to the first problem, cmake not finding boost, had nothing to do with cmake but Clion. Installing Clion with flatpak can apparently cause this error. To fix it I just needed to de-install Clion and re-install it with snap.
The second issue was cmake not finding several thread related includes. For that cmake needed
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -pthread")
to be added. I found this solution in these threads:
linking errors for boost for visual studio linux project
Undefined reference to pthread_create in Linux
The complete and working cmake looks like this now:
cmake_minimum_required(VERSION 3.22)
project(boost_project)
message(STATUS "start running cmake...")
find_package(Boost COMPONENTS system)
if(Boost_FOUND)
message(STATUS "Boost_INCLUDE_DIRS: ${Boost_INCLUDE_DIRS}")
message(STATUS "Boost_LIBRARIES: ${Boost_LIBRARIES}")
message(STATUS "Boost_VERSION: ${Boost_VERSION}")
set(CMAKE_CXX_STANDARD 14)
add_executable(boost_project main.cpp)
include_directories(${Boost_INCLUDE_DIR})
link_directories(${Boost_LIBRARY_DIR})
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -pthread")
endif()
target_link_libraries(boost_project ${Boost_LIBRARIES})
Related
I'm trying to use NTL library for ZZ class, and would like to use dedicated functions. Unfortunately during compilation I'm getting a lot of errors:
[100%] Linking CXX executable hpc5
CMakeFiles/hpc5.dir/main.cpp.o: In function `findX(NTL::ZZ, NTL::ZZ, NTL::ZZ)':
/home/rooter/CLionProjects/hpc5/main.cpp:44: undefined reference to `find_xi(NTL::ZZ, NTL::ZZ)'
/home/rooter/CLionProjects/hpc5/main.cpp:57: undefined reference to `chinese_remainder(NTL::ZZ*, NTL::ZZ*, NTL::ZZ)'
/home/rooter/CLionProjects/hpc5/main.cpp:58: undefined reference to `NTL::operator<<(std::ostream&, NTL::ZZ const&)'
CMakeFiles/hpc5.dir/main.cpp.o: In function `NTL::ZZ::ZZ(NTL::ZZ const&)':
/usr/include/NTL/ZZ.h:58: undefined reference to `_ntl_gcopy(void*, void**)'
CMakeFiles/hpc5.dir/main.cpp.o: In function `NTL::ZZ::operator=(NTL::ZZ const&)':
/usr/include/NTL/ZZ.h:73: undefined reference to `_ntl_gcopy(void*, void**)'
CMakeFiles/hpc5.dir/main.cpp.o: In function `NTL::ZZ::operator=(long)':
/usr/include/NTL/ZZ.h:75: undefined reference to `_ntl_gintoz(long, void**)'
I have installed libntl-dev on my linux mint, added set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -lntl" ) to my CMakeLists.txt and set CMake option -lntl and it has no effect. How can I link this library?
My CMakeLists.txt contains:
cmake_minimum_required(VERSION 3.10)
project(hpc5)
set(CMAKE_CXX_STANDARD 14)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -lntl" )
add_executable(hpc5 main.cpp)
If you want to link to a runtime library using CMake, you need to use target_link_libraries command. For example, you may change your CMakeLists.txt file as follows:
cmake_minimum_required(VERSION 3.10)
project(hpc5)
set(CMAKE_CXX_STANDARD 14)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall" )
add_executable(hpc5 main.cpp)
target_link_libraries(hpc5 ntl)
This is assuming CMake is able to find the NTL library in your system.
EDIT : Fix executable name typo.
I am trying to write a single CMakeLists.txt file for C++ compilation on Linux with G++ and on OSX with Clang.
I want to use the Target Library flags -Wl,--start-group and -Wl,--end-group with G++, but these give an error when linking with Clang: ld: unknown option: --start-group
The only results I can find choose to just delete these flags on Mac copies, but that doesn't allow easy project migration from linux to OSX. I tried to make these statements CMAKE conditions, but those are treated as literal libraries which are not found:
eg: $<IF($<NOT:APPLE>)> -Wl,--start-group $<ENDIF($<NOT:APPLE>)>
produces: c++: error: $<IF: No such file or directory
Is there a way to conditionally edit in the CMAKE TARGET_LINK_LIBRARIES field?
I'd try this:
if(${CMAKE_CXX_COMPILER_ID} STREQUAL "GNU")
set(START_GROUP "-Wl,--start-group")
set(END_GROUP "-Wl,--end-group")
else()
set(START_GROUP "")
set(END_GROUP "")
endif()
Then just replace those options with ${START_GROUP} and ${END_GROUP}, and they will only be used with the GNU compiler.
Don't switch based on platform if it's really the compiler that matters. Otherwise, Clang users on Linux won't be able to build your project.
(Sorry for bad English, I'm German)
Hello programmers,
I'm desperately trying to cross compile a program (using OpenCV) on my linux(fedora) x86_x64 laptop for a linux(raspian) Raspberry pi 3.
I'm following this tutorial (https://solderspot.wordpress.com/2016/02/04/cross-compiling-for-raspberry-pi-part-ii/) and a simple std::cout << "test\n"; program compiles and works fine with the following CMakeLists.txt:
cmake_minimum_required (VERSION 2.8)
project (CVS)
find_package(OpenCV REQUIRED)
SET(COMPILE_DEFINITIONS -Werror)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
add_definitions(-DHOST_ROBO_DIR=/home/username/Dropbox/Roboter)
include_directories(SYSTEM ${PIROOT}/opt/vc/include ${PIROOT}/opt/vc/include/interface/vcos/pthreads ${PIROOT}/opt/vc/include/interface/vmcs_host/linux )
link_directories( ${PIROOT}/opt/vc/lib )
add_executable (CVS ../source/main.cpp)
The Problem comes with OpenCV...
If I use the CMakeLists.txt from above it generates (for obvious reasons) many "undefined reference" errors eg.: "undefined reference to `cv::waitKey(int)'"
If I add "target_link_libraries( CVS ${OpenCV_LIBS} )" or "target_link_libraries(CVS -lopencv_core)" to the CMakeLists.txt make produces the following error:
/home/username/pidev/piroot/usr/local/lib/libopencv_core.so: undefined reference to `std::__throw_out_of_range_fmt(char const*, ...)#GLIBCXX_3.4.20'
If I replace "SET(TOOLROOT ${PITOOLS}/arm-bcm2708/gcc-linaro-arm-linux-gnueabihf-raspbian-x64)" in ~/pidev/pi-toolchain.cmake with "SET(TOOLROOT ${PITOOLS}/arm-bcm2708/arm-rpi-4.9.3-linux-gnueabihf)" I got the following error:
[username#MSI-Linux msi]$ cmake -DCMAKE_TOOLCHAIN_FILE=~/pidev/pi-toolchain.cmake .
-- The C compiler identification is GNU 4.9.3
-- The CXX compiler identification is GNU 4.9.3
-- Check for working C compiler: /home/username/pidev/pitools/arm-bcm2708/arm-rpi-4.9.3-linux-gnueabihf/bin/arm-linux-gnueabihf-gcc
-- Check for working C compiler: /home/username/pidev/pitools/arm-bcm2708/arm-rpi-4.9.3-linux-gnueabihf/bin/arm-linux-gnueabihf-gcc -- broken
CMake Error at /usr/share/cmake/Modules/CMakeTestCCompiler.cmake:61 (message):
The C compiler
"/home/username/pidev/pitools/arm-bcm2708/arm-rpi-4.9.3-linux-gnueabihf/bin/arm-linux-gnueabihf-gcc"
is not able to compile a simple test program.
It fails with the following output:
Change Dir: /home/username/Dropbox/Roboter/raspySync/CVS/cvs5-6/msi/CMakeFiles/CMakeTmp
Run Build Command:"/usr/bin/gmake" "cmTC_b1abe/fast"
/usr/bin/gmake -f CMakeFiles/cmTC_b1abe.dir/build.make
CMakeFiles/cmTC_b1abe.dir/build
gmake[1]: Verzeichnis
„/home/username/Dropbox/Roboter/raspySync/CVS/cvs5-6/msi/CMakeFiles/CMakeTmp“
wird betreten
Building C object CMakeFiles/cmTC_b1abe.dir/testCCompiler.c.o
/home/username/pidev/pitools/arm-bcm2708/arm-rpi-4.9.3-linux-gnueabihf/bin/arm-linux-gnueabihf-gcc
--sysroot=/home/username/pidev/piroot
-Wl,-rpath-link,/home/username/pidev/piroot/opt/vc/lib
-Wl,-rpath-link,/home/username/pidev/piroot/lib/arm-linux-gnueabihf
-Wl,-rpath-link,/home/username/pidev/piroot/usr/lib/arm-linux-gnueabihf
-Wl,-rpath-link,/home/username/pidev/piroot/usr/local/lib -o
CMakeFiles/cmTC_b1abe.dir/testCCompiler.c.o -c
/home/username/Dropbox/Roboter/raspySync/CVS/cvs5-6/msi/CMakeFiles/CMakeTmp/testCCompiler.c
Linking C executable cmTC_b1abe
/usr/bin/cmake -E cmake_link_script CMakeFiles/cmTC_b1abe.dir/link.txt
--verbose=1
/home/username/pidev/pitools/arm-bcm2708/arm-rpi-4.9.3-linux-gnueabihf/bin/arm-linux-gnueabihf-gcc
--sysroot=/home/username/pidev/piroot
-Wl,-rpath-link,/home/username/pidev/piroot/opt/vc/lib
-Wl,-rpath-link,/home/username/pidev/piroot/lib/arm-linux-gnueabihf
-Wl,-rpath-link,/home/username/pidev/piroot/usr/lib/arm-linux-gnueabihf
-Wl,-rpath-link,/home/username/pidev/piroot/usr/local/lib
CMakeFiles/cmTC_b1abe.dir/testCCompiler.c.o -o cmTC_b1abe -rdynamic
/home/username/pidev/pitools/arm-bcm2708/arm-rpi-4.9.3-linux-gnueabihf/bin/../lib/gcc/arm-linux-gnueabihf/4.9.3/../../../../arm-linux-gnueabihf/bin/ld:
cannot find crt1.o: No such file or directory
/home/username/pidev/pitools/arm-bcm2708/arm-rpi-4.9.3-linux-gnueabihf/bin/../lib/gcc/arm-linux-gnueabihf/4.9.3/../../../../arm-linux-gnueabihf/bin/ld:
cannot find crti.o: No such file or directory
collect2: error: ld returned 1 exit status
CMakeFiles/cmTC_b1abe.dir/build.make:97: die Regel für Ziel
„cmTC_b1abe“ scheiterte
gmake[1]: *** [cmTC_b1abe] Fehler 1
gmake[1]: Verzeichnis
„/home/username/Dropbox/Roboter/raspySync/CVS/cvs5-6/msi/CMakeFiles/CMakeTmp“
wird verlassen
Makefile:126: die Regel für Ziel „cmTC_b1abe/fast“ scheiterte
gmake: *** [cmTC_b1abe/fast] Fehler 2
CMake will not be able to correctly generate this project.
Call Stack (most recent call first):
CMakeLists.txt:2 (project)
-- Configuring incomplete, errors occurred!
See also "/home/username/Dropbox/Roboter/raspySync/CVS/cvs5-6/msi/CMakeFiles/CMakeOutput.log".
See also "/home/username/Dropbox/Roboter/raspySync/CVS/cvs5-6/msi/CMakeFiles/CMakeError.log".
[username#MSI-Linux msi]$
I would be thankful if anyone could answer one of these questions:
- How can I cross-compile a program that uses OpenCV commands
- Why is there an "undefined reference to `std::__throw_out_of_range_fmt(char const*, ...)#GLIBCXX_3.4.20'" and a "The C compiler ... is not able to compile a simple test program."
- Would it be bad or REALLY bad style if I add a something like this in the program:
namespace std
{
void __throw_out_of_range_fmt(char const*, ...)
{
std::cout << "throw out of range error\n";
}
}
Heres my ~/pidev/pi-toolchain.cmake:
SET(CMAKE_SYSTEM_NAME Linux)
SET(CMAKE_SYSTEM_VERSION 1)
SET(DEVROOT $ENV{HOME}/pidev)
SET(PIROOT ${DEVROOT}/piroot)
SET(PITOOLS ${DEVROOT}/pitools)
SET(TOOLROOT ${PITOOLS}/arm-bcm2708/gcc-linaro-arm-linux-gnueabihf-raspbian-x64)
# specify the cross compiler
SET(CMAKE_C_COMPILER ${TOOLROOT}/bin/arm-linux-gnueabihf-gcc)
SET(CMAKE_CXX_COMPILER ${TOOLROOT}/bin/arm-linux-gnueabihf-g++)
SET(CMAKE_SYSROOT ${PIROOT})
SET(CMAKE_FIND_ROOT_PATH ${PIROOT})
# search for programs in the build host directories
SET(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)
# for libraries and headers in the target directories
SET(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
SET(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)
SET(FLAGS "-Wl,-rpath-link,${PIROOT}/opt/vc/lib -Wl,-rpath-link,${PIROOT}/lib/arm-linux-gnueabihf -Wl,-rpath-link,${PIROOT}/usr/lib/arm-linux-gnueabihf -Wl,-rpath-link,${PIROOT}/usr/local/lib")
UNSET(CMAKE_C_FLAGS CACHE)
UNSET(CMAKE_CXX_FLAGS CACHE)
SET(CMAKE_CXX_FLAGS ${FLAGS} CACHE STRING "" FORCE)
SET(CMAKE_C_FLAGS ${FLAGS} CACHE STRING "" FORCE)
The "cannot find crt1.o: No such file or directory"-error is a known problem with the "arm-rpi-4.9.3-linux-gnueabihf"-toolchain, see 4.9 toolchain issue #50.
The "`std::__throw_out_of_range_fmt(char const*, ...)#GLIBCXX_3.4.20'"-error is related to gcc 4.8, see std::__throw_out_of_range_fmt(char const*, …)#GLIBCXX_3.4.20'. I'm not quite sure why this happens to you, since I was able to build opencv with the "gcc-linaro-arm-linux-gnueabihf-raspbian-x64"-toolchain.
Maybe you have some luck with the way I did it: How do I cross compile OpenCV for the Raspberry Pi with additional modules (highgui…)
Example CMakeLists.txt
project(Test)
cmake_minimum_required(VERSION 3.2)
set(CMAKE_CXX_STANDARD 11)
aux_source_directory(. SRC_LIST)
find_package(OpenCV REQUIRED)
add_executable(${PROJECT_NAME} ${SRC_LIST})
target_link_libraries(${PROJECT_NAME} ${OpenCV_LIBS})
Invoke cmake like this:
cmake -D CMAKE_TOOLCHAIN_FILE=$CMAKE_TOOLCHAIN_FILE -D PIROOT=$MOUNT_DIR -D TOOLROOT=$TOOLCHAIN ..
I want to build a project using make in Ubuntu. This project includes VTK, Xerces and Cmake libraries. While builing I get the following error:
Linking CXX static library libMA_LaTIM.a
[ 96%] Built target MA_LaTIM
Scanning dependencies of target MA_LaTIM_entrainement
[100%] Building CXX object applications/CMakeFiles/MA_LaTIM_entrainement.dir/entrainement.cxx.o
Linking CXX executable MA_LaTIM_entrainement
/usr/bin/ld: ../algorithmes/libMA_LaTIM.a(LecteurImage.cxx.o): undefined reference to symbol 'vtkImageAlgorithm::GetOutput()'
/usr/bin/ld: note: 'vtkImageAlgorithm::GetOutput()' is defined in DSO /usr/lib/libvtkFiltering.so.5.8 so try adding it to the linker command line
/usr/lib/libvtkFiltering.so.5.8: could not read symbols: Invalid operation
collect2: ld returned 1 exit status
How can I fix this problem? Here is my CMakeLists.txt, and here is my Makefile.
If you happen to not use cmake to build your application, here is the solution to resolve the linker error undefined reference to symbol with VTK!
The libraries have to be chosen manually. The problem is, that e.g. with my version VTK 6.1 there are 377 shared libs to choose from. Well, some are as before with version 5 but split in Core and special libs.
Others can be found if you check the header of the module the compiler complains about: there is a macro between "class" and the class name: "class VTK_......_EXPORT" , which gives you a hint how the lib is named.
E.g. class VTKRENDERINGCORE_EXPORT vtkRenderer needs library libvtkRenderingCore. Here an example (Linux) which worked for my application:
-L/usr/lib64/vtk -lvtkCommonCore -lvtkFiltersCore -lvtkRenderingCore -lvtkImagingCore -lvtkCommonExecutionModel -l vtkCommonDataModel \
-lvtkFiltersSources -lvtkFiltersGeometry -lvtkFiltersGeneral -lvtkIOCore -lvtkIOImage -lvtkRenderingOpenGL
Following the instructions here I have built a Fortran enabled NDK toolchain (OSX, NDK-7b) with the goal of building LAPACK/BLAS.
Using android-cmake with the 3.4.0 net lib source it seems that I'm nearly successful. However, the BLAS build fails when linking one of the tests (with an error stating unresolved sincos and sincosf). A little searching reveals that these functions are not available in legacy Android versions. I'm wondering what is the best way to resolve these functions?
Below is and example of a linking error:
cd /Users/marc/software/lapack-3.4.0/Android/BLAS/TESTING && /opt/local/bin/cmake -E cmake_link_script CMakeFiles/xblat2c.dir/link.txt --verbose=1
/opt/local/share/java/android-ndk-macosx/toolchains/arm-linux-androideabi-4.7.0/prebuilt/darwin-x86/bin/arm-linux-androideabi-gfortran -Wl,--gc-sections -Wl,-z,nocopyreloc -Wl,--fix-cortex-a8 -Wl,--no-undefined -lstdc++ -lsupc++ CMakeFiles/xblat2c.dir/cblat2.f.o -o ../../bin/xblat2c -rdynamic -L/Users/marc/software/lapack-3.4.0/Android/systemlibs/armeabi-v7a -L/opt/local/share/java/android-ndk-macosx/toolchains/arm-linux-androideabi-4.7.0/prebuilt/darwin-x86/user/libs/armeabi-v7a ../../lib/libblas.a -lm -Wl,-rpath,/Users/marc/software/lapack-3.4.0/Android/systemlibs/armeabi-v7a:/opt/local/share/java/android-ndk-macosx/toolchains/arm-linux-androideabi-4.7.0/prebuilt/darwin-x86/user/libs/armeabi-v7a
/opt/local/share/java/android-ndk-macosx/toolchains/arm-linux-androideabi-
4.7.0/prebuilt/darwin-x86/lib/gcc/arm-linux-androideabi/4.7.0/../../../../arm-linux-androideabi/lib/libgfortran.a(c99_functions.o): In function cexpf':
/opt/local/share/java/android-ndk-macosx/src/build/../gcc/gcc-4.7.0/libgfortran/intrinsics/c99_functions.c:910: undefined reference tosincosf'
GCC needs to know at compile time whether sincos is available or not. It does so based on the target. In case of the target triplet arm-linux-androideabi, it looks at gcc/config/linux.h and finds there:
/* Whether we have sincos that follows the GNU extension. */
#undef TARGET_HAS_SINCOS
#define TARGET_HAS_SINCOS (OPTION_GLIBC || OPTION_BIONIC)
The reason for the inclusion of Bionic is that Android 2.3 added support for sincosf/sincos/sincosl [1]. Thus, you can either update Bionic or you patch GCC to assume that no sincos is available; cf. also [2].
[1] http://source-android.frandroid.com/bionic/libc/docs/CHANGES.TXT
[2] https://bugs.launchpad.net/linaro-android/+bug/908125