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")
Related
I've been trying to get SDL2 to link with my project but I think my cmake file is no good. I get this error
====================[ Build | Chip_8_Interpreter | Debug ]======================
"C:\Program Files\JetBrains\CLion 2018.3.4\bin\cmake\win\bin\cmake.exe" --build "D:\Dropbox\Programming\Chip-8 Interpreter\cmake-build-debug" --target Chip_8_Interpreter -- -j 4
[ 50%] Linking CXX executable Chip_8_Interpreter.exe
c:/mingw/bin/../lib/gcc/mingw32/6.3.0/../../../libmingw32.a(main.o):(.text.startup+0xa0): undefined reference to `WinMain#16'
collect2.exe: error: ld returned 1 exit status
mingw32-make.exe[3]: *** [Chip_8_Interpreter.exe] Error 1
CMakeFiles\Chip_8_Interpreter.dir\build.make:87: recipe for target 'Chip_8_Interpreter.exe' failed
mingw32-make.exe[2]: *** [CMakeFiles/Chip_8_Interpreter.dir/all] Error 2
CMakeFiles\Makefile2:71: recipe for target 'CMakeFiles/Chip_8_Interpreter.dir/all' failed
mingw32-make.exe[1]: *** [CMakeFiles/Chip_8_Interpreter.dir/rule] Error 2
CMakeFiles\Makefile2:83: recipe for target 'CMakeFiles/Chip_8_Interpreter.dir/rule' failed
mingw32-make.exe: *** [Chip_8_Interpreter] Error 2
Makefile:117: recipe for target 'Chip_8_Interpreter' failed
And currently my CMakeLists.txt looks like this
cmake_minimum_required(VERSION 3.14)
project(Chip_8_Interpreter)
set(CMAKE_CXX_STANDARD 14)
# include cmake/FindSDL2.cmake
set(SDL2_PATH "C:\\SDL2-2.0.12\\i686-w64-mingw32")
set(CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/cmake)
find_package(SDL2 REQUIRED)
include_directories(${SDL2_INCLUDE_DIR})
add_executable(Chip_8_Interpreter main.cpp)
target_link_libraries(Chip_8_Interpreter ${SDL2_LIBRARY} -lmingw32 -lSDL2main -lSDL2 -mwindows)
And my main.cpp is just the basic SDL example. I've also recently installed MinGW and SDL2, so they should both be the newest stable release.
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)
I'm compiling NVIDIA Caffe tool on a Fedora 22 and I having problems to find lpthread library:
Determining if the pthread_create exist failed with the following output:
Change Dir: /home/user1/Sources/caffe/build/CMakeFiles/CMakeTmp
Run Build Command:"/usr/bin/gmake" "cmTC_d410e/fast"
/usr/bin/gmake -f CMakeFiles/cmTC_d410e.dir/build.make CMakeFiles/cmTC_d410e.dir/build
gmake[1]: Entering directory '/home/user1/Sources/caffe/build/CMakeFiles/CMakeTmp'
Building C object CMakeFiles/cmTC_d410e.dir/CheckSymbolExists.c.o
/usr/bin/cc -o CMakeFiles/cmTC_d410e.dir/CheckSymbolExists.c.o -c /home/user1/Sources/caffe/build/CMakeFiles/CMakeTmp/CheckSymbolExists.c
Linking C executable cmTC_d410e
/usr/bin/cmake -E cmake_link_script CMakeFiles/cmTC_d410e.dir/link.txt --verbose=1
/usr/bin/cc CMakeFiles/cmTC_d410e.dir/CheckSymbolExists.c.o -o cmTC_d410e -rdynamic
CMakeFiles/cmTC_d410e.dir/CheckSymbolExists.c.o: En la función `main':
CheckSymbolExists.c:(.text+0x16): referencia a `pthread_create' sin definir
collect2: error: ld devolvió el estado de salida 1
CMakeFiles/cmTC_d410e.dir/build.make:97: recipe for target 'cmTC_d410e' failed
gmake[1]: Leaving directory '/home/user1/Sources/caffe/build/CMakeFiles/CMakeTmp'
Makefile:126: recipe for target 'cmTC_d410e/fast' failed
gmake[1]: *** [cmTC_d410e] Error 1
gmake: *** [cmTC_d410e/fast] Error 2
File /home/user1/Sources/caffe/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/user1/Sources/caffe/build/CMakeFiles/CMakeTmp
Run Build Command:"/usr/bin/gmake" "cmTC_e693b/fast"
/usr/bin/gmake -f CMakeFiles/cmTC_e693b.dir/build.make CMakeFiles/cmTC_e693b.dir/build
gmake[1]: Entering directory '/home/user1/Sources/caffe/build/CMakeFiles/CMakeTmp'
Building C object CMakeFiles/cmTC_e693b.dir/CheckFunctionExists.c.o
/usr/bin/cc -DCHECK_FUNCTION_EXISTS=pthread_create -o CMakeFiles/cmTC_e693b.dir/CheckFunctionExists.c.o -c /usr/share/cmake/Modules/CheckFunctionExists.c
Linking C executable cmTC_e693b
/usr/bin/cmake -E cmake_link_script CMakeFiles/cmTC_e693b.dir/link.txt --verbose=1
/usr/bin/cc -DCHECK_FUNCTION_EXISTS=pthread_create CMakeFiles/cmTC_e693b.dir/CheckFunctionExists.c.o -o cmTC_e693b -rdynamic -lpthreads
/usr/bin/ld: cannot find -lpthreads
collect2: error: ld outputs 1
CMakeFiles/cmTC_e693b.dir/build.make:97: recipe for target 'cmTC_e693b' failed
gmake[1]: Leaving directory '/home/user1/Sources/caffe/build/CMakeFiles/CMakeTmp'
Makefile:126: recipe for target 'cmTC_e693b/fast' failed
gmake[1]: *** [cmTC_e693b] Error 1
gmake: *** [cmTC_e693b/fast] Error 2
It seems that it doesn't find pthreads lib (/usr/bin/ld: cannot find -lpthreads), but running command find / name "*pthread*" it outputs this:
/usr/lib64/libpthread.so
/usr/lib64/libpthread.a
/usr/lib64/libpthread_nonshared.a
/usr/lib64/libpthread-2.21.so
/usr/lib64/libpthread.so.0
But there is something strange with this, becaue CMakeOutut.log has this:
Determining if the function pthread_create exists in the pthread passed with the following output:
Change Dir: /home/user1/Sources/caffe/build/CMakeFiles/CMakeTmp
Run Build Command:"/usr/bin/gmake" "cmTC_705ba/fast"
/usr/bin/gmake -f CMakeFiles/cmTC_705ba.dir/build.make CMakeFiles/cmTC_705ba.dir/build
gmake[1]: Entering directory '/home/user1/Sources/caffe/build/CMakeFiles/CMakeTmp'
Building C object CMakeFiles/cmTC_705ba.dir/CheckFunctionExists.c.o
/usr/bin/cc -DCHECK_FUNCTION_EXISTS=pthread_create -o CMakeFiles/cmTC_705ba.dir/CheckFunctionExists.c.o -c /usr/share/cmake/Modules/CheckFunctionExists.c
Linking C executable cmTC_705ba
/usr/bin/cmake -E cmake_link_script CMakeFiles/cmTC_705ba.dir/link.txt --verbose=1
/usr/bin/cc -DCHECK_FUNCTION_EXISTS=pthread_create CMakeFiles/cmTC_705ba.dir/CheckFunctionExists.c.o -o cmTC_705ba -rdynamic -lpthread
gmake[1]: Leaving directory '/home/user1/Sources/caffe/build/CMakeFiles/CMakeTmp'
It has this line using lpthread:
/usr/bin/cc -DCHECK_FUNCTION_EXISTS=pthread_create CMakeFiles/cmTC_705ba.dir/CheckFunctionExists.c.o -o cmTC_705ba -rdynamic -lpthread
And this is CMakeList.txt:
cmake_minimum_required(VERSION 2.8.7)
# ---[ Caffe project
project(Caffe C CXX)
# ---[ Caffe version
set(CAFFE_TARGET_VERSION "0.14.5")
set(CAFFE_TARGET_SOVERSION "0.14")
add_definitions(-DCAFFE_VERSION=${CAFFE_TARGET_VERSION})
# ---[ Using cmake scripts and modules
list(APPEND CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/cmake/Modules)
include(ExternalProject)
include(cmake/Utils.cmake)
include(cmake/Targets.cmake)
include(cmake/Misc.cmake)
include(cmake/Summary.cmake)
include(cmake/ConfigGen.cmake)
# ---[ Options
caffe_option(CPU_ONLY "Build Caffe without CUDA support" OFF) # TODO: rename to USE_CUDA
caffe_option(USE_CUDNN "Build Caffe with cuDNN library support" ON IF NOT CPU_ONLY)
caffe_option(BUILD_SHARED_LIBS "Build shared libraries" ON)
caffe_option(BUILD_python "Build Python wrapper" ON)
set(python_version "2" CACHE STRING "Specify which Python version to use")
caffe_option(BUILD_matlab "Build Matlab wrapper" OFF IF UNIX OR APPLE)
caffe_option(BUILD_docs "Build documentation" ON IF UNIX OR APPLE)
caffe_option(BUILD_python_layer "Build the Caffe Python layer" ON)
caffe_option(USE_OPENCV "Build with OpenCV support" ON)
caffe_option(USE_LEVELDB "Build with levelDB" ON)
caffe_option(USE_LMDB "Build with lmdb" ON)
caffe_option(ALLOW_LMDB_NOLOCK "Allow MDB_NOLOCK when reading LMDB files (only if necessary)" OFF)
# ---[ Dependencies
include(cmake/Dependencies.cmake)
# ---[ Flags
if(UNIX OR APPLE)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fPIC -Wall")
endif()
if(USE_libstdcpp)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -stdlib=libstdc++")
message("-- Warning: forcing libstdc++ (controlled by USE_libstdcpp option in cmake)")
endif()
add_definitions(-DGTEST_USE_OWN_TR1_TUPLE)
# ---[ Warnings
caffe_warnings_disable(CMAKE_CXX_FLAGS -Wno-sign-compare -Wno-uninitialized)
# ---[ Config generation
configure_file(cmake/Templates/caffe_config.h.in "${PROJECT_BINARY_DIR}/caffe_config.h")
# ---[ Includes
set(Caffe_INCLUDE_DIR ${PROJECT_SOURCE_DIR}/include)
set(THIRDPARTY_DIR ${PROJECT_SOURCE_DIR}/3rdparty)
include_directories(${Caffe_INCLUDE_DIR} ${PROJECT_BINARY_DIR} ${THIRDPARTY_DIR})
include_directories(BEFORE src) # This is needed for gtest.
# ---[ Subdirectories
add_subdirectory(src/gtest)
add_subdirectory(src/caffe)
add_subdirectory(tools)
add_subdirectory(examples)
add_subdirectory(python)
add_subdirectory(matlab)
add_subdirectory(docs)
# ---[ Linter target
add_custom_target(lint COMMAND ${CMAKE_COMMAND} -P ${PROJECT_SOURCE_DIR}/cmake/lint.cmake)
# ---[ pytest target
add_custom_target(pytest COMMAND python${python_version} -m unittest discover -s caffe/test WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}/python )
add_dependencies(pytest pycaffe)
# ---[ Configuration summary
caffe_print_configuration_summary()
# ---[ Export configs generation
caffe_generate_export_configs()
What do I have to install? Does libphreads exist or I have to use libphread?
You have a typo. You should use -lpthread instead of -lpthreads.
Alternatively you can just use -pthread, that also works.
I have this error when I follow the caffe doc on a new Ubuntu, and after some Google with caffe collect2: error: ld returned 1 exit status, I find I was lack of some packages, not typo:
sudo apt-get install libprotobuf-dev libleveldb-dev libsnappy-dev libopencv-dev libboost-all-dev libhdf5-serial-dev libgflags-dev libgoogle-glog-dev liblmdb-dev protobuf-compiler libatlas-base-dev
sudo apt-get install python-dev python-pip gfortran
For more detailed info, ref here.
The problem is caused by googletest, and just set the following line of CMakeList.txt of googletest:
option(gtest_disable_pthreads "Disable uses of pthreads in gtest." ON)
by the flags on.
The reason is:
We must check for the threads library under a number of different
names; the ordering is very important because some systems
(e.g. DEC) have both -lpthread and -lpthreads, where one of the
libraries is broken (non-POSIX).
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)
I'm currently struggling with linking a C++ project that requires linking with GLFW3, i.e., ld is called with -lglfw3. The thing is that this worked before, and right now I have no clue, why it is not anymore.
sudo pkg-config --libs glfw3
-L/usr/local/lib -lglfw3
The library exists also at that specific location printed by the statement above. However, ld says:
Linking CXX executable modeling_2014_framework
/usr/bin/ld: error: cannot find -lglfw3
CMakeFiles/modeling_2014_framework.dir/main.cpp.o:main.cpp:function init(): error: undefined reference to 'glfwInit'
...
collect2: error: ld returned 1 exit status
make[2]: *** [modeling_2014_framework] Error 1
make[1]: *** [CMakeFiles/modeling_2014_framework.dir/all] Error 2
make: *** [all] Error 2
I'm using cmake to create the makefile. This is how the CMakeLists looks like:
cmake_minimum_required (VERSION 2.8)
set (project_name modeling_2014_framework)
# create project
project (${project_name})
# sources
set(SOURCES main.cpp)
# add headers to the project
file(GLOB MY_HEADERS "src/*.h")
list(APPEND HEADERS ${MY_HEADERS})
# add sources to the project
file(GLOB MY_SOURCES "src/*.cpp")
list(APPEND SOURCES ${MY_SOURCES})
# i also tried to manually set them here, which however also does not help
#set(CMAKE_LIBRARY_PATH ${CMAKE_LIBRARY_PATH} /usr/local/lib)
# OpenGL
find_package(OpenGL REQUIRED)
if (NOT OPENGL_FOUND)
message (FATAL_ERROR "OpenGL not found!")
return()
endif ()
# glew
if (UNIX)
set(GLEW_LIBRARY libGLEW.so)
else()
set (GLEW_INCLUDE_DIR "" CACHE PATH "glew include directory")
set (GLEW_LIBRARY_DIR "" CACHE PATH "glew build directory")
if (GLEW_INCLUDE_DIR STREQUAL "" AND GLEW_LIBRARY_DIR STREQUAL "")
return()
else()
find_library(GLEW_LIBRARY NAMES GLEW glew32 glew glew32s PATHS ${GLEW_LIBRARY_DIR})
endif()
endif(UNIX)
# glfw
set (GLFW_STATIC FALSE CACHE BOOL "link against static or dynamic glfw3 library")
if (WIN32)
set (GLFW_INCLUDE_DIR "" CACHE PATH "GLFW include directory")
set (GLFW_LIBRARY_DIR "" CACHE PATH "GLFW build directory")
if (GLFW_INCLUDE_DIR STREQUAL "" AND GLFW_LIBRARY_DIR STREQUAL "")
return()
else()
if (GLFW_STATIC)
find_library(GLFW_LIBS NAMES glfw3 PATHS ${GLFW_LIBRARY_DIR})
else(GLFW_STATIC)
find_library(GLFW_LIBS NAMES glfw3dll PATHS ${GLFW_LIBRARY_DIR})
endif(GLFW_STATIC)
endif()
else()
find_package(PkgConfig REQUIRED)
pkg_search_module(GLFW REQUIRED glfw3)
if (GLFW_STATIC)
set (GLFW_LIBS ${GLFW_STATIC_LIBRARIES})
else(GLFW_STATIC)
set (GLFW_LIBS ${GLFW_LIBRARIES})
endif(GLFW_STATIC)
list(APPEND GLFW_LIBS libXxf86vm.so)
list(APPEND GLFW_LIBS libGLU.so)
list(APPEND GLFW_LIBS libX11.so)
list(APPEND GLFW_LIBS libXrandr.so)
list(APPEND GLFW_LIBS libpthread.so)
list(APPEND GLFW_LIBS libXi.so)
endif()
include_directories(${GLEW_INCLUDE_DIR}
${GLFW_INCLUDE_DIR}
${OPENGL_INCLUDE_DIR}
)
# executable
add_executable(${project_name} ${SOURCES} ${HEADERS})
target_link_libraries(${project_name}
${OPENGL_LIBRARIES}
${GLEW_LIBRARY}
${EXTRA_LIBS}
${GLFW_LIBS}
)
If I run the ld command manually and add -L/usr/local/lib then the linker runs fine. So my most important question would be, how to make sure that cmake adds this path to the makefile so that ld runs fine.
Thanks in advance =)
Edit1: as per request, the stdout with verbose logging:
myname#fxt ~/courses/Modellierung/proj/exercise1 $ make VERBOSE=1
/usr/bin/cmake -H/home/myname/courses/Modellierung/proj/exercise1 -B/home/myname/courses/Modellierung/proj/exercise1 --check-build-system CMakeFiles/Makefile.cmake 0
/usr/bin/cmake -E cmake_progress_start /home/myname/courses/Modellierung/proj/exercise1/CMakeFiles /home/myname/courses/Modellierung/proj/exercise1/CMakeFiles/progress.marks
make -f CMakeFiles/Makefile2 all
make[1]: Entering directory `/home/myname/courses/Modellierung/proj/exercise1'
make -f CMakeFiles/modeling_2014_framework.dir/build.make CMakeFiles/modeling_2014_framework.dir/depend
make[2]: Entering directory `/home/myname/courses/Modellierung/proj/exercise1'
cd /home/myname/courses/Modellierung/proj/exercise1 && /usr/bin/cmake -E cmake_depends "Unix Makefiles" /home/myname/courses/Modellierung/proj/exercise1 /home/myname/courses/Modellierung/proj/exercise1 /home/myname/courses/Modellierung/proj/exercise1 /home/myname/courses/Modellierung/proj/exercise1 /home/myname/courses/Modellierung/proj/exercise1/CMakeFiles/modeling_2014_framework.dir/DependInfo.cmake --color=
make[2]: Leaving directory `/home/myname/courses/Modellierung/proj/exercise1'
make -f CMakeFiles/modeling_2014_framework.dir/build.make CMakeFiles/modeling_2014_framework.dir/build
make[2]: Entering directory `/home/myname/courses/Modellierung/proj/exercise1'
Linking CXX executable modeling_2014_framework
/usr/bin/cmake -E cmake_link_script CMakeFiles/modeling_2014_framework.dir/link.txt --verbose=1
/usr/bin/g++ -g3 -gdwarf-2 CMakeFiles/modeling_2014_framework.dir/main.cpp.o CMakeFiles/modeling_2014_framework.dir/src/VertexBufferObjectAttribs.cpp.o CMakeFiles/modeling_2014_framework.dir/src/Vector3.cpp.o CMakeFiles/modeling_2014_framework.dir/src/Shader.cpp.o CMakeFiles/modeling_2014_framework.dir/src/Matrix4x4.cpp.o CMakeFiles/modeling_2014_framework.dir/src/Scene.cpp.o CMakeFiles/modeling_2014_framework.dir/src/TgaLoader.cpp.o CMakeFiles/modeling_2014_framework.dir/src/Vector4.cpp.o CMakeFiles/modeling_2014_framework.dir/src/Vector2.cpp.o CMakeFiles/modeling_2014_framework.dir/src/Renderer.cpp.o CMakeFiles/modeling_2014_framework.dir/src/Matrix3x3.cpp.o CMakeFiles/modeling_2014_framework.dir/src/Common.cpp.o CMakeFiles/modeling_2014_framework.dir/src/SkyCube.cpp.o -o modeling_2014_framework -rdynamic -lGLU -lGL -lSM -lICE -lX11 -lXext -lGLEW -lglfw3 -lXxf86vm -lGLU -lX11 -lXrandr -lpthread -lXi
/usr/bin/ld: error: cannot find -lglfw3
/home/myname/courses/Modellierung/proj/exercise1/main.cpp:49: error: undefined reference to 'glfwInit'
/home/myname/courses/Modellierung/proj/exercise1/main.cpp:53: error: undefined reference to 'glfwInit'
/home/myname/courses/Modellierung/proj/exercise1/main.cpp:60: error: undefined reference to 'glfwSetErrorCallback'
/home/myname/courses/Modellierung/proj/exercise1/main.cpp:63: error: undefined reference to 'glfwWindowHint'
/home/myname/courses/Modellierung/proj/exercise1/main.cpp:64: error: undefined reference to 'glfwWindowHint'
/home/myname/courses/Modellierung/proj/exercise1/main.cpp:65: error: undefined reference to 'glfwWindowHint'
/home/myname/courses/Modellierung/proj/exercise1/main.cpp:66: error: undefined reference to 'glfwWindowHint'
/home/myname/courses/Modellierung/proj/exercise1/main.cpp:74: error: undefined reference to 'glfwCreateWindow'
/home/myname/courses/Modellierung/proj/exercise1/main.cpp:77: error: undefined reference to 'glfwSetKeyCallback'
/home/myname/courses/Modellierung/proj/exercise1/main.cpp:78: error: undefined reference to 'glfwSetWindowSizeCallback'
/home/myname/courses/Modellierung/proj/exercise1/main.cpp:79: error: undefined reference to 'glfwSetMouseButtonCallback'
/home/myname/courses/Modellierung/proj/exercise1/main.cpp:80: error: undefined reference to 'glfwSetCursorPosCallback'
/home/myname/courses/Modellierung/proj/exercise1/main.cpp:81: error: undefined reference to 'glfwSetScrollCallback'
/home/myname/courses/Modellierung/proj/exercise1/main.cpp:84: error: undefined reference to 'glfwMakeContextCurrent'
/home/myname/courses/Modellierung/proj/exercise1/main.cpp:164: error: undefined reference to 'glfwTerminate'
/home/myname/courses/Modellierung/proj/exercise1/main.cpp:170: error: undefined reference to 'glfwGetTime'
/home/myname/courses/Modellierung/proj/exercise1/main.cpp:171: error: undefined reference to 'glfwGetTime'
/home/myname/courses/Modellierung/proj/exercise1/main.cpp:189: error: undefined reference to 'glfwPollEvents'
/home/myname/courses/Modellierung/proj/exercise1/main.cpp:190: error: undefined reference to 'glfwSwapBuffers'
/home/myname/courses/Modellierung/proj/exercise1/main.cpp:192: error: undefined reference to 'glfwSetWindowTitle'
/home/myname/courses/Modellierung/proj/exercise1/main.cpp:186: error: undefined reference to 'glfwWindowShouldClose'
/home/myname/courses/Modellierung/proj/exercise1/src/Renderer.cpp:61: error: undefined reference to 'glfwGetTime'
/home/myname/courses/Modellierung/proj/exercise1/src/Renderer.cpp:64: error: undefined reference to 'glfwGetTime'
collect2: error: ld returned 1 exit status
make[2]: *** [modeling_2014_framework] Error 1
make[2]: Leaving directory `/home/myname/courses/Modellierung/proj/exercise1'
make[1]: *** [CMakeFiles/modeling_2014_framework.dir/all] Error 2
make[1]: Leaving directory `/home/myname/courses/Modellierung/proj/exercise1'
make: *** [all] Error 2
myname#fxt ~/courses/Modellierung/proj/exercise1 $
EDIT / QUICK HACK SOLUTION:
I think this is not the real answer to the original problem, but at least I've found a workaround. I added
SET( CMAKE_EXE_LINKER_FLAGS "-L/usr/local/lib")
to the CMakeLists.txt file, directly above add_executable(...)
Now it works, because the -L... option is correctly passed to the linker. However, since I've hardcoded the path this might not work on any computer.
Seems you are missing the library search directories from the the link command line. Try something like.
LINK_DIRECTORIES(${GLFW_LIBRARY_DIR})
after successfully finding for the GLFW libraries and before add_executable
http://www.cmake.org/cmake/help/v2.8.12/cmake.html#command:link_directories