Cmake error undefined reference to `pthread_create' - c++

I make a test for cmake FindThreads. Here is my source code test.cpp and CMakeLists.txt:
#include <pthread.h>
void* test_func(void* data)
{
return data;
}
int main(void)
{
pthread_t thread;
pthread_create(&thread, NULL, test_func, NULL);
pthread_detach(thread);
pthread_cancel(thread);
pthread_join(thread, NULL);
pthread_atfork(NULL, NULL, NULL);
pthread_exit(NULL);
return 0;
}
cmake_minimum_required(VERSION 3.5)
project(test C CXX)
set(CMAKE_THREAD_PREFER_PTHREAD ON)
set(THREADS_PREFER_PTHREAD_FLAG ON)
find_package(Threads REQUIRED)
add_executable(test test.cpp)
if(TARGET Threads::Threads)
target_link_libraries(test PRIVATE Threads::Threads)
endif()
when I run:
cmake .
I get the outpu:
-- Looking for pthread.h
-- Looking for pthread.h - found
-- Performing Test CMAKE_HAVE_LIBC_PTHREAD
-- Performing Test CMAKE_HAVE_LIBC_PTHREAD - Failed
-- Check if compiler accepts -pthread
-- Check if compiler accepts -pthread - yes
-- Found Threads: TRUE
then I check CMakeError.txt, find that:
gmake[1]: Entering directory '/home/hye/tmp/cmake-error/CMakeFiles/CMakeTmp'
Building C object CMakeFiles/cmTC_55ab6.dir/src.c.o
/usr/bin/clang -DCMAKE_HAVE_LIBC_PTHREAD -o CMakeFiles/cmTC_55ab6.dir/src.c.o -c /home/hye/tmp/cmake-error/CMakeFiles/CMakeTmp/src.c
Linking C executable cmTC_55ab6
/usr/bin/cmake -E cmake_link_script CMakeFiles/cmTC_55ab6.dir/link.txt --verbose=1
/usr/bin/clang -DCMAKE_HAVE_LIBC_PTHREAD CMakeFiles/cmTC_55ab6.dir/src.c.o -o cmTC_55ab6
/usr/bin/ld: CMakeFiles/cmTC_55ab6.dir/src.c.o: in function `main':
src.c:(.text+0x35): undefined reference to `pthread_create'
/usr/bin/ld: src.c:(.text+0x41): undefined reference to `pthread_detach'
/usr/bin/ld: src.c:(.text+0x4d): undefined reference to `pthread_cancel'
/usr/bin/ld: src.c:(.text+0x5f): undefined reference to `pthread_join'
clang-9: error: linker command failed with exit code 1 (use -v to see invocation)
gmake[1]: *** [CMakeFiles/cmTC_55ab6.dir/build.make:107: cmTC_55ab6] Error 1
gmake[1]: Leaving directory '/home/hye/tmp/cmake-error/CMakeFiles/CMakeTmp'
gmake: *** [Makefile:141: cmTC_55ab6/fast] Error 2
My question is why performing Test CMAKE_HAVE_LIBC_PTHREAD - Failed, and since it failed, did it
really find Threads, I am totally confused. Thanks for any replying!

There is a little reason to examine CMakeError.txt until CMake reports about not-working compiler or CMake reports about unavailable feature, which is detected by probe compilation/linking, but you expect that feature to be available.
In your case you have successful CMake configuration (look at the last lines in CMake output), and you have successfully detected Threads-related library(see below). No reasons to worry and no reasons to look into CMakeError.txt.
did it really find Threads?
Yes, Threads are found. E.g., CMake clearly states
-- Found Threads: TRUE
Other ways for deduce, that Threads has been found:
You use REQUIRED keyword with find_package(Threads). Would Threads not found, CMake will report about an error and terminate configuration.
You may check Threads_FOUND variable after the find_package(Threads) call. (With REQUIRED keyword this check is redudant).
You may check Threads::Threads target after the find_package(Threads) call. (With REQUIRED keyword this check is redudant).

FindThreads.cmake tries it best to determine if the compiler supports pthreads as a library, a compile time switch, or a link time switch, etcetera.
The failure you are seeing is if a pthreads library exists. It doesn't. Instead you are supposed to compile the files with -pthreads which is a compile time switch your compiler does accept. This is align with the clang documentation which shows that -pthreads is a compiler option, not a linker option. https://clang.llvm.org/docs/ClangCommandLineReference.html#compilation-flags
So, did it find Threads? Not exactly, there is nothing to find. Did it determine how to use pthreads? Yes.
You didn't show any errors when building the target test with test.cpp. You only posted errors one would expect when pthreads is not implemented as a separate library.

Related

C++ Tensorflow Lite undefined referance

I'm trying to build a project using Tensorflow Lite on my debian 11 machine, however it says I'm getting an undefined reference on some functions.
Here is the code I'm trying to run:
// Works
std::unique_ptr<tflite::FlatBufferModel> model =
tflite::FlatBufferModel::BuildFromFile(filename);
TFLITE_MINIMAL_CHECK(model != nullptr);
// Undefined referance:
tflite::ops::builtin::BuiltinOpResolver resolver;
std::unique_ptr<tflite::Interpreter> interpreter;
tflite::InterpreterBuilder(*model, resolver)(&interpreter);
The first few lines in on itself works fine. When I add the lines below starting from BuiltinOpResolver I'm getting the following error when running make:
[ 50%] Linking CXX executable TFLiteCheck
/usr/bin/ld: CMakeFiles/TFLiteCheck.dir/main.cpp.o: in function `main':
main.cpp:(.text+0x106): undefined reference to `tflite::InterpreterBuilder::InterpreterBuilder(tflite::FlatBufferModel const&, tflite::OpResolver const&)'
/usr/bin/ld: main.cpp:(.text+0x11f): undefined reference to `tflite::InterpreterBuilder::operator()(std::unique_ptr<tflite::Interpreter, std::default_delete<tflite::Interpreter> >*)'
/usr/bin/ld: main.cpp:(.text+0x12e): undefined reference to `tflite::InterpreterBuilder::~InterpreterBuilder()'
/usr/bin/ld: main.cpp:(.text+0x19e): undefined reference to `tflite::InterpreterBuilder::~InterpreterBuilder()'
/usr/bin/ld: CMakeFiles/TFLiteCheck.dir/main.cpp.o: in function `std::default_delete<tflite::Interpreter>::operator()(tflite::Interpreter*) const':
main.cpp:(.text._ZNKSt14default_deleteIN6tflite11InterpreterEEclEPS1_[_ZNKSt14default_deleteIN6tflite11InterpreterEEclEPS1_]+0x1e): undefined reference to `tflite::Interpreter::~Interpreter()'
collect2: error: ld returned 1 exit status
make[2]: *** [CMakeFiles/TFLiteCheck.dir/build.make:104: TFLiteCheck] Error 1
make[1]: *** [CMakeFiles/Makefile2:95: CMakeFiles/TFLiteCheck.dir/all] Error 2
make: *** [Makefile:103: all] Error 2
I've tried [this answer][1] but it's on an arm architecture while I'm on an intel chip, and when I try it regardless I'm getting a completely different error that I've never seen before.
I've followed these steps to setup TFLite:
Got the source code from tenserflow github page
Got bazel version 3.7.2 (bazel-3.7.2-linux-x86_64)
Ran python3 ./configure.py setting everything to default and opting to say n to everything
Ran bazel build -c opt //tensorflow/lite:libtensorflowlite.so --local_ram_resources=10240 --config=noaws(Tried it with and without --local_ram_resources=10240 --config=noaws params)
Move the .so file to the designated file, right next to tenserflow include files.
Ran cmake .. and make on the build folder with the following CMake file:
cmake_minimum_required(VERSION 3.17)
project(TFLiteCheck)
set(CMAKE_CXX_STANDARD 14)
# include has 2 subdirectories: tensorflow and flatbuffers
INCLUDE_DIRECTORIES(${CMAKE_CURRENT_SOURCE_DIR}/third-party/tflite-dist/include/)
# lib has 1 file: libtensorflowlite.so
ADD_LIBRARY(tensorflowlite SHARED IMPORTED)
set_property(TARGET tensorflowlite PROPERTY IMPORTED_LOCATION ${CMAKE_CURRENT_SOURCE_DIR}/third-party/tflite-dist/libs/linux_x64/libtensorflowlite.so)
add_executable(TFLiteCheck main.cpp)
target_link_libraries(TFLiteCheck PUBLIC tensorflowlite)
And running make results in the above error. What could be the problem? Is there a better way to setup tenserflow? Like I've said only running FlatBufferModel works just fine.
Update:
By removing the -J flag from the official build instructions I've managed to built the project propperly. However when I use the official cmake example:
cmake_minimum_required(VERSION 3.16)
project(minimal C CXX)
set(TENSORFLOW_SOURCE_DIR "" CACHE PATH
"Directory that contains the TensorFlow project" )
if(NOT TENSORFLOW_SOURCE_DIR)
get_filename_component(TENSORFLOW_SOURCE_DIR
"${CMAKE_CURRENT_LIST_DIR}/../../../../" ABSOLUTE)
endif()
add_subdirectory(
"${TENSORFLOW_SOURCE_DIR}/user/tensorflow_src/tensorflow/lite"
"${CMAKE_CURRENT_BINARY_DIR}/tensorflow-lite" EXCLUDE_FROM_ALL)
add_executable(minimal main.cpp)
target_link_libraries(minimal tensorflow-lite)
When I run this with my example main.cpp using cmake . provided above, I'm getting this output and the terminal is stuck like this, without resolving:
user#debian:~/Desktop/SmartAlpha/tf_test$ cmake .
-- Setting build type to Release, for debug builds use'-DCMAKE_BUILD_TYPE=Debug'.
CMake Warning at abseil-cpp/CMakeLists.txt:70 (message):
A future Abseil release will default ABSL_PROPAGATE_CXX_STD to ON for CMake
3.8 and up. We recommend enabling this option to ensure your project still
builds correctly.
-- Looking for pthread.h
-- Looking for pthread.h - found
-- Performing Test CMAKE_HAVE_LIBC_PTHREAD
-- Performing Test CMAKE_HAVE_LIBC_PTHREAD - Failed
-- Looking for pthread_create in pthreads
-- Looking for pthread_create in pthreads - not found
-- Looking for pthread_create in pthread
-- Looking for pthread_create in pthread - found
-- Found Threads: TRUE
It's not frozen or anything, it just stays like this untill I hit ctrl+c to break it, without ever finishing.
Update 2:
The compilation finished with this error:
user#debian:~/Desktop/SmartAlpha/tf_test$ cmake .
-- Setting build type to Release, for debug builds use'-DCMAKE_BUILD_TYPE=Debug'.
CMake Warning at abseil-cpp/CMakeLists.txt:70 (message):
A future Abseil release will default ABSL_PROPAGATE_CXX_STD to ON for CMake
3.8 and up. We recommend enabling this option to ensure your project still
builds correctly.
-- Looking for pthread.h
-- Looking for pthread.h - found
-- Performing Test CMAKE_HAVE_LIBC_PTHREAD
-- Performing Test CMAKE_HAVE_LIBC_PTHREAD - Failed
-- Looking for pthread_create in pthreads
-- Looking for pthread_create in pthreads - not found
-- Looking for pthread_create in pthread
-- Looking for pthread_create in pthread - found
-- Found Threads: TRUE
CMake Error at eigen/CMakeLists.txt:36 (message):
In-source builds not allowed. Please make a new directory (called a build
directory) and run CMake from there. You may need to remove
CMakeCache.txt.
-- Configuring incomplete, errors occurred!
See also "/home/user/Desktop/tf_test/CMakeFiles/CMakeOutput.log".
See also "/home/user/Desktop/tf_test/CMakeFiles/CMakeError.log".
Which cmake cache is it talking about? The one in my projects directory or the one inside my tensorflow build?
Am I missing spmething?
[1]: Tensorflow Lite error undefined reference to `tflite::DefaultErrorReporter()'

CMake can't find Boost_LIBRARIES variable

Final Edit: Solved.
Guys the problem occured because of the "WinSock32".
I've added
target_link_libraries(modernC__ -lws2_32)
and the code has been builded.
I have been using the Boost library on CLion in Ubuntu for about 1 year. But this week I also decided to install it on the Windows operating system.
so I downloaded the Boost library on GitHub and installed the Boost library using Find.Boost first and then "Find.Boost".
After writing the necessary commands in CMake settings section in CLion, I noticed that the variable $ {Boost_LIBRARIES} is empty.
When I don't use the message () function, the "CMake" part of the project does not give an error, but I get the error "undefined reference" after "build". Below are the CMake commands I wrote on CLion and the errors I received.
cmake_minimum_required(VERSION 3.16)
project(modernC__)
set(CMAKE_CXX_STANDARD 17)
find_package(Boost 1.66.0)
message(${Boost_INCLUDE_DIR})
message(${Boost_FOUND})
message(${Boost_LIBRARY_DIRS})
IF (Boost_FOUND)
include_directories(${Boost_INCLUDE_DIR})
add_executable(modernC__
main.cpp
#concurrencyExampleOne.cpp
#adapterExampleOne.cpp
)
target_link_libraries(modernC__ ${Boost_LIBRARIES})
message(${Boost_LIBRARIES})
endif()
My output is:
"C:\Program Files\JetBrains\CLion 2020.1.1\bin\cmake\win\bin\cmake.exe" -DCMAKE_BUILD_TYPE=Debug -G "CodeBlocks - MinGW Makefiles" C:\Users\Berke\CLionProjects\modernC++
C:/boost/include/boost-1_66
TRUE
C:/boost/lib
CMake Error at CMakeLists.txt:22 (message):
message called with incorrect number of arguments
-- Configuring incomplete, errors occurred!
In this case, obviously Cmake is currently unable to find the .lib files that should link to my code.
My question is; How can I permanently drop .lib files into this variable, or is there any other way to do this?
If I do not use "message(${Boost_LIBRARIES}" so the compiler gives me this error;
[ 50%] Linking CXX executable modernC__.exe
CMakeFiles\modernC__.dir/objects.a(main.cpp.obj): In function `ZNK5boost6system14error_category12std_category10equivalentEiRKSt15error_condition':
C:/boost/include/boost-1_66/boost/system/error_code.hpp:676: undefined reference to `boost::system::generic_category()'
C:/boost/include/boost-1_66/boost/system/error_code.hpp:679: undefined reference to `boost::system::generic_category()'
CMakeFiles\modernC__.dir/objects.a(main.cpp.obj): In function `ZNK5boost6system14error_category12std_category10equivalentERKSt10error_codei':
C:/boost/include/boost-1_66/boost/system/error_code.hpp:706: undefined reference to `boost::system::generic_category()'
C:/boost/include/boost-1_66/boost/system/error_code.hpp:709: undefined reference to `boost::system::generic_category()'
C:/boost/include/boost-1_66/boost/system/error_code.hpp:721: undefined reference to `boost::system::generic_category()'
CMakeFiles\modernC__.dir/objects.a(main.cpp.obj): In function `ZN5boost4asio5error19get_system_categoryEv':
C:/boost/include/boost-1_66/boost/asio/error.hpp:230: undefined reference to `boost::system::system_category()'
CMakeFiles\modernC__.dir/objects.a(main.cpp.obj): In function `ZN5boost4asio6detail17winsock_init_base7startupERNS2_4dataEhh':
C:/boost/include/boost-1_66/boost/asio/detail/impl/winsock_init.ipp:39: undefined reference to `_imp__WSAStartup#8'
CMakeFiles\modernC__.dir/objects.a(main.cpp.obj): In function `ZN5boost4asio6detail17winsock_init_base7cleanupERNS2_4dataE':
C:/boost/include/boost-1_66/boost/asio/detail/impl/winsock_init.ipp:56: undefined reference to `_imp__WSACleanup#0'
collect2.exe: error: ld returned 1 exit status
mingw32-make.exe[3]: *** [CMakeFiles\modernC__.dir\build.make:86: modernC__.exe] Error 1
mingw32-make.exe[2]: *** [CMakeFiles\Makefile2:75: CMakeFiles/modernC__.dir/all] Error 2
mingw32-make.exe[1]: *** [CMakeFiles\Makefile2:82: CMakeFiles/modernC__.dir/rule] Error 2
mingw32-make.exe: *** [Makefile:117: modernC__] Error 2
I think, you should specify boost libraries (as components) explicitly.
And you should add libraries to target.
Like this:
find_package(Boost COMPONENTS filesystem system locale context REQUIRED)
...
target_link_libraries(${PROJECT_NAME}
...
${Boost_FILESYSTEM_LIBRARY}
${Boost_SYSTEM_LIBRARY}
${Boost_LOCALE_LIBRARY}
${Boost_CONTEXT_LIBRARY}
...
)

Linking boost_numpy3 with CMAKE

TL;DR When updating from CMake 3.10 to CMake 3.11.1 on archlinux, the following configuration line:
find_package(Boost COMPONENTS python3 COMPONENTS numpy3 REQUIRED)
leads to CMake linking against 3 different libraries
-- Boost version: 1.66.0
-- Found the following Boost libraries:
-- python3
-- numpy3
-- python
instead of the previous behaviour:
-- Boost version: 1.66.0
-- Found the following Boost libraries:
-- python3
-- numpy3
resulting in a linker error.
I use CMake to build a piece of software that relies on Boost python, and, since a couple of days ago, it seems that the line
find_package(Boost COMPONENTS numpy3 REQUIRED)
is no longer sufficient for CMake to understand that it should link the program against the Boost python3 library, and it uses the Boost library python instead.
Here is a minimal working example to reproduce what I am talking about.
test.cpp
#include <iostream>
using namespace std;
int main()
{
cout << "Hello, world!" << endl;
}
CMakeList.txt
set(CMAKE_VERBOSE_MAKEFILE ON)
find_package(PythonLibs 3 REQUIRED)
find_package(Boost COMPONENTS numpy3 REQUIRED)
add_executable (test test.cpp)
target_link_libraries(test ${Boost_LIBRARIES} ${PYTHON_LIBRARIES})
With this configuration of CMake, a linker error will occur, and the error persists when I change the line adding numpy to
find_package(Boost COMPONENTS python3 COMPONENTS numpy3 REQUIRED)
Here is the result of cmake . && make:
/home/rastapopoulos/test $ cmake .
-- Boost version: 1.66.0
-- Found the following Boost libraries:
-- numpy3
-- python
CMake Warning (dev) in CMakeLists.txt:
No cmake_minimum_required command is present. A line of code such as
cmake_minimum_required(VERSION 3.11)
should be added at the top of the file. The version specified may be lower
if you wish to support older CMake versions for this project. For more
information run "cmake --help-policy CMP0000".
This warning is for project developers. Use -Wno-dev to suppress it.
-- Configuring done
-- Generating done
-- Build files have been written to: /home/rastapopoulos/test
/home/rastapopoulos/test $ make
/usr/bin/cmake -H/home/rastapopoulos/test -B/home/rastapopoulos/test --check-build-system CMakeFiles/Makefile.cmake 0
/usr/bin/cmake -E cmake_progress_start /home/rastapopoulos/test/CMakeFiles /home/rastapopoulos/test/CMakeFiles/progress.marks
make -f CMakeFiles/Makefile2 all
make[1]: Entering directory '/home/rastapopoulos/test'
make -f CMakeFiles/test.dir/build.make CMakeFiles/test.dir/depend
make[2]: Entering directory '/home/rastapopoulos/test'
cd /home/rastapopoulos/test && /usr/bin/cmake -E cmake_depends "Unix Makefiles" /home/rastapopoulos/test /home/rastapopoulos/test /home/rastapopoulos/test /home/rastapopoulos/test /home/rastapopoulos/test/CMakeFi
les/test.dir/DependInfo.cmake --color=
make[2]: Leaving directory '/home/rastapopoulos/test'
make -f CMakeFiles/test.dir/build.make CMakeFiles/test.dir/build
make[2]: Entering directory '/home/rastapopoulos/test'
[ 50%] Linking CXX executable test
/usr/bin/cmake -E cmake_link_script CMakeFiles/test.dir/link.txt --verbose=1
/usr/bin/c++ -rdynamic CMakeFiles/test.dir/test.o -o test -lboost_numpy3 -lboost_python -lpython3.6m
/usr/lib/gcc/x86_64-pc-linux-gnu/7.3.1/../../../../lib/libboost_python.so: undefined reference to `PyString_Size'
/usr/lib/gcc/x86_64-pc-linux-gnu/7.3.1/../../../../lib/libboost_python.so: undefined reference to `PyUnicodeUCS4_FromEncodedObject'
/usr/lib/gcc/x86_64-pc-linux-gnu/7.3.1/../../../../lib/libboost_python.so: undefined reference to `PyFile_FromString'
/usr/lib/gcc/x86_64-pc-linux-gnu/7.3.1/../../../../lib/libboost_python.so: undefined reference to `PyString_Type'
/usr/lib/gcc/x86_64-pc-linux-gnu/7.3.1/../../../../lib/libboost_python.so: undefined reference to `PyInt_Type'
/usr/lib/gcc/x86_64-pc-linux-gnu/7.3.1/../../../../lib/libboost_python.so: undefined reference to `PyString_FromString'
/usr/lib/gcc/x86_64-pc-linux-gnu/7.3.1/../../../../lib/libboost_python.so: undefined reference to `PyUnicodeUCS4_AsWideChar'
/usr/lib/gcc/x86_64-pc-linux-gnu/7.3.1/../../../../lib/libboost_python.so: undefined reference to `PyString_FromStringAndSize'
/usr/lib/gcc/x86_64-pc-linux-gnu/7.3.1/../../../../lib/libboost_python.so: undefined reference to `Py_InitModule4_64'
/usr/lib/gcc/x86_64-pc-linux-gnu/7.3.1/../../../../lib/libboost_python.so: undefined reference to `PyString_FromFormat'
/usr/lib/gcc/x86_64-pc-linux-gnu/7.3.1/../../../../lib/libboost_python.so: undefined reference to `PyNumber_Divide'
/usr/lib/gcc/x86_64-pc-linux-gnu/7.3.1/../../../../lib/libboost_python.so: undefined reference to `PyNumber_InPlaceDivide'
/usr/lib/gcc/x86_64-pc-linux-gnu/7.3.1/../../../../lib/libboost_python.so: undefined reference to `PyInt_AsLong'
/usr/lib/gcc/x86_64-pc-linux-gnu/7.3.1/../../../../lib/libboost_python.so: undefined reference to `PyString_InternFromString'
/usr/lib/gcc/x86_64-pc-linux-gnu/7.3.1/../../../../lib/libboost_python.so: undefined reference to `PyClass_Type'
/usr/lib/gcc/x86_64-pc-linux-gnu/7.3.1/../../../../lib/libboost_python.so: undefined reference to `PyString_AsString'
/usr/lib/gcc/x86_64-pc-linux-gnu/7.3.1/../../../../lib/libboost_python.so: undefined reference to `PyInt_FromLong'
/usr/lib/gcc/x86_64-pc-linux-gnu/7.3.1/../../../../lib/libboost_python.so: undefined reference to `PyFile_AsFile'
collect2: error: ld returned 1 exit status
make[2]: *** [CMakeFiles/test.dir/build.make:90: test] Error 1
make[2]: Leaving directory '/home/rastapopoulos/test'
make[1]: *** [CMakeFiles/Makefile2:71: CMakeFiles/test.dir/all] Error 2
make[1]: Leaving directory '/home/rastapopoulos/test'
make: *** [Makefile:87: all] Error 2
Has anyone experienced a similar problem and managed to solve it? I use cmake 3.11.1, boost 1.66.0-2, and run an updated version of Archlinux.
This bug is due to an invalid dependency description in FindBoost.cmake
set(_Boost_NUMPY_DEPENDENCIES python)
This has been fixed at https://github.com/Kitware/CMake/commit/c747d4ccb349f87963a8d1da69394bc4db6b74ed
Please use latest one, or you can rewrite it manually:
set(_Boost_NUMPY_DEPENDENCIES python${component_python_version})
CMake 3.10 does not properly support Boost 1.66. The Boost dependencies are hard-coded and if they chance, CMake has to adopt.
Delete the build directory and reconfigure. The configure step uses cached variables which prevents re-detection with the newer routines.

CMake does not properly find CUDA library

I'm trying to build a program that requires CUDA. To the CMake script I supply:
cmake -D CUDA_TOOLKIT_ROOT_DIR=/usr/local/cuda ..
CUDA is found and CMake runs normally:
staudt ~/workspace/clutbb/cluster/build $ cmake -D CUDA_TOOLKIT_ROOT_DIR=/usr/local/cuda ..
-- Found CUDA: /usr/local/cuda (found version "6.5")
-- Found Intel TBB
-- Boost version: 1.56.0
-- Found the following Boost libraries:
-- iostreams
-- program_options
-- Looking for include file pthread.h
-- Looking for include file pthread.h - found
-- Looking for pthread_create
-- Looking for pthread_create - not found
-- Looking for pthread_create in pthreads
-- Looking for pthread_create in pthreads - not found
-- Looking for pthread_create in pthread
-- Looking for pthread_create in pthread - found
-- Found Threads: TRUE
-- Could NOT find SDL (missing: SDL_LIBRARY SDL_INCLUDE_DIR)
-- Configuring done
-- Generating done
-- Build files have been written to: /home/i11/staudt/workspace/clutbb/cluster/build
But then the linker step fails:
staudt ~/workspace/clutbb/cluster/build $ make
[ 69%] Built target cluster
Linking CXX executable clu
CMakeFiles/clu.dir/clu.cpp.o: In function `initCUDA(int&, CUctx_st*&, int const&)':
clu.cpp:(.text+0x517): undefined reference to `cuInit'
clu.cpp:(.text+0x52b): undefined reference to `cuDeviceGet'
clu.cpp:(.text+0x53f): undefined reference to `cuCtxCreate_v2'
clu.cpp:(.text+0x559): undefined reference to `cuDeviceGetName'
clu.cpp:(.text+0x55e): undefined reference to `cuCtxSynchronize'
CMakeFiles/clu.dir/clu.cpp.o: In function `exitCUDA(int&, CUctx_st*&)':
clu.cpp:(.text+0x684): undefined reference to `cuCtxDestroy_v2'
CMakeFiles/clu.dir/clu.cpp.o: In function `main':
clu.cpp:(.text.startup+0x1092): undefined reference to `cuCtxDestroy_v2'
clu.cpp:(.text.startup+0x10d1): undefined reference to `cuCtxSynchronize'
clu.cpp:(.text.startup+0x10e1): undefined reference to `cuCtxSynchronize'
collect2: error: ld returned 1 exit status
make[2]: *** [bin/clu] Fehler 1
make[1]: *** [bin/CMakeFiles/clu.dir/all] Fehler 2
make: *** [all] Fehler 2
The required library is at /usr/local/cuda/lib64/stubs/libcuda.so, but how can I point that out to cmake or make?
In the archive you have now posted, there are multiple project hierarchies. The actual error you have posted in the question is occurring during compile and linking of the clu project based on clu.cpp in the clutbb/cluster/bin directory.
In this same directory, there is a CMakeLists.txt file. This file governs this particular level of the project hierarchy.
In this particular CMakeLists.txt file, there is the following section:
cuda_add_executable(clu clu.cpp)
target_link_libraries(clu ${CUDA_LIBRARY} ${TBB_LIBRARY} ${Boost_LIBRARIES} rt)
target_link_libraries(clu cluster)
Try modifying the middle line above to:
target_link_libraries(clu ${CUDA_LIBRARY} ${TBB_LIBRARY} ${Boost_LIBRARIES} rt cuda)
This should fix the missing -lcuda in the linker command line. It may still be necessary to give it the path to libcuda.so on your machine, but it may not be necessary, depending on how your machine environment is set up.
The correct way of doing this on CMake 3.17+ is to use the FindCUDAToolkit module, like so:
find_package(CUDAToolkit REQUIRED)
target_link_libraries(my_target PRIVATE CUDA::cudart CUDA::cuda_driver)
The CUDA::cuda_driver target is equivalent to -lcuda when the linker would find it, and is otherwise an absolute path to the correct library. You should avoid adding system libraries via target_link_libraries to ensure portability.
If you have an older version (CMake 3.0+), you can use the (now-deprecated) FindCUDA module to imitate the 3.17 module, like so:
find_package(CUDA REQUIRED)
# Do what the new package does
find_library(CUDA_DRIVER_LIBRARY
NAMES cuda_driver cuda
HINTS ${CUDA_TOOLKIT_ROOT_DIR}
ENV CUDA_PATH
PATH_SUFFIXES nvidia/current lib64 lib/x64 lib)
if (NOT CUDA_DRIVER_LIBRARY)
# Don't try any stub directories until we have exhausted all other search locations.
find_library(CUDA_DRIVER_LIBRARY
NAMES cuda_driver cuda
HINTS ${CUDA_TOOLKIT_ROOT_DIR}
ENV CUDA_PATH
PATH_SUFFIXES lib64/stubs lib/x64/stubs lib/stubs stubs)
endif ()
mark_as_advanced(CUDA_DRIVER_LIBRARY)
##
target_include_directories(my_target PRIVATE ${CUDA_INCLUDE_DIRS})
target_link_libraries(my_target PRIVATE ${CUDA_LIBRARIES} ${CUDA_DRIVER_LIBRARY})
This is adapted from the official sources, here.
The C++ file with host calls doesnt know it needs to link to libcudart. You have to explicitly set dependencies for the file/binary that file is in, eg.
target_link_libraries(clu ${CUDA_LIBRARIES})
The above response is slightly wrong. It looks like libcuda.so is installed in unexpected location for whatever reason. You can try setting CMAKE_LIBRARY_PATH or/and CUDA_LIB_PATH to that path.
The CUDA_LIB_PATH needs to be set outside cmake I think, eg export CUDA_LIB_PATH=/usr/local/cuda/lib64/stubs/

Error linking to Boost filesystem using cmake on cygwin

I'm using cmake 2.8.9, g++ 3.4.4, and Boost 1.50. in Cygwin on Windows 8 64 bit.
Here is the error message I get.
Linking CXX executable RayTracer.exe
CMakeFiles/RayTracer.dir/Ray_Tracer.cpp.o:Ray_Tracer.cpp:(.text+0x89c):
undefined reference to boost::system::generic_category()'
CMakeFiles/RayTracer.dir/Ray_Tracer.cpp.o:Ray_Tracer.cpp:(.text+0x8a6):
undefined reference toboost::system::generic_category()'
CMakeFiles/RayTracer.dir/Ray_Tracer.cpp.o:Ray_Tracer.cpp:(.text+0x8b0):
undefined reference to boost::system::system_category()'
/usr/lib/gcc/i686-pc-cygwin/4.5.3/../../../../i686-pc-cygwin/bin/ld:
CMakeFiles/RayTracer.dir/Ray_Tracer.cpp.o: bad reloc address 0xb in
section
.text$_ZN5boost6system14error_categoryD1Ev[boost::system::error_category::~error_category()]'
collect2: ld returned 1 exit status
CMakeFiles/RayTracer.dir/build.make:94: recipe for target
RayTracer.exe' failed make[2]: *** [RayTracer.exe] Error 1
CMakeFiles/Makefile2:64: recipe for target
CMakeFiles/RayTracer.dir/all' failed make[1]: *
[CMakeFiles/RayTracer.dir/all] Error 2 Makefile:75: recipe for target
`all' failed make: * [all] Error 2
From what I've seen, the usual problem is failing to link the boost system library, but I made sure to do that. Here is the relevant portion of my CMakeLists.txt file:
#Edit: cmake can't find the static libraries on cygwin, so I'm setting this to false for now.
SET(Boost_USE_STATIC_LIBS FALSE)
FIND_PACKAGE(Boost 1.50 REQUIRED date_time program_options thread filesystem system unit_test_framework)
IF(${Boost_FOUND})
INCLUDE_DIRECTORIES(${Boost_INCLUDE_DIR})
ENDIF()
add_executable(RayTracer
Ray_Tracer.cpp
)
target_link_libraries(RayTracer ${Boost_PROGRAM_OPTIONS_LIBRARIES})
And here's the line in my .cpp file that triggers the error:
#include <boost/filesystem.hpp>
Any idea what I'm doing wrong?
You need to tell the linker to link Boost.Filesystem and Boost.System libraries.
You can do:
target_link_libraries(RayTracer
${Boost_PROGRAM_OPTIONS_LIBRARIES}
${Boost_FILESYSTEM_LIBRARIES}
${Boost_SYSTEM_LIBRARIES}
)
or if you just want to link all the libs specified in your find_package(Boost...) call, you can do:
target_link_libraries(RayTracer ${Boost_LIBRARIES})
For further details on the FindBoost CMake module, see the docs or run:
cmake --help-module FindBoost