/usr/bin/ld: cannot find -lpthreads - c++

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).

Related

C++ VCPKG: ld: library not found for -lprotobuf

I'm working on a C++ Project with CMake and VCPKG for Dependencies.
After I installed by dependencies: OpenCV, gRPC, Protobuf and OpenSSL,
I get a bunch of warnings and a linker error when I start my "main" Application.
Configuration is executed as
cmake -S . -B build-files/ "-DCMAKE_TOOLCHAIN_FILE=./vcpkg/scripts/buildsystems/vcpkg.cmake"
This is output of CMake: Pastebin.
Build is executed as
cmake --build build-files/ --target myproject.Core -v
This is its output:
/usr/local/Cellar/cmake/3.24.2/bin/cmake -S/Users/oliverkarger/Desktop/myproject -B/Users/oliverkarger/Desktop/myproject/build-files --check-build-system CMakeFiles/Makefile.cmake 0
/Applications/Xcode.app/Contents/Developer/usr/bin/make -f CMakeFiles/Makefile2 myproject.Core
/usr/local/Cellar/cmake/3.24.2/bin/cmake -S/Users/oliverkarger/Desktop/myproject -B/Users/oliverkarger/Desktop/myproject/build-files --check-build-system CMakeFiles/Makefile.cmake 0
/usr/local/Cellar/cmake/3.24.2/bin/cmake -E cmake_progress_start /Users/oliverkarger/Desktop/myproject/build-files/CMakeFiles 8
/Applications/Xcode.app/Contents/Developer/usr/bin/make -f CMakeFiles/Makefile2 src/Core/CMakeFiles/myproject.Core.dir/all
/Applications/Xcode.app/Contents/Developer/usr/bin/make -f libraries/Protos/CMakeFiles/myproject.Protos.dir/build.make libraries/Protos/CMakeFiles/myproject.Protos.dir/depend
cd /Users/oliverkarger/Desktop/myproject/build-files && /usr/local/Cellar/cmake/3.24.2/bin/cmake -E cmake_depends "Unix Makefiles" /Users/oliverkarger/Desktop/myproject /Users/oliverkarger/Desktop/myproject/libraries/Protos /Users/oliverkarger/Desktop/myproject/build-files /Users/oliverkarger/Desktop/myproject/build-files/libraries/Protos /Users/oliverkarger/Desktop/myproject/build-files/libraries/Protos/CMakeFiles/myproject.Protos.dir/DependInfo.cmake --color=
Dependencies file "libraries/Protos/CMakeFiles/myproject.Protos.dir/Configuration.grpc.pb.cc.o.d" is newer than depends file "/Users/oliverkarger/Desktop/myproject/build-files/libraries/Protos/CMakeFiles/myproject.Protos.dir/compiler_depend.internal".
Dependencies file "libraries/Protos/CMakeFiles/myproject.Protos.dir/Configuration.pb.cc.o.d" is newer than depends file "/Users/oliverkarger/Desktop/myproject/build-files/libraries/Protos/CMakeFiles/myproject.Protos.dir/compiler_depend.internal".
Consolidate compiler generated dependencies of target myproject.Protos
/Applications/Xcode.app/Contents/Developer/usr/bin/make -f libraries/Protos/CMakeFiles/myproject.Protos.dir/build.make libraries/Protos/CMakeFiles/myproject.Protos.dir/build
make[3]: Nothing to be done for `libraries/Protos/CMakeFiles/myproject.Protos.dir/build'.
[ 62%] Built target myproject.Protos
/Applications/Xcode.app/Contents/Developer/usr/bin/make -f src/Core/CMakeFiles/myproject.Core.dir/build.make src/Core/CMakeFiles/myproject.Core.dir/depend
cd /Users/oliverkarger/Desktop/myproject/build-files && /usr/local/Cellar/cmake/3.24.2/bin/cmake -E cmake_depends "Unix Makefiles" /Users/oliverkarger/Desktop/myproject /Users/oliverkarger/Desktop/myproject/src/Core /Users/oliverkarger/Desktop/myproject/build-files /Users/oliverkarger/Desktop/myproject/build-files/src/Core /Users/oliverkarger/Desktop/myproject/build-files/src/Core/CMakeFiles/myproject.Core.dir/DependInfo.cmake --color=
Dependencies file "src/Core/CMakeFiles/myproject.Core.dir/ConfigurationServiceProvider.cc.o.d" is newer than depends file "/Users/oliverkarger/Desktop/myproject/build-files/src/Core/CMakeFiles/myproject.Core.dir/compiler_depend.internal".
Dependencies file "src/Core/CMakeFiles/myproject.Core.dir/Core.cc.o.d" is newer than depends file "/Users/oliverkarger/Desktop/myproject/build-files/src/Core/CMakeFiles/myproject.Core.dir/compiler_depend.internal".
Consolidate compiler generated dependencies of target myproject.Core
/Applications/Xcode.app/Contents/Developer/usr/bin/make -f src/Core/CMakeFiles/myproject.Core.dir/build.make src/Core/CMakeFiles/myproject.Core.dir/build
[ 75%] Linking CXX executable /Users/oliverkarger/Desktop/myproject/build/runtime/myproject.Core
cd /Users/oliverkarger/Desktop/myproject/build-files/src/Core && /usr/local/Cellar/cmake/3.24.2/bin/cmake -E cmake_link_script CMakeFiles/myproject.Core.dir/link.txt --verbose=1
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/c++ -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX13.0.sdk -Wl,-search_paths_first -Wl,-headerpad_max_install_names CMakeFiles/myproject.Core.dir/ConfigurationServiceProvider.cc.o CMakeFiles/myproject.Core.dir/Core.cc.o -o /Users/oliverkarger/Desktop/myproject/build/runtime/myproject.Core -L/Users/oliverkarger/Desktop/myproject/vcpkg/installed/x64-osx/debug/lib -L/Users/oliverkarger/Desktop/myproject/build/archive -lgRPC /Users/oliverkarger/Desktop/myproject/vcpkg/installed/x64-osx/debug/lib/libre2.a /Users/oliverkarger/Desktop/myproject/vcpkg/installed/x64-osx/debug/lib/libcares.a /Users/oliverkarger/Desktop/myproject/build/archive/libmyproject.Protos.a -lresolv -lgRPC -lprotobuf
ld: library not found for -lprotobuf
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make[3]: *** [/Users/oliverkarger/Desktop/myproject/build/runtime/myproject.Core] Error 1
make[2]: *** [src/Core/CMakeFiles/myproject.Core.dir/all] Error 2
make[1]: *** [src/Core/CMakeFiles/myproject.Core.dir/rule] Error 2
make: *** [myproject.Core] Error 2
Project: myproject.Protos is a self-built library dependency of myproject.core
CMake Files:
CMakeLists.txt
# Project Name
project("myproject")
# Options
set (CMAKE_CXX_STANDARD 20)
set (CMAKE_CXX_STANDARD_REQUIRED 17)
set (CMAKE_ARCHIVE_OUTPUT_DIRECTORY ../../../build/archive)
set (CMAKE_LIBRARY_OUTPUT_DIRECTORY ../../../build/lib)
set (CMAKE_RUNTIME_OUTPUT_DIRECTORY ../../../build/runtime)
# Application Metadata
set (CMAKE_PROJECT_VERSION_MAJOR 1)
set (CMAKE_PROJECT_VERSION_MINOR 0)
set (CMAKE_PROJECT_VERSION_PATCH 0)
set (CMAKE_PROJECT_VERSION_TWEAK 0)
# CMake Required Version
cmake_minimum_required(VERSION 3.24)
# Add VCPKG Toolchain
include (./vcpkg/scripts/buildsystems/vcpkg.cmake)
set (VCPKG_TARGET_TRIPLET x64-osx)
# Packages
find_package(gRPC CONFIG REQUIRED)
find_package(protobuf CONFIG REQUIRED)
find_package(OpenSSL REQUIRED)
# Third Party Includes
include_directories(./vcpkg/installed/x64-osx/include)
include_directories("include")
include_directories(${OpenCV_INCLUDE_DIRS})
# Doxygen
find_package(Doxygen)
if (DOXYGEN_FOUND)
# set input and output files
set(DOXYGEN_IN ${CMAKE_CURRENT_SOURCE_DIR}/docs/Doxyfile.in)
set(DOXYGEN_OUT ${CMAKE_CURRENT_BINARY_DIR}/Doxyfile)
# request to configure the file
configure_file(${DOXYGEN_IN} ${DOXYGEN_OUT} #ONLY)
message("Doxygen build started")
# note the option ALL which allows to build the docs together with the application
add_custom_target( docs ALL
COMMAND ${DOXYGEN_EXECUTABLE} ${DOXYGEN_OUT}
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
COMMENT "Generating API documentation with Doxygen"
VERBATIM )
else (DOXYGEN_FOUND)
message("Doxygen need to be installed to generate the doxygen documentation")
endif (DOXYGEN_FOUND)
# Google Test
include(FetchContent)
FetchContent_Declare(
googletest
URL https://github.com/google/googletest/archive/03597a01ee50ed33e9dfd640b249b4be3799d395.zip
)
# For Windows: Prevent overriding the parent project's compiler/linker settings
set(gtest_force_shared_crt ON CACHE BOOL "" FORCE)
FetchContent_MakeAvailable(googletest)
# Directories
add_subdirectory(src/Core)
add_subdirectory(src/CoreGUI)
add_subdirectory(src/CameraServer)
# add_subdirectory(src/TestRunner)
add_subdirectory(libraries/Math)
add_subdirectory(libraries/ImageProcessing)
add_subdirectory(libraries/DataStructures)
add_subdirectory(libraries/Protos)
add_subdirectory(libraries/CommonCC)
src/myproject.Core/CMakeLists.txt
file(GLOB_RECURSE SOURCES LIST_DIRECTORIES true *.cc *.h)
set(SOURCES ${SOURCES})
add_executable(myproject.Core ${SOURCES})
target_link_libraries(myproject.Core gRPC)
target_link_libraries(myproject.Core re2::re2 c-ares::cares)
target_link_libraries(myproject.Core myproject.Protos)
libraries/myproject.Protos/CMakeLists.txt
file(GLOB_RECURSE PROTOS LIST_DIRECTORIES true *.proto)
set(PROTOS ${PROTOS})
add_library(myproject.Protos ${PROTOS})
target_link_libraries(myproject.Protos gRPC)
target_link_libraries(myproject.Protos protobuf)
get_target_property(grpc_cpp_plugin_location gRPC::grpc_cpp_plugin LOCATION)
protobuf_generate(TARGET myproject.Protos LANGUAGE cpp)
protobuf_generate(TARGET myproject.Protos LANGUAGE grpc GENERATE_EXTENSIONS .grpc.pb.h .grpc.pb.cc PLUGIN "protoc-gen-grpc=${grpc_cpp_plugin_location}")
As far as I understand the issue is that cmake can't find the library file for protobuf. But I checked with vcpkg (is a submodule of my project) that the file and path both exist.
I also tried passing the path to the .a-file directly by using
target_link_library(myproject.(Core|Protos) vcpkg/installed/x64-osx/lib/libprotobuf.a)
but that didn't resolve my issues.
How to link libprotobuf properly?

Linking error on CMake and boost when compiling with MinGW on windows: undefined reference

I am trying to build a project on Windows using CMake and MinGW.
When using the command mingw32-make.exe I got link errors for any boost library (except for unit_test library), as for example:
"undefined reference to `boost::chrono::system_clock::now()'"
My boost installation is under C:\boost\ that contains both include and lib directories. I know that the boost installation is correct because if I use a handmade Makefile and I link the libraries with -l I can compile my project without any issue. So I guess the problem is related to how I link libraries in my CMakeLists.txt.
I tried these solution but they didn't work:
CMake Boost Static Libraries Undefined Reference boost::chrono::steady_clock::now()
Cmake with "undefined references" Despite Finding Boost Libs
Undefined reference to 'boost::system::generic_category()'?
This is the CMakeLists I am using, that correctly finds the libraries, as printed by Boost_DEBUG:
CMAKE_MINIMUM_REQUIRED(VERSION 3.23)
SET(CMAKE_CXX_STANDARD 11)
SET(CMAKE_CXX_STANDARD_REQUIRED ON)
SET(CMAKE_CXX_EXTENSIONS OFF)
SET(Boost_USE_STATIC_LIBS ON)
SET(Boost_DETAILED_FAILURE_MSG OFF)
SET(Boost_USE_MULTITHREADED ON)
SET(Boost_THREADAPI win32)
SET(BOOST_ROOT "C:\\boost")
SET(Boost_LIBRARY_DIR "C:\\boost\\lib")
SET(Boost_INCLUDE_DIR "C:/boost/include/boost-1_76")
SET(Boost_INCLUDE_DIRS "C:/boost/include/boost-1_76")
SET(Boost_USE_STATIC_RUNTIME OFF)
SET(Boost_DEBUG ON)
PROJECT(myproject LANGUAGES CXX)
find_package(Boost 1.76.0
COMPONENTS
log
system
timer
chrono
unit_test_framework
REQUIRED
)
add_executable(test_cmake test_cmake.cpp)
target_include_directories(test_cmake PRIVATE ${Boost_INCLUDE_DIRS})
target_include_directories(test_cmake PRIVATE ${Boost_LIBRARY_DIR})
target_link_libraries(test_cmake
${Boost_UNIT_TEST_FRAMEWORK_LIBRARY}
${Boost_LIBRARIES}
)
This is the simple main file that can be compiled and produces "undefined reference to `boost::chrono::system_clock::now()'" error.
#define BOOST_AUTO_TEST_MAIN
#define BOOST_TEST_DYN_LINK
#include <boost/test/included/unit_test.hpp>
#include <turtle/mock.hpp>
// #include <boost/chrono.hpp> // I tried this too but it didn't work as well
#include <boost/chrono/chrono.hpp>
/**
* #brief Test of boost chrono library
*/
BOOST_AUTO_TEST_CASE(test_boost_chrono) {
auto start_time = boost::chrono::system_clock::now();
int sleep_time = 1000;
// boost::this_thread::sleep_for(boost::chrono::milliseconds(sleep_time));
auto curr_time = boost::chrono::system_clock::now();
auto diff_time = boost::chrono::duration_cast<boost::chrono::milliseconds>(curr_time - start_time);
BOOST_CHECK(diff_time.count() > sleep_time);
int epsilon = 10;
BOOST_CHECK(diff_time.count() < (sleep_time + epsilon));
}
I think the problem is related to target_include_directories and target_link_libraries but I cannot understand which is the issue.
Do you have any suggestion?
Edit:
This is what I get when compiling with mingw32-make.exe VERBOSE=1:
PS C:\Workspace\test_cmake_project_cmake\build> mingw32-make.exe VERBOSE=1
"C:\Program Files\CMake\bin\cmake.exe" -SC:\Workspace\test_cmake_project_cmake -BC:\Workspace\test_cmake_project_cmake\build --check-build-system CMakeFiles\Makefile.cmake 0
"C:\Program Files\CMake\bin\cmake.exe" -E cmake_progress_start C:\Workspace\test_cmake_project_cmake\build\CMakeFiles C:\Workspace\test_cmake_project_cmake\build\\CMakeFiles\progress.marks
C:/MinGW/bin/mingw32-make -f CMakeFiles\Makefile2 all
mingw32-make[1]: Entering directory 'C:/Workspace/test_cmake_project_cmake/build'
C:/MinGW/bin/mingw32-make -f test_cmake\CMakeFiles\test_cmake.dir\build.make test_cmake/CMakeFiles/test_cmake.dir/depend
mingw32-make[2]: Entering directory 'C:/Workspace/test_cmake_project_cmake/build'
"C:\Program Files\CMake\bin\cmake.exe" -E cmake_depends "MinGW Makefiles" C:\Workspace\test_cmake_project_cmake C:\Workspace\test_cmake_project_cmake\test_cmake C:\Workspace\test_cmake_project_cmake\build C:\Workspace\test_cmake_project_cmake\build\test_cmake C:\Workspace\test_cmake_project_cmake\build\test_cmake\CMakeFiles\test_cmake.dir\DependInfo.cmake --color=
Dependencies file "test_cmake/CMakeFiles/test_cmake.dir/test_cmake.cpp.obj.d" is newer than depends file "C:/Workspace/test_cmake_project_cmake/build/test_cmake/CMakeFiles/test_cmake.dir/compiler_depend.internal".
Consolidate compiler generated dependencies of target test_cmake
mingw32-make[2]: Leaving directory 'C:/Workspace/test_cmake_project_cmake/build'
C:/MinGW/bin/mingw32-make -f test_cmake\CMakeFiles\test_cmake.dir\build.make test_cmake/CMakeFiles/test_cmake.dir/build
mingw32-make[2]: Entering directory 'C:/Workspace/test_cmake_project_cmake/build'
[ 50%] Linking CXX executable test_cmake.exe
cd /d C:\Workspace\test_cmake_project_cmake\build\test_cmake && "C:\Program Files\CMake\bin\cmake.exe" -E cmake_link_script CMakeFiles\test_cmake.dir\link.txt --verbose=1
"C:\Program Files\CMake\bin\cmake.exe" -E rm -f CMakeFiles\test_cmake.dir/objects.a
C:\MinGW\bin\ar.exe qc CMakeFiles\test_cmake.dir/objects.a #CMakeFiles\test_cmake.dir\objects1.rsp
C:\MinGW\bin\g++.exe -g -Wl,--whole-archive CMakeFiles\test_cmake.dir/objects.a -Wl,--no-whole-archive -o test_cmake.exe -Wl,--out-implib,libtest_cmake.dll.a -Wl,--major-image-version,0,--minor-image-version,0 #CMakeFiles\test_cmake.dir\linklibs.rsp
CMakeFiles\test_cmake.dir/objects.a(test_cmake.cpp.obj): In function `main':
C:/Workspace/test_cmake_project_cmake/test_cmake/test_cmake.cpp:6: undefined reference to `boost::chrono::system_clock::now()'
C:/Workspace/test_cmake_project_cmake/test_cmake/test_cmake.cpp:11: undefined reference to `boost::chrono::system_clock::now()'
collect2.exe: error: ld returned 1 exit status
mingw32-make[2]: *** [test_cmake\CMakeFiles\test_cmake.dir\build.make:99: test_cmake/test_cmake.exe] Error 1
mingw32-make[2]: Leaving directory 'C:/Workspace/test_cmake_project_cmake/build'
mingw32-make[1]: *** [CMakeFiles\Makefile2:97: test_cmake/CMakeFiles/test_cmake.dir/all] Error 2
mingw32-make[1]: Leaving directory 'C:/Workspace/test_cmake_project_cmake/build'
mingw32-make: *** [Makefile:90: all] Error 2
Edit:
It seems it was an issue of Visual Studio Code: maybe some variables were set with wrong values and CMake could not link boost.
Finally I have created a new project and I have used a minimal CMakeLists without setting any boost variable but BOOST_ROOT (SET(BOOST_ROOT "C:\\boost") and now it works!!
Unfortunately I did not find the real reason :(, but cleaning the project and starting from scretch helped! Thanks everybody!

Cross compiling for arm android

i want to build c++ library for my android device, more specific i want to build taglib to use with my Qt project, i have a music app that uses taglib, i build it for windows and ubuntu and it works as expected, but now i want to cross compile taglib to use on my android phone, i searched hours to find the solution, i found some article about toolchain file and manual cmake option but none of them worked for me,
i tried to set CMAKE_SYSTEM_NAME to android and set other Cmake option like below:
set(CMAKE_SYSTEM_NAME Android)
message("this is host system ${CMAKE_SYSTEM_NAME} ${CMAKE_HOST_SYSTEM_NAME} {CMAKE_SYSTEM_VERSION}")
set(CMAKE_SYSTEM_VERSION 21)
set(CMAKE_ANDROID_ARCH_ABI armeabi-v7a)
set(CMAKE_ANDROID_NDK /home/sub/Downloads/android/android-ndk-r20)
set(CMAKE_ANDROID_STL_TYPE gnustl_static)
and when i check the generated .so file with file command it show my system info like below:
libmylib.so: ELF 64-bit LSB shared object, x86-64, version 1 (SYSV), dynamically linked, BuildID[sha1]=4245460a39baea2cbd1f28c3a2fd8037b07fe995, with debug_info, not stripped
so can anyone give me some link or help to do this?
** update
my cmake command is like below:
cmake CMakeLists.txt -DCMAKE_SYSTEM_NAME=ANDROID -DCMAKE_TOOLCHAIN_FILE=/home/sub/Downloads/android-ndk-r21/build/cmake/android.toolchain.cmake
now i use new ndk and use it android.toolchain.cmake file but the output say it doesn't use it:
-- Configuring done
-- Generating done
CMake Warning:
Manually-specified variables were not used by the project:
CMAKE_TOOLCHAIN_FILE
-- Build files have been written to: /home/sub/Documents/Projects/cpp/build-for-android
my new CMakeLists.txt is :
cmake_minimum_required(VERSION 3.17)
project(addlib CXX)
# set(CMAKE_SYSTEM_NAME Android)
# message("this is host system ${CMAKE_SYSTEM_NAME} ${CMAKE_HOST_SYSTEM_NAME} ${CMAKE_SYSTEM_VERSION}")
# set(CMAKE_SYSTEM_VERSION 21)
# set(CMAKE_ANDROID_ARCH_ABI armeabi-v7a)
# set(CMAKE_ANDROID_NDK /home/sub/Downloads/android/android-ndk-r20)
# set(CMAKE_ANDROID_STL_TYPE gnustl_static)
set(BUILD_SHARED_LIBS true)
add_library(mylib main.cpp)
target_include_directories(mylib PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/include)
message(${CMAKE_CROSSCOMPILING})
install(TARGETS mylib DESTINATION ${CMAKE_CURRENT_SOURCE_DIR}/libs/)
make command output with verbose on is same as below:
/usr/local/bin/cmake -S/home/sub/Documents/Projects/cpp/build-for-android -B/home/sub/Documents/Projects/cpp/build-for-android --check-build-system CMakeFiles/Makefile.cmake 0
/usr/local/bin/cmake -E cmake_progress_start /home/sub/Documents/Projects/cpp/build-for-android/CMakeFiles /home/sub/Documents/Projects/cpp/build-for-android/CMakeFiles/progress.marks
make -f CMakeFiles/Makefile2 all
make[1]: Entering directory '/home/sub/Documents/Projects/cpp/build-for-android'
make -f CMakeFiles/mylib.dir/build.make CMakeFiles/mylib.dir/depend
make[2]: Entering directory '/home/sub/Documents/Projects/cpp/build-for-android'
cd /home/sub/Documents/Projects/cpp/build-for-android && /usr/local/bin/cmake -E cmake_depends "Unix Makefiles" /home/sub/Documents/Projects/cpp/build-for-android /home/sub/Documents/Projects/cpp/build-for-android /home/sub/Documents/Projects/cpp/build-for-android /home/sub/Documents/Projects/cpp/build-for-android /home/sub/Documents/Projects/cpp/build-for-android/CMakeFiles/mylib.dir/DependInfo.cmake --color=
make[2]: Leaving directory '/home/sub/Documents/Projects/cpp/build-for-android'
make -f CMakeFiles/mylib.dir/build.make CMakeFiles/mylib.dir/build
make[2]: Entering directory '/home/sub/Documents/Projects/cpp/build-for-android'
[ 50%] Linking CXX shared library libmylib.so
/usr/local/bin/cmake -E cmake_link_script CMakeFiles/mylib.dir/link.txt --verbose=1
/usr/bin/g++ -fPIC -g -shared -Wl,-soname,libmylib.so -o libmylib.so CMakeFiles/mylib.dir/main.cpp.o
make[2]: Leaving directory '/home/sub/Documents/Projects/cpp/build-for-android'
[100%] Built target mylib
make[1]: Leaving directory '/home/sub/Documents/Projects/cpp/build-for-android'
/usr/local/bin/cmake -E cmake_progress_start /home/sub/Documents/Projects/cpp/build-for-android/CMakeFiles 0
thank you

static linking with cmake fails [duplicate]

This question already has answers here:
cmake and libpthread
(4 answers)
Closed 3 years ago.
I am trying to link to a static c++ Library with cmake on ubuntu.
the library is installed here: /usr/local/lib/libfrnetlib.a and headers are located here: /usr/local/include/frnetlib
Thanks for any help!
my cmakelists.txt looks like this:
###### setup
cmake_minimum_required(VERSION 3.2)
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
###### names + sources
project(ws_server)
set(SOURCE_FILES ws_server.cpp TcpListener.h WebFrame.h WebSocket.h)
###### include
include_directories(/usr/local/lib)
include_directories(/usr/local/include/frnetlib)
link_directories(/usr/local/lib/libfrnetlib.a)
###### library
find_library(frnetlib NAMES libfrnetlib.a HINTS /usr/local/lib)
if(NOT frnetlib)
message([status] "
frnetlib library not found
")
else(frnetlib)
message([status] "---------------frnetlib library found :-)")
endif()
###### executable + linking
add_executable(${PROJECT_NAME} ${SOURCE_FILES})
target_link_libraries(${PROJECT_NAME} ${frnetlib})
install(TARGETS ${PROJECT_NAME} DESTINATION bin)
and the output of cmake like this:
> Executing task: if [ -d build ]; then rm -Rf build; fi && mkdir build && cd build && cmake -v -DCMAKE_BUILD_TYPE=release .. && cmake --build . -v <
-- The C compiler identification is GNU 5.4.0
-- The CXX compiler identification is GNU 5.4.0
-- Check for working C compiler: /usr/bin/cc
-- Check for working C compiler: /usr/bin/cc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Check for working CXX compiler: /usr/bin/c++
-- Check for working CXX compiler: /usr/bin/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
[status]--------------------------------------frnetlib library found :-)
-- Configuring done
-- Generating done
-- Build files have been written to: /home/pandapc/vscode/own_ws_server/build
/usr/local/bin/cmake -S/home/pandapc/vscode/own_ws_server -B/home/pandapc/vscode/own_ws_server/build --check-build-system CMakeFiles/Makefile.cmake 0
/usr/local/bin/cmake -E cmake_progress_start /home/pandapc/vscode/own_ws_server/build/CMakeFiles /home/pandapc/vscode/own_ws_server/build/CMakeFiles/progress.marks
/usr/bin/make -f CMakeFiles/Makefile2 all
make[1]: Entering directory '/home/pandapc/vscode/own_ws_server/build'
/usr/bin/make -f CMakeFiles/ws_server.dir/build.make CMakeFiles/ws_server.dir/depend
make[2]: Entering directory '/home/pandapc/vscode/own_ws_server/build'
cd /home/pandapc/vscode/own_ws_server/build && /usr/local/bin/cmake -E cmake_depends "Unix Makefiles" /home/pandapc/vscode/own_ws_server /home/pandapc/vscode/own_ws_server /home/pandapc/vscode/own_ws_server/build /home/pandapc/vscode/own_ws_server/build /home/pandapc/vscode/own_ws_server/build/CMakeFiles/ws_server.dir/DependInfo.cmake --color=
Dependee "/home/pandapc/vscode/own_ws_server/build/CMakeFiles/ws_server.dir/DependInfo.cmake" is newer than depender "/home/pandapc/vscode/own_ws_server/build/CMakeFiles/ws_server.dir/depend.internal".
Dependee "/home/pandapc/vscode/own_ws_server/build/CMakeFiles/CMakeDirectoryInformation.cmake" is newer than depender "/home/pandapc/vscode/own_ws_server/build/CMakeFiles/ws_server.dir/depend.internal".
Scanning dependencies of target ws_server
make[2]: Leaving directory '/home/pandapc/vscode/own_ws_server/build'
/usr/bin/make -f CMakeFiles/ws_server.dir/build.make CMakeFiles/ws_server.dir/build
make[2]: Entering directory '/home/pandapc/vscode/own_ws_server/build'
[ 50%] Building CXX object CMakeFiles/ws_server.dir/ws_server.cpp.o
/usr/bin/c++ -I/usr/local/lib -I/usr/local/include/frnetlib -std=c++11 -O3 -DNDEBUG -std=gnu++11 -o CMakeFiles/ws_server.dir/ws_server.cpp.o -c /home/pandapc/vscode/own_ws_server/ws_server.cpp
[100%] Linking CXX executable ws_server
/usr/local/bin/cmake -E cmake_link_script CMakeFiles/ws_server.dir/link.txt --verbose=1
/usr/bin/c++ -std=c++11 -O3 -DNDEBUG -rdynamic CMakeFiles/ws_server.dir/ws_server.cpp.o -o ws_server -L/usr/local/lib/libfrnetlib.a -Wl,-rpath,/usr/local/lib/libfrnetlib.a: /usr/local/lib/libfrnetlib.a
CMakeFiles/ws_server.dir/ws_server.cpp.o: In function `main':
ws_server.cpp:(.text.startup+0x18e): undefined reference to `pthread_create'
collect2: error: ld returned 1 exit status
CMakeFiles/ws_server.dir/build.make:84: recipe for target 'ws_server' failed
make[2]: *** [ws_server] Error 1
make[2]: Leaving directory '/home/pandapc/vscode/own_ws_server/build'
CMakeFiles/Makefile2:72: recipe for target 'CMakeFiles/ws_server.dir/all' failed
make[1]: *** [CMakeFiles/ws_server.dir/all] Error 2
make[1]: Leaving directory '/home/pandapc/vscode/own_ws_server/build'
Makefile:129: recipe for target 'all' failed
make: *** [all] Error 2
The terminal process terminated with exit code: 2
Terminal will be reused by tasks, press any key to close it.
thanks some programmer dude,
the solution is either to set this flag:
#set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -pthread")
or use this cmakelists.txt
###### setup
cmake_minimum_required(VERSION 3.2)
set(CMAKE_CXX_STANDARD 11)
#set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -pthread")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ")
###### names + sources
project(ws_server)
set(SOURCE_FILES ws_server.cpp TcpListener.h WebFrame.h WebSocket.h)
###### library
find_library(frnetlib NAMES libfrnetlib.a HINTS /usr/local/lib)
find_package( Threads )
if(NOT frnetlib)
message([status] "
frnetlib library not found!
")
else(frnetlib)
message([status] "--------------------------------------frnetlib library found :-)")
endif()
###### executable + linking
add_executable(${PROJECT_NAME} ${SOURCE_FILES})
target_link_libraries(${PROJECT_NAME} ${frnetlib} ${CMAKE_THREAD_LIBS_INIT})
install(TARGETS ${PROJECT_NAME} DESTINATION bin)

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")