I'm trying to add boost::stacktrace library to my project via CMake. CMake finds all required libs well, but when I call boost::stacktrace::stacktrace() to print stack information into std::cout - it gives me the following error:
undefined reference to 'dladdr'
I've already tried to add the -ldl compile flag to the CMAKE_CXX_FLAGS compile flags to see if it can help, but it doesn't work. I understand that I missed some required packages, but I don't know which. Maybe you could advise me.
Gcc version 7.4.0 (Ubuntu 7.4.0-1ubuntu1~18.04)
Boost version 1.68.0
Here is some code:
CMakeLists.txt
#-------------------------------------------------------------------#
# cmake version & project name
cmake_minimum_required(VERSION 3.10)
project(stack_trace_exmpl)
#-------------------------------------------------------------------#
# project flags config
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_C_STANDARD 11)
set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -g3 -pthread")
set(CMAKE_C_FLAGS_DEBUG "-g3")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -pthread -ldl")
set(CMAKE_C_FLAGS "-Wall")
set(BOOST_ROOT "/opt/Boost_1_68_0/")
#-------------------------------------------------------------------#
# find Boost libs
set(Boost_USE_STATIC_LIBS OFF)
set(Boost_USE_MULTITHREADED ON)
set(Boost_USE_STATIC_RUNTIME OFF)
find_package(
Boost 1.68.0
COMPONENTS REQUIRED
stacktrace_basic
stacktrace_backtrace
stacktrace_addr2line
stacktrace_noop
)
#-------------------------------------------------------------------#
# project sources & project's type
file(GLOB SRCS "*.cpp")
file(GLOB HDRS "*.hpp")
add_executable(${PROJECT_NAME} ${HDRS} ${SRCS})
#-------------------------------------------------------------------#
# link libraries
target_link_libraries(${PROJECT_NAME}
PUBLIC ${Boost_STACKTRACE_BASIC_LIBRARY}
PUBLIC ${Boost_STACKTRACE_BACKTRACE_LIBRARY}
PUBLIC ${Boost_STACKTRACE_ADDR2LINE_LIBRARY}
PUBLIC ${Boost_STACKTRACE_NOOP_LIBRARY}
)
#-------------------------------------------------------------------#
# include directories
target_include_directories(${PROJECT_NAME}
PUBLIC ${Boost_INCLUDE_DIRS}
)
#-------------------------------------------------------------------#
main.cpp
#include <iostream>
#include <boost/stacktrace.hpp>
void tracesToCout( int _n )
{
std::cout << boost::stacktrace::stacktrace();
if( _n > 0 )
{
tracesToCout( --_n );
}
}
int main()
{
tracesToCout( 5 );
return 0;
}
CMake output:
-- The C compiler identification is GNU 7.4.0
-- The CXX compiler identification is GNU 7.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
-- Found Boost: /opt/Boost_1_68_0/include (found suitable version "1.68.0", minimum required is "1.68.0") found components: stacktrace_basic stacktrace_backtrace stacktrace_addr2line stacktrace_noop
-- Configuring done
-- Generating done
-- Build files have been written to: /home/user/Work/boost_stacktrace/Debug64
build output:
CMakeFiles/stack_trace_exmpl.dir/main.cpp.o: In function `boost::stacktrace::detail::location_from_symbol::location_from_symbol(void const*)':
main.cpp:(.text._ZN5boost10stacktrace6detail20location_from_symbolC2EPKv[_ZN5boost10stacktrace6detail20location_from_symbolC5EPKv]+0x4e): undefined reference to `dladdr'
CMakeFiles/stack_trace_exmpl.dir/main.cpp.o: In function `boost::stacktrace::frame::name[abi:cxx11]() const':
main.cpp:(.text._ZNK5boost10stacktrace5frame4nameB5cxx11Ev[_ZNK5boost10stacktrace5frame4nameB5cxx11Ev]+0x31): undefined reference to `dladdr'
collect2: error: ld returned 1 exit status
CMakeFiles/stack_trace_exmpl.dir/build.make:87: recipe for target 'stack_trace_exmpl' failed
make[2]: *** [stack_trace_exmpl] Error 1
CMakeFiles/Makefile2:75: recipe for target 'CMakeFiles/stack_trace_exmpl.dir/all' failed
make[1]: *** [CMakeFiles/stack_trace_exmpl.dir/all] Error 2
Makefile:83: recipe for target 'all' failed
make: *** [all] Error 2
Turning my comment into an answer:
Because the dl library is needed by boost::stacktrace (see the Boost requirements), the dl library should be linked after the Boost stacktrace libraries are linked. To ensure this ordering, link the dl library (and perhaps pthread as well) at the end of the target_link_libraries() call instead:
target_link_libraries(${PROJECT_NAME}
PUBLIC ${Boost_STACKTRACE_BASIC_LIBRARY}
PUBLIC ${Boost_STACKTRACE_BACKTRACE_LIBRARY}
PUBLIC ${Boost_STACKTRACE_ADDR2LINE_LIBRARY}
PUBLIC ${Boost_STACKTRACE_NOOP_LIBRARY}
PUBLIC pthread dl
)
Related
Why does Cmake find the library but I can't include it?
I am trying to use the gsl library in C++ but I get gsl/gsl_sf_bessel.h: No such file or directory when I run cmake --build even though it says -- Found GSL: /usr/include (found version "2.7.1") in the terminal after running cmake ...
I am using Ubuntu 22.04
I installed gsl with sudo apt-get install libgsl-dev and it is in /usr/include.
I think it might be a problem with the compiler I am using but I'm not sure how to check.
My CMakeLists.txt file
cmake_minimum_required(VERSION 3.5.1)
project(mujoco_gym)
set(CMAKE_CXX_STANDARD 14)
# It prevents the decay to C++98 when the compiler does not support C++14
set(CMAKE_CXX_STANDARD_REQUIRED ON)
# It disables the use of compiler-specific extensions
# e.g. -std=c++14 rather than -std=gnu++14
set(CMAKE_CXX_EXTENSIONS OFF)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall")
find_package(GSL REQUIRED)
link_libraries(GSL::gsl)
include_directories(${PROJECT_SOURCE_DIR})
file(GLOB SOURCE_FILES mujoco_gym.cpp)
add_executable(${CMAKE_PROJECT_NAME} ${SOURCE_FILES})
target_link_libraries (
${CMAKE_PROJECT_NAME}
${GSL_LIBRARIES}
)
My .cpp file.
#include<stdbool.h> //for bool
#include "stdlib.h"
#include "string.h"
#include <iostream>
#include <gsl/gsl_sf_bessel.h>
int main()
{
double x = 5.0;
std::cout << gsl_sf_bessel_j0(x);
return 0;
}
Cmake and build in the terminal
$ cmake ..
-- The C compiler identification is GNU 9.4.0
-- The CXX compiler identification is GNU 9.4.0
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working C compiler: /home/iii/miniconda3/envs/tf/bin/x86_64-conda-linux-gnu-cc - 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: /home/iii/miniconda3/envs/tf/bin/x86_64-conda-linux-gnu-c++ - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Found PkgConfig: /usr/bin/pkg-config (found version "0.29.2")
-- Found GSL: /usr/include (found version "2.7.1")
-- Configuring done
-- Generating done
-- Build files have been written to: /home/iii/.mujoco/mujoco210/sample/build
$ cmake --build . --config Release
[ 50%] Building CXX object CMakeFiles/mujoco_gym.dir/mujoco_gym.cpp.o
/home/iii/.mujoco/mujoco210/sample/mujoco_gym.cpp:21:10: fatal error: gsl/gsl_sf_bessel.h: No such file or directory
21 | #include <gsl/gsl_sf_bessel.h>
| ^~~~~~~~~~~~~~~~~~~~~
compilation terminated.
gmake[2]: *** [CMakeFiles/mujoco_gym.dir/build.make:76: CMakeFiles/mujoco_gym.dir/mujoco_gym.cpp.o] Error 1
gmake[1]: *** [CMakeFiles/Makefile2:83: CMakeFiles/mujoco_gym.dir/all] Error 2
gmake: *** [Makefile:91: all] Error 2
$
I Written a very simple program in protobuf without services at all but when i run the code it throws erros like Example::Note::~Note() all information are given below please tell me where i'm making mistake.
example.proto
syntax = "proto3";
package Example;
message Note {
int32 id = 1;
string title = 2;
string description = 3;
}
i use protoc --cpp_out=. example.proto command to generate auto generated files
main.cpp
#include "example.pb.h"
#include <iostream>
int main(int argc, char const *argv[])
{
Example::Note note;
note.set_id(1);
note.set_title("This is Title");
note.set_description("This is Description");
std::cout << "Note Information: " << note.DebugString() << std::endl;
return 0;
}
CMakeLists.txt
cmake_minimum_required(VERSION 3.14)
project(EXAMPLE LANGUAGES CXX)
# Find Protobuf installation
# Looks for protobuf-config.cmake file installed by Protobuf's cmake installation.
set(protobuf_MODULE_COMPATIBLE TRUE)
find_package(Protobuf CONFIG REQUIRED)
message(STATUS "Using protobuf ${Protobuf_VERSION}")
set(_PROTOBUF_LIBPROTOBUF protobuf::libprotobuf)
set(_PROTOBUF_PROTOC $<TARGET_FILE:protobuf::protoc>)
# Find gRPC installation
# Looks for gRPCConfig.cmake file installed by gRPC's cmake installation.
find_package(gRPC CONFIG REQUIRED)
message(STATUS "Using gRPC ${gRPC_VERSION}")
set(_GRPC_GRPCPP gRPC::grpc++)
set(_REFLECTION gRPC::grpc++_reflection)
set(_GRPC_CPP_PLUGIN_EXECUTABLE $<TARGET_FILE:gRPC::grpc_cpp_plugin>)
add_executable(Example
main.cpp
)
target_link_libraries(Example
Threads::Threads
${_REFLECTION}
${_GRPC_GRPCPP}
${_PROTOBUF_LIBPROTOBUF}
)
Error
.
.
.
-- Build files have been written to: /home/react-vision/dev/workingWithProtobuff/simpleExample/build
[ 50%] Building CXX object CMakeFiles/Example.dir/main.cpp.o
[100%] Linking CXX executable Example
/usr/bin/ld: CMakeFiles/Example.dir/main.cpp.o: in function `main':
main.cpp:(.text+0xf4): undefined reference to `Example::Note::~Note()'
/usr/bin/ld: main.cpp:(.text+0x12f): undefined reference to `Example::Note::~Note()'
/usr/bin/ld: CMakeFiles/Example.dir/main.cpp.o: in function `Example::Note::Note()':
main.cpp:(.text._ZN7Example4NoteC2Ev[_ZN7Example4NoteC5Ev]+0x22): undefined reference to `Example::Note::Note(google::protobuf::Arena*, bool)'
collect2: error: ld returned 1 exit status
make[2]: *** [CMakeFiles/Example.dir/build.make:153: Example] Error 1
make[1]: *** [CMakeFiles/Makefile2:83: CMakeFiles/Example.dir/all] Error 2
make: *** [Makefile:91: all] Error 2
The following CMakeLists.txt is enough for your example.proto and main.cpp file. It does not use gRPC because that wasn't relevant to testing.
cmake_minimum_required(VERSION 3.23)
project(example)
find_package(Protobuf REQUIRED)
protobuf_generate_cpp(example_srcs example_hdrs example.proto)
add_executable(main main.cpp ${example_srcs} ${example_hdrs})
target_include_directories(main PRIVATE "${CMAKE_CURRENT_BINARY_DIR}")
target_link_libraries(main PRIVATE protobuf::libprotobuf)
Below is a line-by-line explanation of this build and why it works.
Let's go line by line:
cmake_minimum_required(VERSION 3.23)
This must always be the first line of your top-level CMakeLists.txt file. It tells CMake which policies to enable. This is to ensure backwards compatibility, but not forwards compatibility, i.e. CMake will not emulate older versions, but will simply toggle back to old behaviors in certain cases. In particular, it will not stop you from using features that are too new for the stated minimum version, so you must always test your build with the version listed here. I wrote this answer using 3.23, but it will probably work with older versions.
project(example)
With very few exceptions, this will be the second line for your CMakeLists.txt. You can pick any project name you want. I chose example here because, well, this is an example. This command is what kicks off CMake's compiler detection routines. It is invalid to do most things before that has happened.
find_package(Protobuf REQUIRED)
This project uses protobuf so we use find_package to access CMake's first-party protobuf support (see documentation here). Because the project cannot possibly work without protobuf, we add the REQUIRED argument.
protobuf_generate_cpp(example_srcs example_hdrs example.proto)
Now we call a function provided by the find_package(Protobuf ...) module. This wraps a call to the protobuf compiler and defines two variables, example_srcs and example_hdrs (the names are arbitrary), to hold the files generated from example.proto. This isn't well-documented, but the generated files are placed in ${CMAKE_CURRENT_BINARY_DIR}.
add_executable(main main.cpp ${example_srcs} ${example_hdrs})
Now we create an executable target from your source file (main.cpp) and the aforementioned generated source files.
target_include_directories(main PRIVATE "${CMAKE_CURRENT_BINARY_DIR}")
The source files for main need to see the generated sources, so we add ${CMAKE_CURRENT_BINARY_DIR} to the list of include directories.
target_link_libraries(main PRIVATE protobuf::libprotobuf)
Finally, the application needs to link to the protobuf library, so we link to the imported protobuf::libprotobuf target. This, too, was created by the find_package(Protobuf ...) command.
To adjust this build to use Protobuf's own CMake support and to skip CMake's module, you will need to make the following changes:
Add CONFIG to find_package.
Replace the use of protobuf_generate_cpp with protobuf_generate.
This is what the build would look like:
cmake_minimum_required(VERSION 3.23)
project(example)
find_package(Protobuf CONFIG REQUIRED)
add_executable(main main.cpp)
protobuf_generate(TARGET main PROTOS example.proto)
target_include_directories(main PRIVATE "${CMAKE_CURRENT_BINARY_DIR}")
target_link_libraries(main PRIVATE protobuf::libprotobuf)
The CONFIG argument tells CMake to ignore any find modules that match (including its own first-party modules). Then the new command protobuf_generate replaces protobuf_generate_cpp. Now instead of populating source variables, you give it the target for which you're generating sources. The PROTOS argument is the list of protobuf files to compile.
Because it takes the target as an argument, it must follow add_exectuable, rather than precede it.
Here's how I built protobuf from sources to test this:
$ git clone --recursive git#github.com:protocolbuffers/protobuf.git
$ cmake -G Ninja -S protobuf -B _build/protobuf -DCMAKE_BUILD_TYPE=Release -Dprotobuf_BUILD_TESTS=NO -DCMAKE_INSTALL_PREFIX=$PWD/_local
$ cmake --build _build/protobuf/ --target install
Now I can compile the program in this example:
alex#alex-ubuntu:~/test$ cmake -G Ninja -S . -B _build/project -DCMAKE_BUILD_TYPE=Release -DCMAKE_PREFIX_PATH=$PWD/_local
-- The C compiler identification is GNU 9.4.0
-- The CXX compiler identification is GNU 9.4.0
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working C compiler: /usr/bin/cc - 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: /usr/bin/c++ - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Found ZLIB: /usr/lib/x86_64-linux-gnu/libz.so (found version "1.2.11")
-- 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
-- Configuring done
-- Generating done
-- Build files have been written to: /home/alex/test/_build/project
alex#alex-ubuntu:~/test$ cmake --build _build/project/ --verbose
[1/4] cd /home/alex/test/_build/project && /home/alex/test/_local/bin/protoc-3.21.0.2 --cpp_out :/home/alex/test/_build/project -I /home/alex/test /home/alex/test/example.proto
[2/4] /usr/bin/c++ -I/home/alex/test/_build/project -isystem /home/alex/test/_local/include -O3 -DNDEBUG -MD -MT CMakeFiles/main.dir/main.cpp.o -MF CMakeFiles/main.dir/main.cpp.o.d -o CMakeFiles/main.dir/main.cpp.o -c /home/alex/test/main.cpp
[3/4] /usr/bin/c++ -I/home/alex/test/_build/project -isystem /home/alex/test/_local/include -O3 -DNDEBUG -MD -MT CMakeFiles/main.dir/example.pb.cc.o -MF CMakeFiles/main.dir/example.pb.cc.o.d -o CMakeFiles/main.dir/example.pb.cc.o -c /home/alex/test/_build/project/example.pb.cc
[4/4] : && /usr/bin/c++ -O3 -DNDEBUG CMakeFiles/main.dir/main.cpp.o CMakeFiles/main.dir/example.pb.cc.o -o main ../../_local/lib/libprotobuf.a -lpthread /usr/lib/x86_64-linux-gnu/libz.so && :
alex#alex-ubuntu:~/test$ ./_build/project/main
Note Information: id: 1
title: "This is Title"
description: "This is Description"
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.
I've been trying to compile a simple OpenMP program using AppleClang on Mac OS X 10.14.5 Mojave with the CLion IDE.
main.cpp:
#include <omp.h>
#include <iostream>
int main() {
std::cout << omp_get_max_threads() << std::endl;
return 1;
}
CMakeLists.txt:
cmake_minimum_required(VERSION 3.14)
project(OpenMPTest)
set(CMAKE_CXX_STANDARD 17)
add_executable(OpenMPTest main.cpp)
find_package(OpenMP)
if (OPENMP_FOUND)
set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${OpenMP_C_FLAGS}")
set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${OpenMP_CXX_FLAGS}")
set (CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${OpenMP_EXE_LINKER_FLAGS}")
endif()
CMake output:
/Applications/CLion.app/Contents/bin/cmake/mac/bin/cmake -DCMAKE_BUILD_TYPE=Debug -G "CodeBlocks - Unix Makefiles" /Users/bully/CLionProjects/OpenMPTest
-- The C compiler identification is AppleClang 10.0.1.10010046
-- The CXX compiler identification is AppleClang 10.0.1.10010046
-- Check for working C compiler: /Library/Developer/CommandLineTools/usr/bin/cc
-- Check for working C compiler: /Library/Developer/CommandLineTools/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: /Library/Developer/CommandLineTools/usr/bin/c++
-- Check for working CXX compiler: /Library/Developer/CommandLineTools/usr/bin/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Found OpenMP_C: -Xclang -fopenmp (found version "3.1")
-- Found OpenMP_CXX: -Xclang -fopenmp (found version "3.1")
-- Found OpenMP: TRUE (found version "3.1")
-- Configuring done
-- Generating done
-- Build files have been written to: /Users/bully/CLionProjects/OpenMPTest/cmake-build-debug
When I build the project, I receive a linker error:
====================[ Build | all | Debug ]=====================================
/Applications/CLion.app/Contents/bin/cmake/mac/bin/cmake --build /Users/bully/CLionProjects/OpenMPTest/cmake-build-debug --target all -- -j 2
Scanning dependencies of target OpenMPTest
[ 50%] Building CXX object CMakeFiles/OpenMPTest.dir/main.cpp.o
[100%] Linking CXX executable OpenMPTest
Undefined symbols for architecture x86_64:
"_omp_get_max_threads", referenced from:
_main in main.cpp.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make[2]: *** [OpenMPTest] Error 1
make[1]: *** [CMakeFiles/OpenMPTest.dir/all] Error 2
make: *** [all] Error 2
How come this does not work? From the command line I can run clang++ main.cpp -lomp successfully after installing the library headers with open /Library/Developer/CommandLineTools/Packages/macOS_SDK_headers_for_macOS_10.14.pkg.
message(STATUS "Linker flags:" "${OpenMP_EXE_LINKER_FLAGS}") prints:
-- Linker flags:
If I replace the CMAKE_EXE_LINKER_FLAGS setter with set (CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -lomp") compilation with linking works. Why do I have to specify this manually? What's the preferred way of getting this to work in a platform-independent way? -fopenmp yields clang: error: unsupported option '-fopenmp'. gcc and MSVC on Linux and Windows respectively played nice with the CMake OpenMP configuration but Mac OS X does not.
As commented by Tsyvarev, the solution is to use the "updated" way of including OpenMP in CMake:
cmake_minimum_required(VERSION 3.14)
project(OpenMPTest)
set(CMAKE_CXX_STANDARD 17)
add_executable(OpenMPTest main.cpp)
find_package(OpenMP REQUIRED) # Find the package
target_link_libraries(${PROJECT_NAME} ${OpenMP_CXX_LIBRARIES}) # Link against it for C++
This compiled on Windows, Ubuntu and Mac OS X using their platform's default compilers respectively.
The accepted answer here is not recommended even though it also has the most upvotes.
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/