static linking with cmake fails [duplicate] - c++

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)

Related

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

Build issues with shared_mutex and MinGW-w64

I'm currently working on a C++14 project, using MinGW-w64 and a CMake configuration (MinGW Makefile generator). I'm having a hard time building it due to compatibility issues with the shared_mutex library. To circumnavigate such problems, I'm using a custom header-only library called mingw-std-threads. After following all the instructions of the library's mark-down file, I still have errors with shared_mutex.
Here's a minimal reproducible example of the error I am getting. Source file:
#include <shared_mutex>
int main()
{
return 0;
}
The CMake configuration for the test:
# CMake 3.9 or newer
cmake_minimum_required(VERSION 3.9)
# project name, version and description
project(test)
# C++14 or higher
set(CMAKE_CXX_STANDARD 14)
set(CMAKE_CXX_STANDARD_REQUIRED True)
# library target
add_executable(test test.cpp)
# include directories
target_include_directories(test PRIVATE .)
#MinGW threads
option(MINGW_STDTHREADS_GENERATE_STDHEADERS "" ON)
add_subdirectory(./mingw-std-threads-master)
target_link_libraries(test PRIVATE mingw_stdthreads)
# defining install rules
include(GNUInstallDirs)
install(TARGETS test
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
PUBLIC_HEADER DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}
)
The project generation log:
PS C:\Users\luigi\Desktop\test> cmake --version
cmake version 3.18.3
CMake suite maintained and supported by Kitware (kitware.com/cmake).
PS C:\Users\luigi\Desktop\test> cmake -G "MinGW Makefiles" -S ./ -B build/
-- The C compiler identification is GNU 8.1.0
-- The CXX compiler identification is GNU 8.1.0
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working C compiler: C:/mingw-w64/mingw64/bin/gcc.exe - skipped
-- Detecting C compile features
-- Detecting C compile features - done
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: C:/mingw-w64/mingw64/bin/g++.exe - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
cmake_stdheaders_generator: output_include_path set to C:/Users/luigi/Desktop/test/build/mingw-std-threads-master/cmake_stdheaders_generator/cmake_stdheaders_generator
cmake_stdheaders_generator: MINGW_STDTHREADS_DIR: C:/Users/luigi/Desktop/test/mingw-std-threads-master
Matched: <C:/mingw-w64/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/condition_variable>
Matched: <C:/mingw-w64/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/future>
Matched: <C:/mingw-w64/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/mutex>
Matched: <C:/mingw-w64/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/shared_mutex>
Matched: <C:/mingw-w64/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/thread>
-- Configuring done
-- Generating done
-- Build files have been written to: C:/Users/luigi/Desktop/test/build
PS C:\Users\luigi\Desktop\test>
The resulting build log with the error:
PS C:\Users\luigi\Desktop\test> cmake --build ./build -v
"C:\Program Files\CMake\bin\cmake.exe" -SC:\Users\luigi\Desktop\test -BC:\Users\luigi\Desktop\test\build --check-build-system CMakeFiles\Makefile.cmake 0
"C:\Program Files\CMake\bin\cmake.exe" -E cmake_progress_start C:\Users\luigi\Desktop\test\build\CMakeFiles C:\Users\luigi\Desktop\test\build\\CMakeFiles\progress.marks
C:/mingw-w64/mingw64/bin/mingw32-make.exe -f CMakeFiles\Makefile2 all
mingw32-make.exe[1]: Entering directory 'C:/Users/luigi/Desktop/test/build'
C:/mingw-w64/mingw64/bin/mingw32-make.exe -f CMakeFiles\test.dir\build.make CMakeFiles/test.dir/depend
mingw32-make.exe[2]: Entering directory 'C:/Users/luigi/Desktop/test/build'
"C:\Program Files\CMake\bin\cmake.exe" -E cmake_depends "MinGW Makefiles" C:\Users\luigi\Desktop\test C:\Users\luigi\Desktop\test C:\Users\luigi\Desktop\test\build C:\Users\luigi\Desktop\test\build C:\Users\luigi\Desktop\test\build\CMakeFiles\test.dir\DependInfo.cmake --color=
Dependee "C:\Users\luigi\Desktop\test\build\CMakeFiles\test.dir\DependInfo.cmake" is newer than depender "C:/Users/luigi/Desktop/test/build/CMakeFiles/test.dir/depend.internal".
Dependee "C:/Users/luigi/Desktop/test/build/CMakeFiles/CMakeDirectoryInformation.cmake" is newer than depender "C:/Users/luigi/Desktop/test/build/CMakeFiles/test.dir/depend.internal".
Scanning dependencies of target test
mingw32-make.exe[2]: Leaving directory 'C:/Users/luigi/Desktop/test/build'
C:/mingw-w64/mingw64/bin/mingw32-make.exe -f CMakeFiles\test.dir\build.make CMakeFiles/test.dir/build
mingw32-make.exe[2]: Entering directory 'C:/Users/luigi/Desktop/test/build'
[ 50%] Building CXX object CMakeFiles/test.dir/test.cpp.obj
C:\mingw-w64\mingw64\bin\g++.exe -DMINGW_STDTHREADS_GENERATED_STDHEADERS #CMakeFiles/test.dir/includes_CXX.rsp -std=gnu++14 -o CMakeFiles\test.dir\test.cpp.obj -c C:\Users\luigi\Desktop\test\test.cpp
In file included from C:/Users/luigi/Desktop/test/mingw-std-threads-master/mingw.condition_variable.h:53,
from C:/Users/luigi/Desktop/test/build/mingw-std-threads-master/cmake_stdheaders_generator/cmake_stdheaders_generator/condition_variable:10,
from C:/mingw-w64/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/shared_mutex:37,
from C:/Users/luigi/Desktop/test/build/mingw-std-threads-master/cmake_stdheaders_generator/cmake_stdheaders_generator/shared_mutex:9,
from C:\Users\luigi\Desktop\test\test.cpp:1:
C:/Users/luigi/Desktop/test/mingw-std-threads-master/mingw.shared_mutex.h:306:12: error: 'std::shared_lock' has not been declared
using std::shared_lock;
^~~~~~~~~~~
C:/Users/luigi/Desktop/test/mingw-std-threads-master/mingw.shared_mutex.h:492:24: error: 'mingw_stdthread::shared_lock' has not been declared
using mingw_stdthread::shared_lock;
^~~~~~~~~~~
mingw32-make.exe[2]: *** [CMakeFiles\test.dir\build.make:82: CMakeFiles/test.dir/test.cpp.obj] Error 1
mingw32-make.exe[2]: Leaving directory 'C:/Users/luigi/Desktop/test/build'
mingw32-make.exe[1]: *** [CMakeFiles\Makefile2:132: CMakeFiles/test.dir/all] Error 2
mingw32-make.exe[1]: Leaving directory 'C:/Users/luigi/Desktop/test/build'
mingw32-make.exe: *** [Makefile:148: all] Error 2
PS C:\Users\luigi\Desktop\test>
These are the versions of the tools I'm using:
Windows 10 Pro x64
MinGW-w64 8.1.0
CMake 3.18.3 (using 3.9 for the configuration)
mingw-std-threads 1.0.0
Thanks for helping!

System include directory is not added by CMake 3.16.5

I have observed the below detailed behaviour when compiling a C++ program with 2 different CMake versions:
3.10.2 (installed from default APT repository for Ubuntu 18.04); and
3.16.5 (installed together with CLion).
In the project the /usr/local/include folder is added to the include path (as a system include directory if it matters), through the ODB package:
include_directories(SYSTEM ${ODB_INCLUDE_DIRS})
Where ${ODB_INCLUDE_DIRS} is /usr/local/include for me.
When I use the CMake version 3.10.2 (from APT repository), the /usr/local/include directory is added to the include path correctly.
When I use the CMake version 3.16.5 (installed with CLion), the /usr/local/include directory is not added to the include path at all.
I could easily verify this difference by running make VERBOSE=1 and examining the compilation commands. This difference raised a compilation error for the project and I would be interested why do I experience this discrepancy between the mentioned 2 CMake versions? Was some default configuration changed between them?
I have created a MWE:
cmake_minimum_required(VERSION 3.4.3)
project(mwe)
include_directories(SYSTEM /usr/local/include)
add_executable(mwe main.cpp)
Where main.cpp is just a classic Hello World application.
Here are the interesting compilation commands (below I attach the complete logs):
with CMake 3.10.2 (/usr/local/include added correctly):
/usr/bin/c++ -isystem /usr/local/include -o CMakeFiles/mwe.dir/main.cpp.o -c /home/mate/Temp/mwe/main.cpp
with CMake 3.16.5 (/usr/local/include missing):
/usr/bin/c++ -o CMakeFiles/mwe.dir/main.cpp.o -c /home/mate/Temp/mwe/main.cpp
Running CMake 3.10.2:
>> cmake ..
-- The C compiler identification is GNU 7.5.0
-- The CXX compiler identification is GNU 7.5.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
-- Configuring done
-- Generating done
-- Build files have been written to: /home/mate/Temp/mwe/build_3-10-2
>> make VERBOSE=1
/usr/bin/cmake -H/home/mate/Temp/mwe -B/home/mate/Temp/mwe/build_3-10-2 --check-build-system CMakeFiles/Makefile.cmake 0
/usr/bin/cmake -E cmake_progress_start /home/mate/Temp/mwe/build_3-10-2/CMakeFiles /home/mate/Temp/mwe/build_3-10-2/CMakeFiles/progress.marks
make -f CMakeFiles/Makefile2 all
make[1]: Entering directory '/home/mate/Temp/mwe/build_3-10-2'
make -f CMakeFiles/mwe.dir/build.make CMakeFiles/mwe.dir/depend
make[2]: Entering directory '/home/mate/Temp/mwe/build_3-10-2'
cd /home/mate/Temp/mwe/build_3-10-2 && /usr/bin/cmake -E cmake_depends "Unix Makefiles" /home/mate/Temp/mwe /home/mate/Temp/mwe /home/mate/Temp/mwe/build_3-10-2 /home/mate/Temp/mwe/build_3-10-2 /home/mate/Temp/mwe/build_3-10-2/CMakeFiles/mwe.dir/DependInfo.cmake --color=
Dependee "/home/mate/Temp/mwe/build_3-10-2/CMakeFiles/mwe.dir/DependInfo.cmake" is newer than depender "/home/mate/Temp/mwe/build_3-10-2/CMakeFiles/mwe.dir/depend.internal".
Dependee "/home/mate/Temp/mwe/build_3-10-2/CMakeFiles/CMakeDirectoryInformation.cmake" is newer than depender "/home/mate/Temp/mwe/build_3-10-2/CMakeFiles/mwe.dir/depend.internal".
Scanning dependencies of target mwe
make[2]: Leaving directory '/home/mate/Temp/mwe/build_3-10-2'
make -f CMakeFiles/mwe.dir/build.make CMakeFiles/mwe.dir/build
make[2]: Entering directory '/home/mate/Temp/mwe/build_3-10-2'
[ 50%] Building CXX object CMakeFiles/mwe.dir/main.cpp.o
/usr/bin/c++ -isystem /usr/local/include -o CMakeFiles/mwe.dir/main.cpp.o -c /home/mate/Temp/mwe/main.cpp
[100%] Linking CXX executable mwe
/usr/bin/cmake -E cmake_link_script CMakeFiles/mwe.dir/link.txt --verbose=1
/usr/bin/c++ CMakeFiles/mwe.dir/main.cpp.o -o mwe
make[2]: Leaving directory '/home/mate/Temp/mwe/build_3-10-2'
[100%] Built target mwe
make[1]: Leaving directory '/home/mate/Temp/mwe/build_3-10-2'
/usr/bin/cmake -E cmake_progress_start /home/mate/Temp/mwe/build_3-10-2/CMakeFiles 0
Running CMake 3.16.5:
>> /opt/CLion/bin/cmake/linux/bin/cmake ..
-- The C compiler identification is GNU 7.5.0
-- The CXX compiler identification is GNU 7.5.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
-- Configuring done
-- Generating done
-- Build files have been written to: /home/mate/Temp/mwe/build_3-16-5
>> make VERBOSE=1
/opt/CLion/bin/cmake/linux/bin/cmake -S/home/mate/Temp/mwe -B/home/mate/Temp/mwe/build_3-16-5 --check-build-system CMakeFiles/Makefile.cmake 0
/opt/CLion/bin/cmake/linux/bin/cmake -E cmake_progress_start /home/mate/Temp/mwe/build_3-16-5/CMakeFiles /home/mate/Temp/mwe/build_3-16-5/CMakeFiles/progress.marks
make -f CMakeFiles/Makefile2 all
make[1]: Entering directory '/home/mate/Temp/mwe/build_3-16-5'
make -f CMakeFiles/mwe.dir/build.make CMakeFiles/mwe.dir/depend
make[2]: Entering directory '/home/mate/Temp/mwe/build_3-16-5'
cd /home/mate/Temp/mwe/build_3-16-5 && /opt/CLion/bin/cmake/linux/bin/cmake -E cmake_depends "Unix Makefiles" /home/mate/Temp/mwe /home/mate/Temp/mwe /home/mate/Temp/mwe/build_3-16-5 /home/mate/Temp/mwe/build_3-16-5 /home/mate/Temp/mwe/build_3-16-5/CMakeFiles/mwe.dir/DependInfo.cmake --color=
Dependee "/home/mate/Temp/mwe/build_3-16-5/CMakeFiles/mwe.dir/DependInfo.cmake" is newer than depender "/home/mate/Temp/mwe/build_3-16-5/CMakeFiles/mwe.dir/depend.internal".
Dependee "/home/mate/Temp/mwe/build_3-16-5/CMakeFiles/CMakeDirectoryInformation.cmake" is newer than depender "/home/mate/Temp/mwe/build_3-16-5/CMakeFiles/mwe.dir/depend.internal".
Scanning dependencies of target mwe
make[2]: Leaving directory '/home/mate/Temp/mwe/build_3-16-5'
make -f CMakeFiles/mwe.dir/build.make CMakeFiles/mwe.dir/build
make[2]: Entering directory '/home/mate/Temp/mwe/build_3-16-5'
[ 50%] Building CXX object CMakeFiles/mwe.dir/main.cpp.o
/usr/bin/c++ -o CMakeFiles/mwe.dir/main.cpp.o -c /home/mate/Temp/mwe/main.cpp
[100%] Linking CXX executable mwe
/opt/CLion/bin/cmake/linux/bin/cmake -E cmake_link_script CMakeFiles/mwe.dir/link.txt --verbose=1
/usr/bin/c++ CMakeFiles/mwe.dir/main.cpp.o -o mwe
make[2]: Leaving directory '/home/mate/Temp/mwe/build_3-16-5'
[100%] Built target mwe
make[1]: Leaving directory '/home/mate/Temp/mwe/build_3-16-5'
/opt/CLion/bin/cmake/linux/bin/cmake -E cmake_progress_start /home/mate/Temp/mwe/build_3-16-5/CMakeFiles 0

Undefined reference to 'cudaRegisterLinkedBinary' - linking error in CMake?

I'm running CentOS 7.8 via dual-boot on a 64-bit 2013 Mac with a GT 650M GPU. I'm using CMake 3.17, CUDA 10.0, and GCC 4.8.5. All CUDA samples have been tested and work fine, and I'm able to compile other standard C++ code perfectly.
I've reduced my full project to a simple test case as follows, where the CMakeLists file is:
CMAKE_MINIMUM_REQUIRED(VERSION 3.8)
PROJECT(test LANGUAGES CUDA CXX C)
SET(CMAKE_VERBOSE_MAKEFILE ON)
MESSAGE(STATUS "Setting to Release mode")
SET(CMAKE_BUILD_TYPE "Release")
# Set CUDA flags
set(CMAKE_CUDA_FLAGS "${CMAKE_CUDA_FLAGS} -arch=sm_30 -rdc=true")
# Set flags
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -g -Wall -ffast-math")
MESSAGE(STATUS "Setting g++ flags for Release configuration")
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -O3") ## Optimize
SET(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -s ") ## Strip binary
ADD_SUBDIRECTORY( src )
In the /src folder I have another CMake file to gather the source files:
INCLUDE_DIRECTORIES( ${CMAKE_CURRENT_SOURCE_DIR} ${CMAKE_CURRENT_BINARY_DIR} )
INCLUDE_DIRECTORIES( ${CMAKE_BINARY_DIR} )
SET(test_SRCS
rsmain.cu
SGP4.cu
SGP4.cuh
)
function(my_add_executable TargetName)
set(Files ${ARGV})
list(REMOVE_AT Files 0)
add_executable(${TargetName} ${Files})
set_target_properties(${TargetName} PROPERTIES
RUNTIME_OUTPUT_DIRECTORY
"${CMAKE_SOURCE_DIR}/build")
endfunction()
my_add_executable(test ${test_SRCS})
INSTALL( TARGETS test DESTINATION bin)
As shown, there are three main source files - both SGP4.cu and SGP4.cuh are empty, while rsmain.cu is simply:
/// Main function
int main(int argc, char *argv[])
{
return 0;
}
When trying to build, I get the following output:
[me#localhost build]$ cmake3 ..
-- The CUDA compiler identification is NVIDIA 10.0.130
-- The CXX compiler identification is GNU 4.8.5
-- The C compiler identification is GNU 4.8.5
-- Check for working CUDA compiler: /usr/local/cuda-10.0/bin/nvcc
-- Check for working CUDA compiler: /usr/local/cuda-10.0/bin/nvcc - works
-- Detecting CUDA compiler ABI info
-- Detecting CUDA compiler ABI info - done
-- Detecting CUDA compile features
-- Detecting CUDA 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
-- 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
-- No build configuration specified, defaulting to Release
-- Setting general compiler flags for detected compiler: gnu-g++
-- Setting g++ flags for Release configuration
-- Configuring done
-- Generating done
-- Build files have been written to: /home/me/Documents/test/build
[me#localhost build]$ make
/usr/bin/cmake3 -S/home/me/Documents/test -B/home/me/Documents/test/build --check-build-system CMakeFiles/Makefile.cmake 0
/usr/bin/cmake3 -E cmake_progress_start /home/me/Documents/test/build/CMakeFiles /home/me/Documents/test/build/CMakeFiles/progress.marks
make -f CMakeFiles/Makefile2 all
make[1]: Entering directory `/home/me/Documents/test/build'
make -f src/CMakeFiles/test.dir/build.make src/CMakeFiles/test.dir/depend
make[2]: Entering directory `/home/me/Documents/test/build'
cd /home/me/Documents/test/build && /usr/bin/cmake3 -E cmake_depends "Unix Makefiles" /home/me/Documents/test /home/me/Documents/test/src /home/me/Documents/test/build /home/me/Documents/test/build/src /home/me/Documents/test/build/src/CMakeFiles/test.dir/DependInfo.cmake --color=
Scanning dependencies of target test
make[2]: Leaving directory `/home/me/Documents/test/build'
make -f src/CMakeFiles/test.dir/build.make src/CMakeFiles/test.dir/build
make[2]: Entering directory `/home/me/Documents/test/build'
[ 33%] Building CUDA object src/CMakeFiles/test.dir/rsmain.cu.o
cd /home/me/Documents/test/build/src && /usr/local/cuda-10.0/bin/nvcc -I/home/me/Documents/test/src -I/home/me/Documents/test/build/src -I/home/me/Documents/test/build -arch=sm_30 -rdc=true -O3 -DNDEBUG -std=c++03 -x cu -c /home/me/Documents/test/src/rsmain.cu -o CMakeFiles/test.dir/rsmain.cu.o
[ 66%] Building CUDA object src/CMakeFiles/test.dir/SGP4.cu.o
cd /home/me/Documents/test/build/src && /usr/local/cuda-10.0/bin/nvcc -I/home/me/Documents/test/src -I/home/me/Documents/test/build/src -I/home/me/Documents/test/build -arch=sm_30 -rdc=true -O3 -DNDEBUG -std=c++03 -x cu -c /home/me/Documents/test/src/SGP4.cu -o CMakeFiles/test.dir/SGP4.cu.o
[100%] Linking CUDA executable ../test
cd /home/me/Documents/test/build/src && /usr/bin/cmake3 -E cmake_link_script CMakeFiles/test.dir/link.txt --verbose=1
/usr/bin/g++ -s CMakeFiles/test.dir/rsmain.cu.o CMakeFiles/test.dir/SGP4.cu.o -o ../test -lcudadevrt -lcudart_static -L"/usr/local/cuda-10.0/targets/x86_64-linux/lib/stubs" -L"/usr/local/cuda-10.0/targets/x86_64-linux/lib" -lcudadevrt -lcudart_static -lrt -lpthread -ldl
CMakeFiles/test.dir/rsmain.cu.o: In function `__sti____cudaRegisterAll()':
tmpxft_00004eed_00000000-5_rsmain.cudafe1.cpp:(.text.startup+0x25): undefined reference to `__cudaRegisterLinkedBinary_41_tmpxft_00004eed_00000000_6_rsmain_cpp1_ii_main'
CMakeFiles/test.dir/SGP4.cu.o: In function `__sti____cudaRegisterAll()':
tmpxft_00004f02_00000000-5_SGP4.cudafe1.cpp:(.text.startup+0x15): undefined reference to `__cudaRegisterLinkedBinary_39_tmpxft_00004f02_00000000_6_SGP4_cpp1_ii_71922fcb'
collect2: error: ld returned 1 exit status
make[2]: *** [test] Error 1
make[2]: Leaving directory `/home/me/Documents/test/build'
make[1]: *** [src/CMakeFiles/test.dir/all] Error 2
make[1]: Leaving directory `/home/me/Documents/test/build'
make: *** [all] Error 2
Could anyone please explain what this 'cudaRegisterLinkedBinary' stuff is all about? I've tried a bunch of things in trying to solve it but nothing has worked so far. Is there an issue with any of the package versions? A problem in CMakeLists? Compatibility issues with CUDA and my hardware?
It's worth noting that the full code compiled and ran perfectly when I tested it on a HPC server (also running CentOS 7 and Cuda 10.0) - but on my personal PC it fails at the linking step. I've even confirmed that the .bashrc files are the same across both installations, but it hasn't fixed anything. I'm also currently able to compile NVIDIA's OptiX software (which also uses CUDA) without any problems.
Any advice would be appreciated. Please let me know if I missed any required details.
EDIT: Answer added below. Resolved.
Finally sorted this out. This was my main CMakeLists file:
CMAKE_MINIMUM_REQUIRED(VERSION 3.8)
PROJECT(test LANGUAGES C CXX CUDA)
SET(CMAKE_BUILD_TYPE "Release")
# Set CUDA flags
set(CMAKE_CUDA_FLAGS "${CMAKE_CUDA_FLAGS} -arch=sm_30 -rdc=true")
# Set flags
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -g -Wall -ffast-math -O3")
MESSAGE(STATUS "Setting g++ flags for Release configuration")
SET(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -s") ## Strip binary
ADD_SUBDIRECTORY( src )
And in the /src CMakeLists file, I had to make the change:
INCLUDE_DIRECTORIES( ${CMAKE_CURRENT_SOURCE_DIR} ${CMAKE_CURRENT_BINARY_DIR} )
INCLUDE_DIRECTORIES( ${CMAKE_BINARY_DIR} )
SET(test_SRCS
rsmain.cu
SGP4.cu
SGP4.cuh
)
function(my_add_executable TargetName)
set(Files ${ARGV})
list(REMOVE_AT Files 0)
add_executable(${TargetName} ${Files})
set_target_properties(${TargetName} PROPERTIES CUDA_RESOLVE_DEVICE_SYMBOLS ON
RUNTIME_OUTPUT_DIRECTORY
"${CMAKE_SOURCE_DIR}/build")
endfunction()
my_add_executable(test ${test_SRCS})
INSTALL( TARGETS test DESTINATION bin)
Setting CUDA_RESOLVE_DEVICE_SYMBOLS to ON is the change. In my main project, I also had to repeat this for every target involving any CUDA files. Everything compiles and runs perfectly now.

C++ cmake & boost & arm cross compilation

I have a problem with compiling my program with boost and cmake. I use cross-compilation with using gcc-linaro-arm-linux-gnueabihf-4.9/bin/arm-linux-gnueabihf-g++ compiler.
And now I have cmake file like:
cmake_minimum_required (VERSION 2.6.2)
project (xxx)
SET(CMAKE_CXX_COMPILER /home/kamil/gcc-linaro-arm-linux-gnueabihf-4.9/bin/arm-linux-gnueabihf-g++)
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -pthread -Dtypeof=__typeof__ -D_GLIBCXX_USE_CXX11_ABI=0 -Wall -Werror -Wextra -Wno-error=array-bounds")
if(DEFINED ENV{SDKTARGETSYSROOT})
set(CMAKE_FIND_ROOT_PATH $ENV{SDKTARGETSYSROOT})
set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)
set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)
endif(DEFINED ENV{SDKTARGETSYSROOT})
find_package(Boost COMPONENTS system filesystem REQUIRED)
include_directories("/usr/local/include")
include_directories(${Boost_INCLUDE_DIRS})
enable_testing()
set(CMAKE_CTEST_COMMAND ctest -V)
add_custom_target(check COMMAND ${CMAKE_CTEST_COMMAND} )
add_subdirectory(src)
add_subdirectory(test EXCLUDE_FROM_ALL)
and
add_executable(xxxx
main.cpp
...cpp
...cpp
...cpp
...cpp
)
target_link_libraries(xxxx
${Boost_SYSTEM_LIBRARY}
${Boost_THREAD_LIBRARY}
)
install(TARGETS xxxx
DESTINATION bin
PERMISSIONS OWNER_READ OWNER_WRITE OWNER_EXECUTE GROUP_READ GROUP_EXECUTE
WORLD_READ WORLD_EXECUTE
)
And now when use commands in linux(Ubuntu):
cmake ..
make
I have response at:
kamil#kamil:~/test/build$ rm -rf *
kamil#kamil:~/test/build$ cmake ..
-- The C compiler identification is GNU 4.8.4
-- The CXX compiler identification is GNU 4.8.4
-- 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
-- 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
-- Boost version: 1.55.0
-- Found the following Boost libraries:
-- system
-- filesystem
-- Found GTest: /usr/local/lib/libgtest.so
-- Configuring done
-- Generating done
-- Build files have been written to: /home/kamil/test/build
kamil#kamil:~/test/build$ make
Scanning dependencies of target xxxx
[ 12%] Building CXX object src/CMakeFiles/test.dir/main.cpp.o
In file included from /home/test/src/test/utils/logger.hpp:4:0,
from /home/kamil/test/src/main.cpp:9:
/home/kamil/test/src/test/utils/singleton.hpp:5:33: fatal error: boost/noncopyable.hpp: No such file or directory
#include <boost/noncopyable.hpp>
^
compilation terminated.
make[2]: *** [src/CMakeFiles/test.dir/main.cpp.o] Error 1
make[1]: *** [src/CMakeFiles/test.dir/all] Error 2
make: *** [all] Error 2
Error:
fatal error: boost/noncopyable.hpp: No such file or directory
#include <boost/noncopyable.hpp>
When I comment on the following line in cmake:
SET(CMAKE_CXX_COMPILER /home/kamil/toradex/gcc-linaro-arm-linux-gnueabihf-4.9/bin/arm-linux-gnueabihf-g++)
then everything is ok.
I install boost in Ubuntu with command: sudo apt-get install libboost1.55-all-dev
What is wrong with compiling with linux-gnueabihf-g++ and how to fix it.
Compiling boost with gcc-linaro-arm-linux-gnueabihf-4.9 worked.
Helps link: http://www.cnx-software.com/2011/10/03/cross-compiling-boost-c-libraries-for-arm/