Why is CMake not linking pthread in this CMakeList.txt? - c++

I get the error "undefined reference to 'pthread_create'" when running make on a program that uses pthread library. It works when I build it directly with g++:
g++ -std=c++11 -pthread pthread_Mutex.c stopwatch.o -o pthread_Mutex
But not with CMake.
I've examined a few good examples, including here: cmake and libpthread
I've tried thehouse's examples for 3.1+ and 2.8+ versions of CMake. I'm running 3.5.1 on Ubuntu. Here is my CMakeList.txt.
cmake_minimum_required(VERSION 3.5)
project(pthread_Mutex)
set (CMAKE_CXX_STANDARD 11)
set(THREADS_PREFER_PTHREAD_FLAG ON)
find_package(Threads REQUIRED)
include_directories(include)
file(GLOB SOURCES "src/*.cpp")
add_executable(stopwatch ${SOURCES})
add_executable(pthread_Mutex ${SOURCES})
# I've tried this one by itself
#target_link_libraries(pthread_Mutex Threads::Threads)
if(THREADS_HAVE_PTHREAD_ARG)
target_compile_options(pthread_Mutex PUBLIC "-pthread")
endif()
if(CMAKE_THREAD_LIBS_INIT)
target_link_libraries(pthread_Mutex "${CMAKE_THREAD_LIBS_INIT}")
endif()
target_link_libraries(pthread_Mutex pthread)
Here is my directory structure:
├── build
├── CMakeLists.txt
├── include
│   └── stopwatch.h
├── src
   ├── pthread_Mutex.cpp
    └── stopwatch.cpp
pthread_Mutex.cpp is my main program. stopwatch.cpp is a library that does not use pthreads.
But I get the following errors when running cmake VERBOSE=1:
/usr/bin/cmake -H/home/ben/workspaces/c++/thread_experiments -B/home/ben/workspaces/c++/thread_experiments/build --check-build-system CMakeFiles/Makefile.cmake 0
/usr/bin/cmake -E cmake_progress_start /home/ben/workspaces/c++/thread_experiments/build/CMakeFiles /home/ben/workspaces/c++/thread_experiments/build/CMakeFiles/progress.marks
make -f CMakeFiles/Makefile2 all
make[1]: Entering directory '/home/ben/workspaces/c++/thread_experiments/build'
make -f CMakeFiles/pthread_Mutex.dir/build.make CMakeFiles/pthread_Mutex.dir/depend
make[2]: Entering directory '/home/ben/workspaces/c++/thread_experiments/build'
cd /home/ben/workspaces/c++/thread_experiments/build && /usr/bin/cmake -E cmake_depends "Unix Makefiles" /home/ben/workspaces/c++/thread_experiments /home/ben/workspaces/c++/thread_experiments /home/ben/workspaces/c++/thread_experiments/build /home/ben/workspaces/c++/thread_experiments/build /home/ben/workspaces/c++/thread_experiments/build/CMakeFiles/pthread_Mutex.dir/DependInfo.cmake --color=
make[2]: Leaving directory '/home/ben/workspaces/c++/thread_experiments/build'
make -f CMakeFiles/pthread_Mutex.dir/build.make CMakeFiles/pthread_Mutex.dir/build
make[2]: Entering directory '/home/ben/workspaces/c++/thread_experiments/build'
make[2]: Nothing to be done for 'CMakeFiles/pthread_Mutex.dir/build'.
make[2]: Leaving directory '/home/ben/workspaces/c++/thread_experiments/build'
[ 50%] Built target pthread_Mutex
make -f CMakeFiles/stopwatch.dir/build.make CMakeFiles/stopwatch.dir/depend
make[2]: Entering directory '/home/ben/workspaces/c++/thread_experiments/build'
cd /home/ben/workspaces/c++/thread_experiments/build && /usr/bin/cmake -E cmake_depends "Unix Makefiles" /home/ben/workspaces/c++/thread_experiments /home/ben/workspaces/c++/thread_experiments /home/ben/workspaces/c++/thread_experiments/build /home/ben/workspaces/c++/thread_experiments/build /home/ben/workspaces/c++/thread_experiments/build/CMakeFiles/stopwatch.dir/DependInfo.cmake --color=
make[2]: Leaving directory '/home/ben/workspaces/c++/thread_experiments/build'
make -f CMakeFiles/stopwatch.dir/build.make CMakeFiles/stopwatch.dir/build
make[2]: Entering directory '/home/ben/workspaces/c++/thread_experiments/build'
[ 66%] Linking CXX executable stopwatch
/usr/bin/cmake -E cmake_link_script CMakeFiles/stopwatch.dir/link.txt --verbose=1
/usr/bin/c++ CMakeFiles/stopwatch.dir/src/pthread_Mutex.cpp.o CMakeFiles/stopwatch.dir/src/stopwatch.cpp.o -o stopwatch
CMakeFiles/stopwatch.dir/src/pthread_Mutex.cpp.o: In function `main':
pthread_Mutex.cpp:(.text+0x15a): undefined reference to `pthread_attr_getstacksize'
pthread_Mutex.cpp:(.text+0x19f): undefined reference to `pthread_attr_setstacksize'
pthread_Mutex.cpp:(.text+0x260): undefined reference to `pthread_create'
pthread_Mutex.cpp:(.text+0x29d): undefined reference to `pthread_join'
collect2: error: ld returned 1 exit status
CMakeFiles/stopwatch.dir/build.make:120: recipe for target 'stopwatch' failed
make[2]: *** [stopwatch] Error 1
make[2]: Leaving directory '/home/ben/workspaces/c++/thread_experiments/build'
CMakeFiles/Makefile2:104: recipe for target 'CMakeFiles/stopwatch.dir/all' failed
make[1]: *** [CMakeFiles/stopwatch.dir/all] Error 2
make[1]: Leaving directory '/home/ben/workspaces/c++/thread_experiments/build'
Makefile:83: recipe for target 'all' failed
make: *** [all] Error 2
So I must have something wrong in the CMakeList.txt file, but I'm running out of ideas to try.
Thanks

You have used target_compile_options, that's great for the compilation flags, but you forgot to do the same for the link options. Add the same line with also target_link_options.

target_compile_options adds the flag for the compilation step. You also need to link to the pthread lib -
target_link_libraries(pthread_Mutex pthread)

Related

Cmake cross-compiling error (missing -lm -lc and crt0.o)

I wrote a simple helloworld program in C++ (main.cpp, print.cpp, print.h) and I wanted to cross compile it to ARM using CMake. But I got an error about missing files. Below is the output of make:
/usr/bin/cmake -H/home/eslam/eslamlearning/learning/CPPtest -B/home/eslam/eslamlearning/learning/CPPtest --check-build-system CMakeFiles/Makefile.cmake 0
/usr/bin/cmake -E cmake_progress_start /home/eslam/eslamlearning/learning/CPPtest/CMakeFiles /home/eslam/eslamlearning/learning/CPPtest/CMakeFiles/progress.marks
make -f CMakeFiles/Makefile2 all
make[1]: Entering directory '/home/eslam/eslamlearning/learning/CPPtest'
make -f CMakeFiles/LibsModule.dir/build.make CMakeFiles/LibsModule.dir/depend
make[2]: Entering directory '/home/eslam/eslamlearning/learning/CPPtest'
cd /home/eslam/eslamlearning/learning/CPPtest && /usr/bin/cmake -E cmake_depends "Unix Makefiles" /home/eslam/eslamlearning/learning/CPPtest /home/eslam/eslamlearning/learning/CPPtest /home/eslam/eslamlearning/learning/CPPtest /home/eslam/eslamlearning/learning/CPPtest /home/eslam/eslamlearning/learning/CPPtest/CMakeFiles/LibsModule.dir/DependInfo.cmake --color=
make[2]: Leaving directory '/home/eslam/eslamlearning/learning/CPPtest'
make -f CMakeFiles/LibsModule.dir/build.make CMakeFiles/LibsModule.dir/build
make[2]: Entering directory '/home/eslam/eslamlearning/learning/CPPtest'
[ 25%] Linking CXX static library libLibsModule.a
/usr/bin/cmake -P CMakeFiles/LibsModule.dir/cmake_clean_target.cmake
/usr/bin/cmake -E cmake_link_script CMakeFiles/LibsModule.dir/link.txt --verbose=1
/home/eslam/eslamlearning/gcc-arm-eabi-toolchains/bin/arm-eabi-ar qc libLibsModule.a CMakeFiles/LibsModule.dir/print.cpp.o
/usr/bin/ranlib libLibsModule.a
make[2]: Leaving directory '/home/eslam/eslamlearning/learning/CPPtest'
[ 50%] Built target LibsModule
make -f CMakeFiles/StaticTest.dir/build.make CMakeFiles/StaticTest.dir/depend
make[2]: Entering directory '/home/eslam/eslamlearning/learning/CPPtest'
cd /home/eslam/eslamlearning/learning/CPPtest && /usr/bin/cmake -E cmake_depends "Unix Makefiles" /home/eslam/eslamlearning/learning/CPPtest /home/eslam/eslamlearning/learning/CPPtest /home/eslam/eslamlearning/learning/CPPtest /home/eslam/eslamlearning/learning/CPPtest /home/eslam/eslamlearning/learning/CPPtest/CMakeFiles/StaticTest.dir/DependInfo.cmake --color=
make[2]: Leaving directory '/home/eslam/eslamlearning/learning/CPPtest'
make -f CMakeFiles/StaticTest.dir/build.make CMakeFiles/StaticTest.dir/build
make[2]: Entering directory '/home/eslam/eslamlearning/learning/CPPtest'
[ 75%] Linking CXX executable StaticTest
/usr/bin/cmake -E cmake_link_script CMakeFiles/StaticTest.dir/link.txt --verbose=1
/home/eslam/eslamlearning/gcc-arm-eabi-toolchains/bin/arm-eabi-g++ --sysroot=/home/eslam/ CMakeFiles/StaticTest.dir/main.cpp.o -o StaticTest libLibsModule.a
/home/eslam/eslamlearning/gcc-arm-eabi-toolchains/bin/../lib/gcc/arm-eabi/7.3.1/../../../../arm-eabi/bin/ld: cannot find crt0.o: No such file or directory
/home/eslam/eslamlearning/gcc-arm-eabi-toolchains/bin/../lib/gcc/arm-eabi/7.3.1/../../../../arm-eabi/bin/ld: cannot find -lm
/home/eslam/eslamlearning/gcc-arm-eabi-toolchains/bin/../lib/gcc/arm-eabi/7.3.1/../../../../arm-eabi/bin/ld: cannot find -lc
collect2: error: ld returned 1 exit status
CMakeFiles/StaticTest.dir/build.make:98: recipe for target 'StaticTest' failed
make[2]: *** [StaticTest] Error 1
make[2]: Leaving directory '/home/eslam/eslamlearning/learning/CPPtest'
CMakeFiles/Makefile2:107: recipe for target 'CMakeFiles/StaticTest.dir/all' failed
make[1]: *** [CMakeFiles/StaticTest.dir/all] Error 2
make[1]: Leaving directory '/home/eslam/eslamlearning/learning/CPPtest'
Makefile:86: recipe for target 'all' failed
make: *** [all] Error 2
Here is my CMakelists.txt
cmake_minimum_required(VERSION 3.10.2)
project (StaticTest)
enable_language(CXX)
SET(CMAKE_SYSTEM_NAME Generic)
set(CMAKE_SYSTEM_PROCESSOR arm)
SET(CMAKE_C_COMPILER /home/eslam/eslamlearning/gcc-arm-eabi-toolchains/bin/arm-eabi-gcc)
SET(CMAKE_CXX_COMPILER /home/eslam/eslamlearning/gcc-arm-eabi-toolchains/bin/arm-eabi-g++)
set(CMAKE_AR /home/eslam/eslamlearning/gcc-arm-eabi-toolchains/bin/arm-eabi-ar)
set(CMAKE_FIND_ROOT_PATH /home/eslam/eslamlearning/gcc-arm-eabi-toolchains)
set(CMAKE_SYSROOT /home/eslam/)
set(CMAKE_STAGING_PREFIX /home/eslam/eslamlearning/learning/CPPtest)
set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM BOTH)
set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY BOTH)
set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE BOTH)
set(CMAKE_FIND_ROOT_PATH_MODE_PACKAGE BOTH)
ADD_LIBRARY(LibsModule STATIC
print.cpp
)
SET( APP_EXE StaticTest )
add_executable(${APP_EXE} main.cpp)
TARGET_LINK_LIBRARIES(LibsModule )
TARGET_LINK_LIBRARIES( ${APP_EXE}
LibsModule )
What am I missing? BTW, I'm not sure if my SYSROOT path is correct or not.
One last question: I was reading https://gitlab.kitware.com/cmake/community/wikis/doc/cmake/CrossCompiling#the-toolchain-file, what is a target environment?

CMake linking error (collect2: ld returned 1 exit status linking to current directory .)

************** EDIT-2 **************
Based on the comments, I have edited my CMakeLists file by getting rid of the automatically set variables and now it works and I am able to compile, build, and execute all three projects (both from the root directory and the individual directory). I have updated the CMakeLists files in this post in case somebody else might find it useful. Thanks for your help.
************** Original Question **************
My problem is similar to the one answered here, but that solution did not help me. I am having problems linking a project in Cmake. My directory structure is as follows:
├── CMakeLists.txt
├── build
├── armadillo_example
│ ├── include
│ ├── example.h
│ ├── src
│ ├── example.cpp
│ └── CMakeLists.txt
├── asio_opencv
│ ├── include
│ ├── asio_opencv.h
│ ├── src
│ ├── asio_opencv.cpp
│ └── CMakeLists.txt
├── boost_a-star_cities
│ ├── include
│ ├── astar-cities.h
│ ├── astar_goal_visitor.hpp
│ ├── city_writer.hpp
│ ├── distance_heuristic.hpp
│ ├── time_writer.hpp
│ ├── src
│ ├── astar_cities.cpp
│ └── CMakeLists.txt
./CMakeLists.txt
cmake_minimum_required ( VERSION 2.8 )
project ( yanack )
set ( CMAKE_BINARY_DIR ${CMAKE_SOURCE_DIR}/build )
set ( PROJECT_INCLUDE_DIR ${PROJECT_SOURCE_DIR}/include )
add_subdirectory ( armadillo_example )
add_subdirectory ( boost_a-star_cities )
add_subdirectory ( asio_opencv )
./armadillo_example/CMakeLists.txt
cmake_minimum_required ( VERSION 2.8 )
project ( armadillo_example )
set ( CMAKE_CXX_FLAGS "-std=c++11" )
set ( PROJECT_INCLUDE_DIR ${PROJECT_SOURCE_DIR}/include )
find_package ( Armadillo REQUIRED )
include_directories ( ${ARMADILLO_INCLUDE_DIRS} )
file ( GLOB_RECURSE PROJ_SRCS src/*.cpp )
include_directories ( "${PROJECT_INCLUDE_DIR}" )
add_executable ( example ${PROJ_SRCS} )
target_link_libraries ( example ${ARMADILLO_LIBRARIES} )
# for compiling into shared lib
#add_library ( ${PROJECT_NAME} SHARED ${PROJECT1_SRCS} )
./asio_opencv/CMakeLists.txt
cmake_minimum_required ( VERSION 2.8 )
project ( asio_opencv )
set ( CMAKE_CXX_FLAGS "-std=c++11" )
set ( PROJECT_INCLUDE_DIR ${PROJECT_SOURCE_DIR}/include )
find_package ( Boost COMPONENTS system REQUIRED )
find_package ( OpenCV REQUIRED )
include_directories ( ${OpenCV_INCLUDE_DIR} ${Boost_INCLUDE_DIR} )
file ( GLOB_RECURSE PROJ_SRCS src/*.cpp )
include_directories ( "${PROJECT_INCLUDE_DIR}" )
add_executable ( asio_opencv ${PROJ_SRCS} )
target_link_libraries ( asio_opencv ${OpenCV_LIBS} ${Boost_LIBRARIES} )
./boost_a-star_cities/CMakeLists.txt
cmake_minimum_required ( VERSION 2.8 )
project ( boost_a-star_cities )
set ( CMAKE_CXX_FLAGS "-std=c++11" )
set ( PROJECT_INCLUDE_DIR ${PROJECT_SOURCE_DIR}/include )
find_package ( Boost COMPONENTS graph REQUIRED )
include_directories ( ${Boost_INCLUDE_DIR} )
file ( GLOB_RECURSE PROJ_SRCS src/*.cpp )
include_directories ( "${PROJECT_INCLUDE_DIR}" )
add_executable ( astar-cities ${PROJ_SRCS} )
target_link_libraries ( astar-cities ${Boost_LIBRARIES} )
Each of the projects have their own main functions (they will eventually be converted to libraries linked to one file containing main), and I am able to build each one individually in their own build directories. I followed the tutorial to set up this CMake structure here. When I set it up I only had armadillo_example and boost_a-star_cities and I was able to build both and get executables for both of them in the root build directory. Today I added asio_opencv (which again can be built and executed in its own build directory) but I am getting the following error:
mkdir build
cd build
cmake ..
make
Scanning dependencies of target example
[ 33%] Building CXX object armadillo_example/CMakeFiles/example.dir/src/example.cpp.o
Linking CXX executable ../example
[ 33%] Built target example
Scanning dependencies of target astar-cities
[ 66%] Building CXX object boost_a-star_cities/CMakeFiles/astar-cities.dir/src/astar-cities.cpp.o
Linking CXX executable ../astar-cities
[ 66%] Built target astar-cities
Scanning dependencies of target asio_opencv
[100%] Building CXX object asio_opencv/CMakeFiles/asio_opencv.dir/src/asio_opencv.cpp.o
Linking CXX executable .
/usr/bin/ld: cannot open output file .: Is a directory
collect2: error: ld returned 1 exit status
make[2]: *** [asio_opencv] Error 1
make[1]: *** [asio_opencv/CMakeFiles/asio_opencv.dir/all] Error 2
make: *** [all] Error 2
ls
armadillo_example asio_opencv astar-cities boost_a-star_cities CMakeCache.txt CMakeFiles cmake_install.cmake example Makefile
As you can see it built the first two projects and has their executables but for the asio_opencv project , it builds it but seems to link it against the current directory (.). I'm not sure what is going wrong especially since the first two projects built and ran without any problems.
************** EDIT-1 **************
Here is the output after running make VERBOSE=1. The first one is running it from the root build directory (the one which fails). The second one is running it from asio_opencv build directory (the one which succeeds).
root build directory (fail):
make VERBOSE=1
/usr/bin/cmake -H/home/user/project-yanack -B/home/user/project-yanack/build --check-build-system CMakeFiles/Makefile.cmake 0
/usr/bin/cmake -E cmake_progress_start /home/user/project-yanack/build/CMakeFiles /home/user/project-yanack/build/CMakeFiles/progress.marks
make -f CMakeFiles/Makefile2 all
make[1]: Entering directory `/home/user/project-yanack/build'
make -f asio_opencv/CMakeFiles/asio_opencv.dir/build.make asio_opencv/CMakeFiles/asio_opencv.dir/depend
make[2]: Entering directory `/home/user/project-yanack/build'
cd /home/user/project-yanack/build && /usr/bin/cmake -E cmake_depends "Unix Makefiles" /home/user/project-yanack /home/user/project-yanack/asio_opencv /home/user/project-yanack/build /home/user/project-yanack/build/asio_opencv /home/user/project-yanack/build/asio_opencv/CMakeFiles/asio_opencv.dir/DependInfo.cmake --color=
Dependee "/home/user/project-yanack/build/asio_opencv/CMakeFiles/asio_opencv.dir/DependInfo.cmake" is newer than depender "/home/user/project-yanack/build/asio_opencv/CMakeFiles/asio_opencv.dir/depend.internal".
Dependee "/home/user/project-yanack/build/asio_opencv/CMakeFiles/CMakeDirectoryInformation.cmake" is newer than depender "/home/user/project-yanack/build/asio_opencv/CMakeFiles/asio_opencv.dir/depend.internal".
Scanning dependencies of target asio_opencv
make[2]: Leaving directory `/home/user/project-yanack/build'
make -f asio_opencv/CMakeFiles/asio_opencv.dir/build.make asio_opencv/CMakeFiles/asio_opencv.dir/build
make[2]: Entering directory `/home/user/project-yanack/build'
/usr/bin/cmake -E cmake_progress_report /home/user/project-yanack/build/CMakeFiles 1
[100%] Building CXX object asio_opencv/CMakeFiles/asio_opencv.dir/src/asio_opencv.cpp.o
cd /home/user/project-yanack/build/asio_opencv && /usr/bin/c++ -std=c++11 -I/home/user/project-yanack/include -I/home/user/project-yanack -I/usr/include/opencv -I/home/user/project-yanack/build/asio_opencv -I/home/user/project-yanack/asio_opencv/include -o CMakeFiles/asio_opencv.dir/src/asio_opencv.cpp.o -c /home/user/project-yanack/asio_opencv/src/asio_opencv.cpp
Linking CXX executable .
cd /home/user/project-yanack/build/asio_opencv && /usr/bin/cmake -E cmake_link_script CMakeFiles/asio_opencv.dir/link.txt --verbose=1
/usr/bin/c++ -std=c++11 CMakeFiles/asio_opencv.dir/src/asio_opencv.cpp.o -o . -rdynamic /usr/lib/x86_64-linux-gnu/libopencv_videostab.so.2.4.8 /usr/lib/x86_64-linux-gnu/libopencv_video.so.2.4.8 /usr/lib/x86_64-linux-gnu/libopencv_ts.so.2.4.8 /usr/lib/x86_64-linux-gnu/libopencv_superres.so.2.4.8 /usr/lib/x86_64-linux-gnu/libopencv_stitching.so.2.4.8 /usr/lib/x86_64-linux-gnu/libopencv_photo.so.2.4.8 /usr/lib/x86_64-linux-gnu/libopencv_ocl.so.2.4.8 /usr/lib/x86_64-linux-gnu/libopencv_objdetect.so.2.4.8 /usr/lib/x86_64-linux-gnu/libopencv_ml.so.2.4.8 /usr/lib/x86_64-linux-gnu/libopencv_legacy.so.2.4.8 /usr/lib/x86_64-linux-gnu/libopencv_imgproc.so.2.4.8 /usr/lib/x86_64-linux-gnu/libopencv_highgui.so.2.4.8 /usr/lib/x86_64-linux-gnu/libopencv_gpu.so.2.4.8 /usr/lib/x86_64-linux-gnu/libopencv_flann.so.2.4.8 /usr/lib/x86_64-linux-gnu/libopencv_features2d.so.2.4.8 /usr/lib/x86_64-linux-gnu/libopencv_core.so.2.4.8 /usr/lib/x86_64-linux-gnu/libopencv_contrib.so.2.4.8 /usr/lib/x86_64-linux-gnu/libopencv_calib3d.so.2.4.8 -lboost_system /usr/lib/x86_64-linux-gnu/libopencv_photo.so.2.4.8 /usr/lib/x86_64-linux-gnu/libopencv_legacy.so.2.4.8 /usr/lib/x86_64-linux-gnu/libopencv_video.so.2.4.8 /usr/lib/x86_64-linux-gnu/libopencv_objdetect.so.2.4.8 /usr/lib/x86_64-linux-gnu/libopencv_ml.so.2.4.8 /usr/lib/x86_64-linux-gnu/libopencv_calib3d.so.2.4.8 /usr/lib/x86_64-linux-gnu/libopencv_features2d.so.2.4.8 /usr/lib/x86_64-linux-gnu/libopencv_highgui.so.2.4.8 /usr/lib/x86_64-linux-gnu/libopencv_imgproc.so.2.4.8 /usr/lib/x86_64-linux-gnu/libopencv_flann.so.2.4.8 /usr/lib/x86_64-linux-gnu/libopencv_core.so.2.4.8
make[2]: Leaving directory `/home/user/project-yanack/build'
make[1]: Leaving directory `/home/user/project-yanack/build'
/usr/bin/ld: cannot open output file .: Is a directory
collect2: error: ld returned 1 exit status
make[2]: *** [asio_opencv] Error 1
make[1]: *** [asio_opencv/CMakeFiles/asio_opencv.dir/all] Error 2
make: *** [all] Error 2
asio_opencv build directory (success):
make VERBOSE=1
/usr/bin/cmake -H/home/user/project-yanack/asio_opencv -B/home/user/project-yanack/asio_opencv/build --check-build-system CMakeFiles/Makefile.cmake 0
/usr/bin/cmake -E cmake_progress_start /home/user/project-yanack/asio_opencv/build/CMakeFiles /home/user/project-yanack/asio_opencv/build/CMakeFiles/progress.marks
make -f CMakeFiles/Makefile2 all
make[1]: Entering directory `/home/user/project-yanack/asio_opencv/build'
make -f CMakeFiles/asio_opencv.dir/build.make CMakeFiles/asio_opencv.dir/depend
make[2]: Entering directory `/home/user/project-yanack/asio_opencv/build'
cd /home/user/project-yanack/asio_opencv/build && /usr/bin/cmake -E cmake_depends "Unix Makefiles" /home/user/project-yanack/asio_opencv /home/user/project-yanack/asio_opencv /home/user/project-yanack/asio_opencv/build /home/user/project-yanack/asio_opencv/build /home/user/project-yanack/asio_opencv/build/CMakeFiles/asio_opencv.dir/DependInfo.cmake --color=
Dependee "/home/user/project-yanack/asio_opencv/build/CMakeFiles/asio_opencv.dir/DependInfo.cmake" is newer than depender "/home/user/project-yanack/asio_opencv/build/CMakeFiles/asio_opencv.dir/depend.internal".
Dependee "/home/user/project-yanack/asio_opencv/build/CMakeFiles/CMakeDirectoryInformation.cmake" is newer than depender "/home/user/project-yanack/asio_opencv/build/CMakeFiles/asio_opencv.dir/depend.internal".
Scanning dependencies of target asio_opencv
make[2]: Leaving directory `/home/user/project-yanack/asio_opencv/build'
make -f CMakeFiles/asio_opencv.dir/build.make CMakeFiles/asio_opencv.dir/build
make[2]: Entering directory `/home/user/project-yanack/asio_opencv/build'
/usr/bin/cmake -E cmake_progress_report /home/user/project-yanack/asio_opencv/build/CMakeFiles 1
[100%] Building CXX object CMakeFiles/asio_opencv.dir/src/asio_opencv.cpp.o
/usr/bin/c++ -std=c++11 -I/usr/include/opencv -I/home/user/project-yanack/asio_opencv/build -I/home/user/project-yanack/asio_opencv/include -o CMakeFiles/asio_opencv.dir/src/asio_opencv.cpp.o -c /home/user/project-yanack/asio_opencv/src/asio_opencv.cpp
Linking CXX executable asio_opencv
/usr/bin/cmake -E cmake_link_script CMakeFiles/asio_opencv.dir/link.txt --verbose=1
/usr/bin/c++ -std=c++11 CMakeFiles/asio_opencv.dir/src/asio_opencv.cpp.o -o asio_opencv -rdynamic /usr/lib/x86_64-linux-gnu/libopencv_videostab.so.2.4.8 /usr/lib/x86_64-linux-gnu/libopencv_video.so.2.4.8 /usr/lib/x86_64-linux-gnu/libopencv_ts.so.2.4.8 /usr/lib/x86_64-linux-gnu/libopencv_superres.so.2.4.8 /usr/lib/x86_64-linux-gnu/libopencv_stitching.so.2.4.8 /usr/lib/x86_64-linux-gnu/libopencv_photo.so.2.4.8 /usr/lib/x86_64-linux-gnu/libopencv_ocl.so.2.4.8 /usr/lib/x86_64-linux-gnu/libopencv_objdetect.so.2.4.8 /usr/lib/x86_64-linux-gnu/libopencv_ml.so.2.4.8 /usr/lib/x86_64-linux-gnu/libopencv_legacy.so.2.4.8 /usr/lib/x86_64-linux-gnu/libopencv_imgproc.so.2.4.8 /usr/lib/x86_64-linux-gnu/libopencv_highgui.so.2.4.8 /usr/lib/x86_64-linux-gnu/libopencv_gpu.so.2.4.8 /usr/lib/x86_64-linux-gnu/libopencv_flann.so.2.4.8 /usr/lib/x86_64-linux-gnu/libopencv_features2d.so.2.4.8 /usr/lib/x86_64-linux-gnu/libopencv_core.so.2.4.8 /usr/lib/x86_64-linux-gnu/libopencv_contrib.so.2.4.8 /usr/lib/x86_64-linux-gnu/libopencv_calib3d.so.2.4.8 -lboost_system /usr/lib/x86_64-linux-gnu/libopencv_photo.so.2.4.8 /usr/lib/x86_64-linux-gnu/libopencv_legacy.so.2.4.8 /usr/lib/x86_64-linux-gnu/libopencv_video.so.2.4.8 /usr/lib/x86_64-linux-gnu/libopencv_objdetect.so.2.4.8 /usr/lib/x86_64-linux-gnu/libopencv_ml.so.2.4.8 /usr/lib/x86_64-linux-gnu/libopencv_calib3d.so.2.4.8 /usr/lib/x86_64-linux-gnu/libopencv_features2d.so.2.4.8 /usr/lib/x86_64-linux-gnu/libopencv_highgui.so.2.4.8 /usr/lib/x86_64-linux-gnu/libopencv_imgproc.so.2.4.8 /usr/lib/x86_64-linux-gnu/libopencv_flann.so.2.4.8 /usr/lib/x86_64-linux-gnu/libopencv_core.so.2.4.8
make[2]: Leaving directory `/home/user/project-yanack/asio_opencv/build'
/usr/bin/cmake -E cmake_progress_report /home/user/project-yanack/asio_opencv/build/CMakeFiles 1
[100%] Built target asio_opencv
make[1]: Leaving directory `/home/user/project-yanack/asio_opencv/build'
/usr/bin/cmake -E cmake_progress_start /home/user/project-yanack/asio_opencv/build/CMakeFiles 0

Make simple GLFW compiling with MinGW and cmake

I'm using cmake (clion embedded) and mingw to compile simple glfw code:
#include <GLFW/glfw3.h>
int main()
{
glfwInit();
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
glfwWindowHint(GLFW_RESIZABLE, GL_FALSE);
return 0;
}
This is my cmake file
cmake_minimum_required(VERSION 3.3)
project(hw)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
find_library(GLFW REQUIRED glfw3)
include_directories(${GLFW_INCLUDE_DIRS})
link_directories(${GLFW_LIBRARY_DIRS})
set(SOURCE_FILES main.cpp)
add_executable(hw ${SOURCE_FILES})
target_link_libraries(hw ${GLFW_LIBRARIES} )
I've included downloaded glfw for win64 files:
GLFW folder to C:\MinGW\include
lib-mingw folder content to C:\MinGW\lib
And I get following error
"C:\Program Files (x86)\JetBrains\CLion 1.1.1\bin\cmake\bin\cmake.exe" --build C:\Users\...\.clion11\system\cmake\generated\82abf0c4\82abf0c4\Debug --target all -- -j 2
"C:\Program Files (x86)\JetBrains\CLion 1.1.1\bin\cmake\bin\cmake.exe" -HC:\Users\...\ClionProjects\hw -BC:\Users\...\.clion11\system\cmake\generated\82abf0c4\82abf0c4\Debug --check-build-system CMakeFiles\Makefile.cmake 0
"C:\Program Files (x86)\JetBrains\CLion 1.1.1\bin\cmake\bin\cmake.exe" -E cmake_progress_start C:\Users\...\.clion11\system\cmake\generated\82abf0c4\82abf0c4\Debug\CMakeFiles C:\Users\...\.clion11\system\cmake\generated\82abf0c4\82abf0c4\Debug\CMakeFiles\progress.marks
C:/MinGW/bin/mingw32-make.exe -f CMakeFiles\Makefile2 all
mingw32-make.exe[1]: Entering directory 'C:/Users/.../.clion11/system/cmake/generated/82abf0c4/82abf0c4/Debug'
C:/MinGW/bin/mingw32-make.exe -f CMakeFiles\hw.dir\build.make CMakeFiles/hw.dir/depend
mingw32-make.exe[2]: Entering directory 'C:/Users/.../.clion11/system/cmake/generated/82abf0c4/82abf0c4/Debug'
"C:\Program Files (x86)\JetBrains\CLion 1.1.1\bin\cmake\bin\cmake.exe" -E cmake_depends "MinGW Makefiles" C:\Users\...\ClionProjects\hw C:\Users\...\ClionProjects\hw C:\Users\...\.clion11\system\cmake\generated\82abf0c4\82abf0c4\Debug C:\Users\...\.clion11\system\cmake\generated\82abf0c4\82abf0c4\Debug C:\Users\...\.clion11\system\cmake\generated\82abf0c4\82abf0c4\Debug\CMakeFiles\hw.dir\DependInfo.cmake --color=
mingw32-make.exe[2]: Leaving directory 'C:/Users/.../.clion11/system/cmake/generated/82abf0c4/82abf0c4/Debug'
C:/MinGW/bin/mingw32-make.exe -f CMakeFiles\hw.dir\build.make CMakeFiles/hw.dir/build
mingw32-make.exe[2]: Entering directory 'C:/Users/.../.clion11/system/cmake/generated/82abf0c4/82abf0c4/Debug'
[ 50%] Linking CXX executable hw.exe
"C:\Program Files (x86)\JetBrains\CLion 1.1.1\bin\cmake\bin\cmake.exe" -E cmake_link_script CMakeFiles\hw.dir\link.txt --verbose=1
"C:\Program Files (x86)\JetBrains\CLion 1.1.1\bin\cmake\bin\cmake.exe" -E remove -f CMakeFiles\hw.dir/objects.a
C:\MinGW\bin\ar.exe cr CMakeFiles\hw.dir/objects.a #CMakeFiles\hw.dir\objects1.rsp
C:\MinGW\bin\g++.exe -std=c++11 -g -Wl,--whole-archive CMakeFiles\hw.dir/objects.a -Wl,--no-whole-archive -o hw.exe -Wl,--out-implib,libhw.dll.a -Wl,--major-image-version,0,--minor-image-version,0 #CMakeFiles\hw.dir\linklibs.rsp
CMakeFiles\hw.dir/objects.a(main.cpp.obj): In function `main':
C:/Users/.../ClionProjects/hw/main.cpp:5: undefined reference to `glfwInit'
C:/Users/.../ClionProjects/hw/main.cpp:6: undefined reference to `glfwWindowHint'
C:/Users/.../ClionProjects/hw/main.cpp:7: undefined reference to `glfwWindowHint'
C:/Users/.../ClionProjects/hw/main.cpp:8: undefined reference to `glfwWindowHint'
C:/Users/.../ClionProjects/hw/main.cpp:9: undefined reference to `glfwWindowHint'
collect2.exe: error: ld returned 1 exit status
mingw32-make.exe[2]: *** [hw.exe] Error 1
mingw32-make.exe[1]: *** [CMakeFiles/hw.dir/all] Error 2
CMakeFiles\hw.dir\build.make:98: recipe for target 'hw.exe' failed
mingw32-make.exe[2]: Leaving directory 'C:/Users/.../.clion11/system/cmake/generated/82abf0c4/82abf0c4/Debug'
CMakeFiles\Makefile2:69: recipe for target 'CMakeFiles/hw.dir/all' failed
mingw32-make.exe[1]: Leaving directory 'C:/Users/.../.clion11/system/cmake/generated/82abf0c4/82abf0c4/Debug'
Makefile:85: recipe for target 'all' failed
mingw32-make.exe: *** [all] Error 2
This is my pretty first program c++ program with cmake and mingw, so I cant underestand what I have to do to make it just compile

error linking GMP library

I have the following CMakeList.txt
cmake_minimum_required(VERSION 2.8)
set(CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}")
find_package(GMP REQUIRED)
add_executable(Turrial main.cpp)
with the following simple source file main.cpp
#include <iostream>
#include <gmp.h>
#include <cstdio>
using namespace std;
int main(){
mpz_t a,b;
mpz_init_set_str(a,"45",10);
mpz_init_set_str(b,"12",10);
mpz_add(a,a,b);
cout <<mpz_get_str(NULL,10,a)<<endl;
}
I have included a FindGMP.cmake file that i found online :
set(GMP_PREFIX "" CACHE PATH "path ")
find_path(GMP_INCLUDE_DIR gmp.h gmpxx.h
PATHS ${GMP_PREFIX}/include /usr/include /usr/local/include )
find_library(GMP_LIBRARY NAMES gmp libgmp
PATHS ${GMP_PREFIX}/lib /usr/lib /usr/local/lib)
if(GMP_INCLUDE_DIR AND GMP_LIBRARY)
get_filename_component(GMP_LIBRARY_DIR ${GMP_LIBRARY} PATH)
set(GMP_FOUND TRUE)
endif()
if(GMP_FOUND)
if(NOT GMP_FIND_QUIETLY)
MESSAGE(STATUS "Found GMP: ${GMP_LIBRARY}")
endif()
elseif(GMP_FOUND)
if(GMP_FIND_REQUIRED)
message(FATAL_ERROR "Could not find GMP")
endif()
endif(
when I run cmake I have the following output:
moyle#localhost:~/Desktop/gmptri$ cmake .
-- Found GMP: /usr/local/lib/libgmp.so
-- Configuring done
-- Generating done
-- Build files have been written to: /home/moyle/Desktop/gmptri
Then I run make and i get the following errors :
(precise)moyle#localhost:~/Desktop/gmptri$ make VERBOSE=1
/usr/bin/cmake -H/home/moyle/Desktop/gmptri -B/home/moyle/Desktop/gmptri --check-build-system CMakeFiles/Makefile.cmake 0
/usr/bin/cmake -E cmake_progress_start /home/moyle/Desktop/gmptri/CMakeFiles /home/moyle/Desktop/gmptri/CMakeFiles/progress.marks
make -f CMakeFiles/Makefile2 all
make[1]: Entering directory `/home/moyle/Desktop/gmptri'
make -f CMakeFiles/Turrial.dir/build.make CMakeFiles/Turrial.dir/depend
make[2]: Entering directory `/home/moyle/Desktop/gmptri'
cd /home/moyle/Desktop/gmptri && /usr/bin/cmake -E cmake_depends "Unix Makefiles" /home/moyle/Desktop/gmptri /home/moyle/Desktop/gmptri /home/moyle/Desktop/gmptri /home/moyle/Desktop/gmptri /home/moyle/Desktop/gmptri/CMakeFiles/Turrial.dir/DependInfo.cmake --color=
make[2]: Leaving directory `/home/moyle/Desktop/gmptri'
make -f CMakeFiles/Turrial.dir/build.make CMakeFiles/Turrial.dir/build
make[2]: Entering directory `/home/moyle/Desktop/gmptri'
Linking CXX executable Turrial
/usr/bin/cmake -E cmake_link_script CMakeFiles/Turrial.dir/link.txt --verbose=1
/usr/bin/c++ CMakeFiles/Turrial.dir/main.cpp.o -o Turrial -rdynamic
CMakeFiles/Turrial.dir/main.cpp.o: In function `main':
main.cpp:(.text+0x1a): undefined reference to `__gmpz_init_set_str'
main.cpp:(.text+0x30): undefined reference to `__gmpz_init_set_str'
main.cpp:(.text+0x47): undefined reference to `__gmpz_add'
collect2: ld returned 1 exit status
make[2]: *** [Turrial] Error 1
make[2]: Leaving directory `/home/moyle/Desktop/gmptri'
make[1]: *** [CMakeFiles/Turrial.dir/all] Error 2
make[1]: Leaving directory `/home/moyle/Desktop/gmptri'
make: *** [all] Error 2
can anyone help me?
You forget to link libraries with your executable.
find_package(GMP REQUIRED)
add_executable(Turrial main.cpp)
target_link_libraries(Turrial gmp libgmp)

CMake adding opencv

I'm trying to include opencv2.4.9 with cmake:
cmake_minimum_required(VERSION 2.8.3)
project(HydroCamel)
SET(CMAKE_CXX_FLAGS "-lpthread")
find_package(catkin REQUIRED COMPONENTS
roscpp
rospy
std_msgs
)
catkin_package()
set(SRC_FOLDER src/src)
set(ALOGS_FOLDER src/Algos)
set(ALOGS_UTILS_FOLDER src/Algos/Utils)
set(SRC_INCLUDE_FOLDER src/include)
set(DIRS ${SRC_FOLDER} ${ALOGS_FOLDER} ${ALOGS_UTILS_FOLDER} ${SRC_INCLUDE_FOLDER})
include_directories(${DIRS})
file(GLOB_RECURSE SRC_FILES "*.h" "*.cpp")
include_directories(include ${catkin_INCLUDE_DIRS})
set(Boost_USE_STATIC_LIBS OFF)
set(Boost_USE_MULTITHREADED ON)
set(Boost_USE_STATIC_RUNTIME OFF)
find_package(Boost 1.55.0 COMPONENTS filesystem system thread)
#FIND_PACKAGE(OpenCV REQUIRED core imgproc highgui)
include_directories(${OpenCV_INCLUDE_DIRS})
if(Boost_FOUND)
include_directories(${Boost_INCLUDE_DIRS})
add_executable(SourceFiles ${SRC_FILES})
TARGET_LINK_LIBRARIES(SourceFiles ${Boost_LIBRARIES})
TARGET_LINK_LIBRARIES(SourceFiles ${OpenCV_LIBS})
endif()
And I receive this error:
CMake Error at HydroCamel/CMakeLists.txt:28 (find_package): Found
package configuration file:
/home/jdorfsman/opencv-2.4.9/build/OpenCVConfig.cmake
but it set OpenCV_FOUND to FALSE so package "OpenCV" is considered
to be NOT FOUND.
-- Configuring incomplete, errors occurred! See also "/home/jdorfsman/catkin_ws/build/CMakeFiles/CMakeOutput.log". See also
"/home/jdorfsman/catkin_ws/build/CMakeFiles/CMakeError.log".
This is the CMakeError.log file:
Determining if the pthread_create exist failed with the following output:
Change Dir: /home/jdorfsman/catkin_ws/build/CMakeFiles/CMakeTmp
Run Build Command:/usr/bin/make "cmTryCompileExec1061744568/fast"
/usr/bin/make -f CMakeFiles/cmTryCompileExec1061744568.dir/build.make CMakeFiles/cmTryCompileExec1061744568.dir/build
make[1]: Entering directory `/home/jdorfsman/catkin_ws/build/CMakeFiles/CMakeTmp'
/usr/bin/cmake -E cmake_progress_report /home/jdorfsman/catkin_ws/build/CMakeFiles/CMakeTmp/CMakeFiles 1
Building C object CMakeFiles/cmTryCompileExec1061744568.dir/CheckSymbolExists.c.o
/usr/bin/cc -o CMakeFiles/cmTryCompileExec1061744568.dir/CheckSymbolExists.c.o -c /home/jdorfsman/catkin_ws/build/CMakeFiles/CMakeTmp/CheckSymbolExists.c
Linking C executable cmTryCompileExec1061744568
/usr/bin/cmake -E cmake_link_script CMakeFiles/cmTryCompileExec1061744568.dir/link.txt --verbose=1
/usr/bin/cc CMakeFiles/cmTryCompileExec1061744568.dir/CheckSymbolExists.c.o -o cmTryCompileExec1061744568 -rdynamic
CMakeFiles/cmTryCompileExec1061744568.dir/CheckSymbolExists.c.o: In function `main':
CheckSymbolExists.c:(.text+0x16): undefined reference to `pthread_create'
collect2: error: ld returned 1 exit status
make[1]: *** [cmTryCompileExec1061744568] Error 1
make[1]: Leaving directory `/home/jdorfsman/catkin_ws/build/CMakeFiles/CMakeTmp'
make: *** [cmTryCompileExec1061744568/fast] Error 2
File /home/jdorfsman/catkin_ws/build/CMakeFiles/CMakeTmp/CheckSymbolExists.c:
/* */
#include <pthread.h>
int main(int argc, char** argv)
{
(void)argv;
#ifndef pthread_create
return ((int*)(&pthread_create))[argc];
#else
(void)argc;
return 0;
#endif
}
Determining if the function pthread_create exists in the pthreads failed with the following output:
Change Dir: /home/jdorfsman/catkin_ws/build/CMakeFiles/CMakeTmp
Run Build Command:/usr/bin/make "cmTryCompileExec3426763052/fast"
/usr/bin/make -f CMakeFiles/cmTryCompileExec3426763052.dir/build.make CMakeFiles/cmTryCompileExec3426763052.dir/build
make[1]: Entering directory `/home/jdorfsman/catkin_ws/build/CMakeFiles/CMakeTmp'
/usr/bin/cmake -E cmake_progress_report /home/jdorfsman/catkin_ws/build/CMakeFiles/CMakeTmp/CMakeFiles 1
Building C object CMakeFiles/cmTryCompileExec3426763052.dir/CheckFunctionExists.c.o
/usr/bin/cc -DCHECK_FUNCTION_EXISTS=pthread_create -o CMakeFiles/cmTryCompileExec3426763052.dir/CheckFunctionExists.c.o -c /usr/share/cmake-2.8/Modules/CheckFunctionExists.c
Linking C executable cmTryCompileExec3426763052
/usr/bin/cmake -E cmake_link_script CMakeFiles/cmTryCompileExec3426763052.dir/link.txt --verbose=1
/usr/bin/cc -DCHECK_FUNCTION_EXISTS=pthread_create CMakeFiles/cmTryCompileExec3426763052.dir/CheckFunctionExists.c.o -o cmTryCompileExec3426763052 -rdynamic -lpthreads
/usr/bin/ld: cannot find -lpthreads
collect2: error: ld returned 1 exit status
make[1]: *** [cmTryCompileExec3426763052] Error 1
make[1]: Leaving directory `/home/jdorfsman/catkin_ws/build/CMakeFiles/CMakeTmp'
make: *** [cmTryCompileExec3426763052/fast] Error 2
Looks like you don't specify the pthread flag.
CMakeFiles/cmTryCompileExec1061744568.dir/CheckSymbolExists.c.o: In function `main':
CheckSymbolExists.c:(.text+0x16): undefined reference to `pthread_create'
collect2: error: ld returned 1 exit status
make[1]: *** [cmTryCompileExec1061744568] Error 1
make[1]: Leaving directory `/home/jdorfsman/catkin_ws/build/CMakeFiles/CMakeTmp'
make: *** [cmTryCompileExec1061744568/fast] Error 2
try add:
SET(CMAKE_CXX_FLAGS "-lpthread")