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"
Related
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'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
)
I am trying to compile my code with CMake. But when I do the make command I get the following error:
[ 17%] Linking CXX shared library libglobal.so
Apple LLVM version 9.1.0 (clang-902.0.39.1)
Target: x86_64-apple-darwin17.5.0
Thread model: posix
InstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin
"/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/ld" -demangle -lto_library /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/libLTO.dylib -dynamic -dylib -arch x86_64 -dylib_install_name #rpath/libglobal.so -macosx_version_min 10.13.0 -o libglobal.so -L/opt/local/lib -L/Users/George/Documents/root/build/lib -lc++ -headerpad_max_install_names CMakeFiles/global.dir/RawHitPeta6.cpp.o CMakeFiles/global.dir/CorrectedHit.cpp.o CMakeFiles/global.dir/CalibrationSettings.cpp.o CMakeFiles/global.dir/Coincidence.cpp.o CMakeFiles/global.dir/AnalysisSettings.cpp.o CMakeFiles/global.dir/Plot.cpp.o CMakeFiles/global.dir/Destination.cpp.o CMakeFiles/global.dir/Crystal.cpp.o CMakeFiles/global.dir/Geometry.cpp.o CMakeFiles/global.dir/geometries/TwoModuleSetup.cpp.o CMakeFiles/global.dir/geometries/P6EvaluationBoard.cpp.o CMakeFiles/global.dir/geometries/SystemSingleLayer7.cpp.o CMakeFiles/global.dir/geometries/SystemSingleLayer8.cpp.o CMakeFiles/global.dir/geometries/SystemDualLayer.cpp.o CMakeFiles/global.dir/geometries/SystemHighResolutionDualLayer.cpp.o CMakeFiles/global.dir/geometries/SystemHighResolutionDualLayerLong.cpp.o -lc++ -lSystem /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/clang/9.1.0/lib/darwin/libclang_rt.osx.a
Undefined symbols for architecture x86_64:
"TVersionCheck::TVersionCheck(int)", referenced from:
__GLOBAL__sub_I_Plot.cpp in Plot.cpp.o
__GLOBAL__sub_I_Destination.cpp in Destination.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]: *** [Analysis/global/libglobal.so] Error 1
make[1]: *** [Analysis/global/CMakeFiles/global.dir/all] Error 2
make: *** [all] Error 2
I already looked for ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation).
This is the cmake outout I get:
CMake Warning (dev):
Policy CMP0042 is not set: MACOSX_RPATH is enabled by default. Run "cmake
--help-policy CMP0042" for policy details. Use the cmake_policy command to
set the policy and suppress this warning.
MACOSX_RPATH is not specified for the following targets:
coincidencePlots
correctedHitPlots
generalPlots
global
processing
rawHitPlots
This warning is for project developers. Use -Wno-dev to suppress it.
-- Generating done
-- Build files have been written to: /Users/George/Desktop/petaAnalysis/build
I already looked for ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation). But I could not find anything useful, that helped.
This is the cmake output I get:
CMake Warning (dev):
Policy CMP0042 is not set: MACOSX_RPATH is enabled by default. Run "cmake
--help-policy CMP0042" for policy details. Use the cmake_policy command to
set the policy and suppress this warning.
MACOSX_RPATH is not specified for the following targets:
coincidencePlots
correctedHitPlots
generalPlots
global
processing
countRawPlots
This warning is for project developers. Use -Wno-dev to suppress it.
-- Generating done
-- Build files have been written to: /Users/Hans/Desktop/Project/build
I tried to run the code on linux. There it seems works fine. I think it is a linking problem between the shared libraries I created.
This is are the CMAKE-files:
File 1(in main directory):
cmake_minimum_required(VERSION 2.8)
project(Analysis)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${Analysis_SOURCE_DIR}/bin)
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -pthread -stdlib=libc++ -lstdc++ -std=c++14 -m64 -I/Users/George/Documents/root/build/include")
#SET(CMAKE_BUILD_WITH_INSTALL_RPATH true)
#SET(CMAKE_INSTALL_RPATH "$ORIGIN/")
SET(CMAKE_SYSTEM_NAME Darwin)
# Add MacPorts
INCLUDE_DIRECTORIES(/opt/local/include)
LINK_DIRECTORIES(/opt/local/lib)
ADD_SUBDIRECTORY(Analysis)
ADD_SUBDIRECTORY(CrystalMapGenerator)
ADD_SUBDIRECTORY(CoincidenceBrowser)
ADD_SUBDIRECTORY(FunctionTests)
ADD_SUBDIRECTORY(AnalysisResultExtractor)
ADD_SUBDIRECTORY(AnalysisComparison)
ADD_SUBDIRECTORY(SDIPRawDataDebug)
ADD_SUBDIRECTORY(GateDataTimeCalibrationTester)
File 2(in subdirectory):
cmake_minimum_required(VERSION 2.8)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${Analysis_SOURCE_DIR}/bin)
if(UNIX)
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -std=gnu++0x")
endif()
#set(CMAKE_BUILD_TYPE Debug)
set(CMAKE_BUILD_TYPE Release)
# Add information on ROOT libraries
INCLUDE(../FindROOT.cmake)
INCLUDE_DIRECTORIES( ${ROOTSYS}/include)
LINK_DIRECTORIES( ${ROOTSYS}/lib)
ADD_SUBDIRECTORY(processing)
ADD_SUBDIRECTORY(generalPlots)
ADD_SUBDIRECTORY(rawHitPlots)
ADD_SUBDIRECTORY(correctedHitPlots)
ADD_SUBDIRECTORY(coincidencePlots)
ADD_SUBDIRECTORY(global)
include_directories("processing")
include_directories("generalPlots")
include_directories("rawHitPlots")
include_directories("correctedHitPlots")
include_directories("coincidencePlots")
include_directories("global")
set(SOURCES
DataChain.cpp
main.cpp
)
ADD_EXECUTABLE(analysis ${SOURCES})
target_link_libraries(analysis processing generalPlots rawHitPlots correctedHitPlots coincidencePlots global ${ROOT_LIBRARIES} -lSpectrum -lMinuit)
File3(in folder of library global):
cmake_minimum_required(VERSION 2.8)
INCLUDE(../../FindROOT.cmake)
INCLUDE_DIRECTORIES( ${ROOT_INCLUDE_DIR})
LINK_DIRECTORIES( ${ROOT_LIBRARY_DIR})
ADD_LIBRARY(global SHARED
RawHitPeta6.cpp
CorrectedHit.cpp
CalibrationSettings.cpp
Coincidence.cpp
AnalysisSettings.cpp
Plot.cpp
Destination.cpp
Crystal.cpp
Geometry.cpp
geometries/TwoModuleSetup.cpp
geometries/P6EvaluationBoard.cpp
geometries/SystemSingleLayer7.cpp
geometries/SystemSingleLayer8.cpp
geometries/SystemDualLayer.cpp
geometries/SystemHighResolutionDualLayer.cpp
geometries/SystemHighResolutionDualLayerLong.cpp
)
Thanks alot!
If you're using a recent version of ROOT5 or ROOT6, instead of writing or downloading a FindROOT.cmake, use the info that ROOT auto-generates for you when your (or your package system) builds it.
In your ROOT installation, find the directory that holds ROOTConfig.cmake. Add that directory to your CMAKE_MODULE_PATH. Then, call find_package(ROOT).
A basic CMakeLists.txt could look like this:
cmake_minimum_required(VERSION 3.10.2 FATAL_ERROR)
execute_process(COMMAND root-config --prefix
COMMAND tr -d \\n
RESULT_VARIABLE RC_RESULT
OUTPUT_VARIABLE ROOT_PREFIX)
list(APPEND CMAKE_MODULE_PATH "${ROOT_PREFIX}/share/root/cmake")
find_package(ROOT)
include(CMakePrintHelpers)
cmake_print_variables(ROOT_FOUND ROOT_LIBRARIES ROOT_INCLUDE_DIRS)
Running cmake with that results in:
~/rt
❯ cmake .
-- The C compiler identification is AppleClang 9.1.0.9020039
-- The CXX compiler identification is AppleClang 9.1.0.9020039
-- Check for working C compiler: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/cc
-- Check for working C compiler: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/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: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/c++
-- Check for working CXX compiler: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- ROOT_FOUND="1" ; ROOT_LIBRARIES="/usr/local/lib/root/libCore.so;/usr/local/lib/root/libImt.so;/usr/local/lib/root/libRIO.so;/usr/local/lib/root/libNet.so;/usr/local/lib/root/libHist.so;/usr/local/lib/root/libGraf.so;/usr/local/lib/root/libGraf3d.so;/usr/local/lib/root/libGpad.so;/usr/local/lib/root/libTree.so;/usr/local/lib/root/libTreePlayer.so;/usr/local/lib/root/libRint.so;/usr/local/lib/root/libPostscript.so;/usr/local/lib/root/libMatrix.so;/usr/local/lib/root/libPhysics.so;/usr/local/lib/root/libMathCore.so;/usr/local/lib/root/libThread.so;/usr/local/lib/root/libMultiProc.so" ; ROOT_INCLUDE_DIRS="/usr/local/include/root"
-- Configuring done
-- Generating done
-- Build files have been written to: /Users/nega/rt
~/rt
❯
You'll notice that ROOT_FOUND has been set to TRUE, and ROOT_LIBRARIES, and ROOT_INCLUDE_DIRS have been populated. You can now use those variables to setup your compilation and linking.
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/
The tool include-what-you-use can be used to detect unneeded headers. I am using CMake for my C++ software project. How can I instruct CMake to run include-what-you-use automatically on the source files of my software project?
CMake 3.3 introduced the new target property CXX_INCLUDE_WHAT_YOU_USE that can be set to the path of the program include-what-you-use. For instance this CMakeLists.txt
cmake_minimum_required(VERSION 3.3 FATAL_ERROR)
add_executable(hello main.cc)
find_program(iwyu_path NAMES include-what-you-use iwyu REQUIRED)
# If using CGAL<3.18, you remove REQUIRED and use
# if(NOT iwyu_path)
# message(FATAL_ERROR "Could not find the program include-what-you-use")
# endif()
set_property(TARGET hello PROPERTY CXX_INCLUDE_WHAT_YOU_USE ${iwyu_path})
is able to build the file main.cc
#include <iostream>
#include <vector>
int main() {
std::cout << "Hello World!" << std::endl;
return 0;
}
and at the same time have include-what-you-use give out a warning that
the included header vector is not needed.
user#ubuntu:/tmp$ ls ~/hello
CMakeLists.txt main.cc
user#ubuntu:/tmp$ mkdir /tmp/build
user#ubuntu:/tmp$ cd /tmp/build
user#ubuntu:/tmp/build$ ~/cmake-3.3.0-rc2-Linux-x86_64/bin/cmake ~/hello
-- The C compiler identification is GNU 4.9.2
-- The CXX compiler identification is GNU 4.9.2
-- 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: /tmp/build
user#ubuntu:/tmp/build$ make
Scanning dependencies of target hello
[ 50%] Building CXX object CMakeFiles/hello.dir/main.cc.o
Warning: include-what-you-use reported diagnostics:
/home/user/hello/main.cc should add these lines:
/home/user/hello/main.cc should remove these lines:
- #include <vector> // lines 2-2
The full include-list for /home/user/hello/main.cc:
#include <iostream> // for operator<<, basic_ostream, cout, endl, ostream
---
[100%] Linking CXX executable hello
[100%] Built target hello
user#ubuntu:/tmp/build$ ./hello
Hello World!
user#ubuntu:/tmp/build$
If you want to pass custom options to include-what-you-use, like for instance --mapping_file you can do it via
set(iwyu_path_and_options
${iwyu_path}
-Xiwyu
--mapping_file=${my_mapping})
set_property(TARGET hello
PROPERTY CXX_INCLUDE_WHAT_YOU_USE ${iwyu_path_and_options})
If you don't have access to CMake 3.3, include-what-you-use comes with a python tool called iwyu_tool.py which can do what you want.
It works by parsing a JSON compilation database, which is easily produced with CMake (see below).
Running the tool manually
Assuming you already have a CMake build dir for your project, you first need to tell CMake to produce the compilation database:
$ cd build
$ cmake -DCMAKE_EXPORT_COMPILE_COMMANDS=ON .
This generates a file, compile_commands.json containing compiler invocations for every object file in your project. You don't need to rebuild the project.
You can now run include-what-you-use on your project by running the python tool on your build directory:
$ python /path/to/iwyu_tool.py -p .
Adding a custom target to your cmake project
The following snippet can be used to add an iwyu target to a cmake project.
# Generate clang compilation database
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
find_package(PythonInterp)
find_program(iwyu_tool_path NAMES iwyu_tool.py)
if (iwyu_tool_path AND PYTHONINTERP_FOUND)
add_custom_target(iwyu
ALL # Remove ALL if you don't iwyu to be run by default.
COMMAND "${PYTHON_EXECUTABLE}" "${iwyu_tool_path}" -p "${CMAKE_BINARY_DIR}"
COMMENT "Running include-what-you-use tool"
VERBATIM
)
endif()
Notes
The include-what-you-use binary needs to be in your path for any of the above to work properly.
By default, iwyu_tool.py is single-threaded, which can be slow for large projects. You can use the --jobs argument to increase the number of source files that will be processed in parallel.
You can also enable it globally outside the cmake script by setting the cmake variable:
cmake -DCMAKE_CXX_INCLUDE_WHAT_YOU_USE="iwyu" <builddir>
It will then call it on each CXX target.
I extended the source code from Alastair Harrison, in order to create a reusable solution. I've came up with the following that should work with all CMake versions:
File iwyu.cmake:
#.rst:
# include-what-you-use (iwyu)
# ----------------------------
#
# Allows to run the static code analyzer `include-what-you-use (iwyu)
# <http://include-what-you-use.org>`_ as a custom target with the build system
# `CMake <http://cmake.org>`_.
#
# .. topic:: Dependencies
#
# This module requires the following *CMake* modules:
#
# * ``FindPythonInterp``
#
# .. topic:: Contributors
#
# * Florian Wolters <wolters.fl#gmail.com>
#===============================================================================
# Copyright 2015 Florian Wolters
#
# Distributed under the Boost Software License, Version 1.0. (See accompanying
# file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
#===============================================================================
# ------------------------------------------------------------------------------
# Include guard for this file.
# ------------------------------------------------------------------------------
if(iwyu_included)
return()
endif()
set(iwyu_included TRUE)
option(BUILD_IWYU
"Run the include-what-you-use static analyzer on the source code of the project."
OFF)
function(iwyu_enable)
set(iwyu_EXECUTABLE_NAME include-what-you-use)
find_program(iwyu_EXECUTABLE ${iwyu_EXECUTABLE_NAME})
if(iwyu_EXECUTABLE)
# This is not exactly the same behavior as with CMake v3.3, since here all
# compiled targets are analyzed.
set(iwyu_tool_EXECUTABLE_NAME iwyu_tool.py)
find_package(PythonInterp)
find_program(iwyu_tool_EXECUTABLE ${iwyu_tool_EXECUTABLE_NAME})
if(PYTHONINTERP_FOUND AND iwyu_tool_EXECUTABLE)
set(CMAKE_EXPORT_COMPILE_COMMANDS ON PARENT_SCOPE)
add_custom_target(iwyu
ALL
COMMAND "${PYTHON_EXECUTABLE}" "${iwyu_tool_EXECUTABLE}" -p "${CMAKE_BINARY_DIR}"
COMMENT "Running the ${iwyu_tool_EXECUTABLE_NAME} compilation database driver"
VERBATIM)
else()
message(STATUS
"Unable to find the Python interpreter and/or the ${iwyu_tool_EXECUTABLE_NAME} script")
endif()
else()
message(STATUS "Unable to find the ${iwyu_EXECUTABLE_NAME} executable")
endif()
endfunction()
File CMakeLists.txt:
cmake_minimum_required(VERSION 3.0)
include(iwyu.cmake)
project(hello_world)
add_executable(${PROJECT_NAME} main.cc)
if(BUILD_IWYU)
iwyu_enable()
endif()
Invoke CMake as follows to run include-what-you-use when the all target is invoked:
cmake -DBUILD_IWYU=ON <path-to-source>
cmake --build . --target all
The output should be as follows:
-- Configuring done
-- Generating done
-- Build files have been written to: /home/wolters/workspace/include-what-you-use_example/build
[ 66%] Built target hello_world
[100%] Running the iwyu_tool.py compilation database driver
/home/wolters/workspace/include-what-you-use_example/develop/main.cc should add these lines:
/home/wolters/workspace/include-what-you-use_example/develop/main.cc should remove these lines:
- #include <vector> // lines 1-1
The full include-list for /home/wolters/workspace/include-what-you-use_example/develop/main.cc:
#include <iostream> // for operator<<, basic_ostream, cout, endl, ostream
---
[100%] Built target iwyu
Edit 2015-08-19: The approach with the CMake property <LANG>_INCLUDE_WHAT_YOU_USE did not work for me with CMake version 3.3.1. Therefore I updated the solution.
Edit 2015-09-30: The output was wrong, since my include-what-you-use installation was broken. The include-what-you-use and iwyu_tool.py files have to be in the same directory as clang, clang++, etc.