Cannot link boost libraries with CMake - c++

I have a sample C++ code that uses boost (program options module) as shown below:
#include <iostream>
#include <boost/program_options/options_description.hpp>
#include <boost/program_options/parsers.hpp>
#include <boost/program_options/variables_map.hpp>
namespace po = boost::program_options;
int main(int argc, const char* argv[]){
po::options_description description("MyTool Usage");
description.add_options()
("help,h", "Display this help message")
("version,v", "Display the version number");
po::positional_options_description p;
po::variables_map vm;
po::store(po::command_line_parser(argc, argv).options(description).positional(p).run(), vm);
po::notify(vm);
if(vm.count("help")){
std::cout << description;
return 0;
}
if(vm.count("version")){
std::cout << "MyTool Version 1.0" << std::endl;
return 0;
}
return 0;
}
I tried to compile using cmake. The corresponding CMakeLists.txt file is shown below:
cmake_minimum_required(VERSION 2.6)
set(CMAKE_CXX_LINK_FLAGS "-std=c++11 -g -Wall -fsigned-char -lboost_program_options-mt")
set(BOOST_ROOT /opt/local/include/boost)
set(BOOST_LIBRARYDIR /opt/local/lib)
find_package(Boost COMPONENTS program_options system filesystem REQUIRED)
include_directories(${BOOST_INCLUDE_DIR})
link_libraries(${BOOST_LIBRARIES})
add_executable(testboostpo testboostpo_simple.cpp)
target_link_libraries(testboostpo ${Boost_LIBRARIES})
Although cmake . seems to succeed and it also detects all the boost modules I typically use (program options, system, filesystem),
$ cmake .
-- The C compiler identification is GNU 5.5.0
-- The CXX compiler identification is GNU 5.5.0
-- Checking whether C compiler has -isysroot
-- Checking whether C compiler has -isysroot - yes
-- Checking whether C compiler supports OSX deployment target flag
-- Checking whether C compiler supports OSX deployment target flag - yes
-- Check for working C compiler: /opt/local/bin/gcc-mp-5
-- Check for working C compiler: /opt/local/bin/gcc-mp-5 -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Checking whether CXX compiler has -isysroot
-- Checking whether CXX compiler has -isysroot - yes
-- Checking whether CXX compiler supports OSX deployment target flag
-- Checking whether CXX compiler supports OSX deployment target flag - yes
-- Check for working CXX compiler: /opt/local/bin/g++-mp-5
-- Check for working CXX compiler: /opt/local/bin/g++-mp-5 -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Boost version: 1.66.0
-- Found the following Boost libraries:
-- program_options
-- system
-- filesystem
-- Configuring done
-- Generating done
-- Build files have been written to: /Users/params/libraries/coding_interview/every_test/boost_test
the next step make fails with the following error:
$ make
Scanning dependencies of target testboostpo
[ 50%] Building CXX object CMakeFiles/testboostpo.dir/testboostpo_simple.cpp.o
[100%] Linking CXX executable testboostpo
ld: library not found for -lboost_program_options-mt
collect2: error: ld returned 1 exit status
make[2]: *** [testboostpo] Error 1
make[1]: *** [CMakeFiles/testboostpo.dir/all] Error 2
make: *** [all] Error 2
There seems to be a problem linking boost library for program options. However, I can see that the library is installed at /opt/local/lib/libboost_program_options-mt.dylib
Alternatively, I also tried isolating the compilation but it fails as well.
$ g++ -L/opt/local/lib testboostpo_simple.cpp -lboost_program_options-mt
Undefined symbols for architecture x86_64:
"boost::program_options::to_internal(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)", referenced from:
std::vector<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::allocator<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > > > boost::program_options::to_internal<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >(std::vector<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::allocator<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > > > const&) in ccg8tggM.o
ld: symbol(s) not found for architecture x86_64
collect2: error: ld returned 1 exit status
$ g++ -L/opt/local/lib testboostpo_simple.cpp /opt/local/lib/libboost_program_options-mt.dylib
Undefined symbols for architecture x86_64:
"boost::program_options::to_internal(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)", referenced from:
std::vector<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::allocator<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > > > boost::program_options::to_internal<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >(std::vector<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::allocator<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > > > const&) in ccg8tggM.o
ld: symbol(s) not found for architecture x86_64
collect2: error: ld returned 1 exit status
I am using gcc5, boost 1.66 and cmake 3.12 (all of them installed using macports).
boost header files are in location:
/opt/local/include/boost/
and the boost library files are in location:
/opt/local/lib
Does anybody here know what might be causing the boost linking to still fail despite providing the paths? Anything else missing here or incorrectly specified?
Thanks,

Because you have used the CMAKE_CXX_LINK_FLAGS, and cmake will check it first instead of using your target_link_libraries, if you want to do it by yourself, you need to add -L/opt/local/lib as well, the simpler way is let cmake do it for you. The following is my example:
cmake_minimum_required(VERSION 2.6)
project(server CXX)
set(CXX_FLAGS
-g
-Wall
)
# set(Boost_NO_SYSTEM_PATHS ON)
set(BOOST_ROOT /mnt/d/Code/boost)
set(BOOST_LIBRARYDIR /mnt/d/Code/boost/stage/lib)
find_package(Threads REQUIRED)
find_package(Boost 1.68.0 COMPONENTS program_options REQUIRED)
file(GLOB SRC
"*cpp*"
)
if(Boost_FOUND)
include_directories(${Boost_INCLUDE_DIRS})
message(${Boost_PROGRAM_OPTIONS_LIBRARIES})
add_executable(main ${SRC})
target_link_libraries(main Threads::Threads ${Boost_PROGRAM_OPTIONS_LIBRARIES})
endif()
Besides, if you build it separately, you should first compile the object then link, or it may turn into undefined reference. In your case, you should link at the last, the result will be different when you put the link in the front or the tail, I am sorry I don't why this happening.

It looks like you're trying to link a 32-bit library against a 64-bit build. The hint is ld: symbol(s) not found for architecture x86_64.
Linking libraries requires that you match the exact build type. Here's some more info about the linking process.
https://www.boost.org/doc/libs/1_63_0/more/getting_started/windows.html

Related

CMake finds Boost, but fails to link

I am trying to do the tutorial on the boost website (https://www.boost.org/doc/libs/1_81_0/doc/html/program_options/tutorial.html), but I cannot get cmake to link to boost. Right now I am only trying to link to program_options. I have pulled boost from github and built it.
intensity.cpp
#include <iostream>
#include <boost/program_options.hpp>
namespace po = boost::program_options;
int main(int ac, char *av[]){
po::options_description desc("allowed options");
desc.add_options()
("help", "--intesity: set intesity levels")
("intensity", po::value<int>(), "set intensity level")
;
po::variables_map vm;
po::store(po::parse_command_line(ac, av, desc), vm);
po::notify(vm);
if(vm.count("help")) {
std::cout << desc << "\n";
return 1;
}
if(vm.count("intensity")) {
std::cout << "intensity level set to "
<< vm["intensity"].as<int>() << "\n";
} else {
std::cout << "intesity was not set\n";
}
return 0;
}
CMakeLists.txt
cmake_minimum_required(VERSION 3.15)
project(Intensity VERSION 1.0)
set(CMAKE_CXX_COMPILER clang)
set(BOOST_ROOT "/Users/nabiel.kandiel/code/thirdparty/boost_1_81_0")
set(Boost_USE_STATIC_LIBS OFF)
set(BOOST_USE_MULTITHREADED ON)
set(BOOST_USE_STATIC_RUNTIME OFF)
find_package(Boost 1.81 REQUIRED COMPONENTS program_options)
add_executable(intensity intensity.cpp)
target_link_libraries(intensity Boost::program_options)
cmake output
NKandiel-REMM22:build nabiel.kandiel$ cmake ../
-- The C compiler identification is AppleClang 14.0.0.14000029
-- The CXX compiler identification is AppleClang 14.0.0.14000029
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working C compiler: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/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: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/c++ - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Found Boost: /opt/homebrew/lib/cmake/Boost-1.81.0/BoostConfig.cmake (found suitable version "1.81.0", minimum required is "1.81") found components: program_options
-- Configuring done
-- Generating done
-- Build files have been written to: /Users/nabiel.kandiel/code/imageProc/build
cmake build
NKandiel-REMM22:build nabiel.kandiel$ cmake --build .
[ 50%] Building CXX object CMakeFiles/intensity.dir/intensity.cpp.o
[100%] Linking CXX executable intensity
Undefined symbols for architecture arm64:
"std::logic_error::what() const", referenced from:
vtable for boost::program_options::error in intensity.cpp.o
"std::runtime_error::what() const", referenced from:
vtable for boost::wrapexcept<boost::bad_function_call> in intensity.cpp.o
vtable for boost::bad_function_call in intensity.cpp.o
"std::__1::__vector_base_common<true>::__throw_length_error() const", referenced
.....
std::__1::allocator<boost::program_options::basic_option<char> >::deallocate(boost::program_options::basic_option<char>*, unsigned long) in intensity.cpp.o
...
ld: symbol(s) not found for architecture arm64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make[2]: *** [intensity] Error 1
make[1]: *** [CMakeFiles/intensity.dir/all] Error 2
make: *** [all] Error 2
I have tried using a combination of CMakeLists.txt I have found online and none have worked.

CMake - undefined symbols encountered when building application depends on PcapPlusPlus

I programmed a simple application using PcapPlusPlus library with CLion on MacOS.
The code is really simple:
#include "../include/PCPP/PcapFileDevice.h"
int main(int argc, char* argv[])
{
std::string path;
pcpp::PcapFileReaderDevice reader(path);
reader.open();
reader.close();
return 0;
}
Here is CMakeLists.txt:
cmake_minimum_required(VERSION 3.5)
set(CMAKE_CXX_STANDARD 11)
project (Test CXX)
add_executable(cmd_main ${PROJECT_SOURCE_DIR}/test/cmd_main.cpp)
target_include_directories(cmd_main
PUBLIC
${PROJECT_SOURCE_DIR}/include)
target_link_libraries(cmd_main
PUBLIC
${PROJECT_SOURCE_DIR}/lib/libCommon++.a
${PROJECT_SOURCE_DIR}/lib/libPacket++.a
${PROJECT_SOURCE_DIR}/lib/libPcap++.a
)
The directory is also simple:
|
|--include
|----PCPP/
|--lib
|----libCommon++.a
|----libPacket++.a
|----libPcap++.a
|--test
|----cmd_main.cpp
Here is compile result:
(base) 2ir0#iMac Test % cmake .
-- Configuring done
-- Generating done
-- Build files have been written to: /Users/2ir0/Documents/Maltrace
(base) 2ir0#iMac Test % make
Scanning dependencies of target cmd_main
[ 50%] Building CXX object CMakeFiles/cmd_main.dir/test/cmd_main.cpp.o
[100%] Linking CXX executable cmd_main
Undefined symbols for architecture x86_64:
"_pcap_close", referenced from:
pcpp::IFileDevice::close() in libPcap++.a(PcapFileDevice.o)
pcpp::PcapFileReaderDevice::open() in libPcap++.a(PcapFileDevice.o)
"_pcap_compile", referenced from:
pcpp::IPcapDevice::setFilter(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >) in libPcap++.a(PcapDevice.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]: *** [cmd_main] Error 1
make[1]: *** [CMakeFiles/cmd_main.dir/all] Error 2
make: *** [all] Error 2
It seems cmd_main didn't link PcapPlusPlus library. But why?
I read the error information carefully, found that PcapPlusPlus need libpcap library, so I added libpcap.a, and fixed the problem.

Fail to run the example of 3D Surface Mesh Generation with CGAL

I am trying to run this example of 3D Surface Mesh Generation with CGAL. The code is:
#include <CGAL/Surface_mesh_default_triangulation_3.h>
#include <CGAL/Complex_2_in_triangulation_3.h>
#include <CGAL/make_surface_mesh.h>
#include <CGAL/Implicit_surface_3.h>
// default triangulation for Surface_mesher
typedef CGAL::Surface_mesh_default_triangulation_3 Tr;
// c2t3
typedef CGAL::Complex_2_in_triangulation_3<Tr> C2t3;
typedef Tr::Geom_traits GT;
typedef GT::Sphere_3 Sphere_3;
typedef GT::Point_3 Point_3;
typedef GT::FT FT;
typedef FT (*Function)(Point_3);
typedef CGAL::Implicit_surface_3<GT, Function> Surface_3;
FT sphere_function (Point_3 p) {
const FT x2=p.x()*p.x(), y2=p.y()*p.y(), z2=p.z()*p.z();
return x2+y2+z2-1;
}
int main() {
Tr tr; // 3D-Delaunay triangulation
C2t3 c2t3 (tr); // 2D-complex in 3D-Delaunay triangulation
// defining the surface
Surface_3 surface(sphere_function, // pointer to function
Sphere_3(CGAL::ORIGIN, 2.)); // bounding sphere
// Note that "2." above is the *squared* radius of the bounding sphere!
// defining meshing criteria
CGAL::Surface_mesh_default_criteria_3<Tr> criteria(30., // angular bound
0.1, // radius bound
0.1); // distance bound
// meshing surface
CGAL::make_surface_mesh(c2t3, surface, criteria, CGAL::Non_manifold_tag());
std::cout << "Final number of points: " << tr.number_of_vertices() << "\n";
}
The CMakeLists file contains:
# Created by the script cgal_create_CMakeLists
# This is the CMake script for compiling a set of CGAL applications.
cmake_minimum_required(VERSION 3.7)
project(_cgal)
# CGAL and its components
find_package( CGAL QUIET COMPONENTS Core )
if ( NOT CGAL_FOUND )
message(STATUS "This project requires the CGAL library, and will not be compiled.")
return()
endif()
# include helper file
include( ${CGAL_USE_FILE} )
# Boost and its components
find_package( Boost REQUIRED )
if ( NOT Boost_FOUND )
message(STATUS "This project requires the Boost library, and will not be compiled.")
return()
endif()
# Creating entries for target: out
# ############################
set(CMAKE_CXX_STANDARD 11)
set(SOURCE_FILES main.cpp)
add_executable(_cgal ${SOURCE_FILES})
add_to_cached_list( CGAL_EXECUTABLE_TARGETS _cgal )
# Link the executable to CGAL and third-party libraries
target_link_libraries(_cgal ${CGAL_LIBRARIES} ${CGAL_3RD_PARTY_LIBRARIES} )
Output of cmake:
-- The C compiler identification is GNU 7.1.0
-- The CXX compiler identification is GNU 7.1.0
-- Checking whether C compiler has -isysroot
-- Checking whether C compiler has -isysroot - yes
-- Checking whether C compiler supports OSX deployment target flag
-- Checking whether C compiler supports OSX deployment target flag - yes
-- Check for working C compiler: /usr/local/bin/gcc-7
-- Check for working C compiler: /usr/local/bin/gcc-7 -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Checking whether CXX compiler has -isysroot
-- Checking whether CXX compiler has -isysroot - yes
-- Checking whether CXX compiler supports OSX deployment target flag
-- Checking whether CXX compiler supports OSX deployment target flag - yes
-- Check for working CXX compiler: /usr/local/bin/g++-7
-- Check for working CXX compiler: /usr/local/bin/g++-7 -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Build type: Release
-- USING CXXFLAGS = ' -DNDEBUG'
-- USING EXEFLAGS = ' '
-- Targetting Unix Makefiles
-- Using /usr/local/bin/g++-7 compiler.
-- DARWIN_VERSION=16
-- Mac Leopard detected
-- Requested component: Core
-- Requested component: MPFR
-- Requested component: GMP
-- Boost version: 1.64.0
-- Configuring done
-- Generating done
However, I am getting the following error:
Scanning dependencies of target _cgal
[ 50%] Building CXX object CMakeFiles/_cgal.dir/main.cpp.o
[100%] Linking CXX executable _cgal
Undefined symbols for architecture x86_64:
"CGAL::get_mode(std::basic_ios<char, std::char_traits<char> >&)", referenced from:
std::basic_ostream<char, std::char_traits<char> >& CGAL::insert<CGAL::Epick>(std::basic_ostream<char, std::char_traits<char> >&, CGAL::Point_3<CGAL::Epick> const&, CGAL::Cartesian_tag const&) in main.cpp.o
ld: symbol(s) not found for architecture x86_64
collect2: error: ld returned 1 exit status
make[3]: *** [_cgal] Error 1
make[2]: *** [CMakeFiles/_cgal.dir/all] Error 2
make[1]: *** [CMakeFiles/_cgal.dir/rule] Error 2
make: *** [_cgal] Error 2
I would highly appreciate if you could explain me how to fix it.
UPD: This seems to be related, but do not get what packages I should add in my cmake.
UPD2: I found that it may be related to the issue of:
There are two implementations of the standard C++ library available on
OS X: libstdc++ and libc++. They are not binary compatible and libMLi3
requires libstdc++.
On 10.8 and earlier libstdc++ is chosen by default, on 10.9 libc++ is
chosen by default. To ensure compatibility with libMLi3, we need to
choose libstdc++ manually.
To do this, add -stdlib=libstdc++ to the linking command.
I modified the following line in the CMake file, but it did not help:
# Link the executable to CGAL and third-party libraries
target_link_libraries(_cgal ${CGAL_LIBRARIES} ${CGAL_3RD_PARTY_LIBRARIES} -static-libstdc++)
UPD3: I tried the solution suggested here, however it does not help
project( _cgal )
cmake_minimum_required(VERSION 2.8.10)
find_package(CGAL QUIET COMPONENTS Core )
if ( CGAL_FOUND )
include( ${CGAL_USE_FILE} )
include( CGAL_CreateSingleSourceCGALProgram )
create_single_source_cgal_program( "main.cpp" )
else()
message(STATUS "This program requires the CGAL library, and will not be compiled.")
endif()
The answer is: fix compiler! CGAL was installed with brew that uses clang compiler. But to compile the project I had been using g++. I should code in C++ more often :)

How to link Boost library with CMake (on a cluster if boost is in non-standard location)?

I have a strange problem, when I try to build a simple Boost test program on a cluster.
On my machine, everything works just fine.
First the example:
//main.cpp
#include <iostream>
#include <string>
#include "boost/program_options.hpp"
namespace po = boost::program_options;
int main( int argc , char* argv[] ) {
po::options_description desc("Allowed options");
desc.add_options()
("help", "produce help message")
("greet", po::value<std::string>()->default_value("World"), "the greeting")
;
po::variables_map vm;
po::store(po::parse_command_line(argc, argv, desc), vm);
std::cout << "Hello, " << vm["greet"].as<std::string>() << "!" << std::endl;
return 0;
}
And the CMakeLists.txt
cmake_minimum_required(VERSION 3.5)
project(boost_test)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
find_package(Boost COMPONENTS program_options)
set(SOURCE_FILES main.cpp)
if(Boost_FOUND)
add_executable(boost_test ${SOURCE_FILES})
target_include_directories(boost_test PRIVATE ${Boost_INCLUDE_DIRS})
target_link_libraries(boost_test PRIVATE ${Boost_LIBRARIES})
endif(Boost_FOUND)
I use cmake like
cmake -G "Unix Makefiles" /path/to/code
make
The error I get is a undefined reference error during linking:
Linking CXX executable boost_test
CMakeFiles/boost_test.dir/main.cpp.o: In function `main':
main.cpp:(.text+0x6d): undefined reference to `boost::program_options::options_description::options_description(std::string const&, unsigned int, unsigned int)'
CMakeFiles/boost_test.dir/main.cpp.o: In function `boost::program_options::variables_map::operator[](std::string const&) const':
main.cpp:(.text._ZNK5boost15program_options13variables_mapixERKSs[_ZNK5boost15program_options13variables_mapixERKSs]+0x1f): undefined reference to `boost::program_options::abstract_variables_map::operator[](std::string const&) const'
and so on ...
So I thought that maybe the installation of boost is not correct and I tried to write a makefile myself.
boost-test-1: ../code/main.cpp
g++ ../code/main.cpp -o boost-test-1 -lboost_program_options
boost-test-2: ../code/main.cpp
g++ ../code/main.cpp -o boost-test-2 /some/fancy/cluster/path/certainly/non/standard/Boost/lib/libboost_program_options.so
Both ways to build the program work fine. But what cmake is doing is basically the following: first, build a object file, second link the object file and the library together and that fails.
boost-test-3: ../code/main.cpp
/usr/bin/c++ -I/some/fancy/cluster/path/certainly/non/standard/Boost/include -std=c++11 -o main.cpp.o -c ../code/main.cpp
/usr/bin/c++ -std=c++11 main.cpp.o -o boost-test-3 /some/fancy/cluster/path/certainly/non/standard/Boost/lib/libboost_program_options.so
As already mentioned, on my computer all described ways to build the program work, but on the cluster, the cmake way unfortunately fails. The problem is that I have another larger project that uses cmake and I am looking for a way to build it on that particular cluster.
Do you have any idea what could cause the problem and how to solve it?
Thank you!
Update: This is the whole output of the build process. I shortened the paths to cmake and boost to "path/to/cmake" and "path/to/boost" to make it shorter and better readable, but it is the same long path as described above.
$ cmake -G "Unix Makefiles" ../code/
-- The C compiler identification is GNU 4.8.5
-- The CXX compiler identification is GNU 4.8.5
-- 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
-- Boost version: 1.61.0
-- Found the following Boost libraries:
-- program_options
-- Boost include directories (Boost_INCLUDE_DIRS): /path/to/Boost/include
-- Link directories for Boost libraries (Boost_LIBRARY_DIRS): /path/to/Boost/lib
-- Boost component libraries to be linked (Boost_LIBRARIES): /path/to/Boost/lib/libboost_program_options.so
-- Configuring done
-- Generating done
-- Build files have been written to: /home/test/build
$ make VERBOSE=1
/path/to/CMake/bin/cmake -H/home/test/code -B/home/test/build --check-build-system CMakeFiles/Makefile.cmake 0
/path/to/CMake/bin/cmake -E cmake_progress_start /home/test/build/CMakeFiles /home/test/build/CMakeFiles/progress.marks
make -f CMakeFiles/Makefile2 all
make[1]: Entering directory `/home/test/build'
make -f CMakeFiles/boost_test.dir/build.make CMakeFiles/boost_test.dir/depend
make[2]: Entering directory `/home/test/build'
cd /home/test/build && /path/to/CMake/bin/cmake -E cmake_depends "Unix Makefiles" /home/test/code /home/test/code /home/test/build /home/test/build /home/test/build/CMakeFiles/boost_test.dir/DependInfo.cmake --color=
Dependee "/home/test/build/CMakeFiles/boost_test.dir/DependInfo.cmake" is newer than depender "/home/test/build/CMakeFiles/boost_test.dir/depend.internal".
Dependee "/home/test/build/CMakeFiles/CMakeDirectoryInformation.cmake" is newer than depender "/home/test/build/CMakeFiles/boost_test.dir/depend.internal".
Scanning dependencies of target boost_test
make[2]: Leaving directory `/home/test/build'
make -f CMakeFiles/boost_test.dir/build.make CMakeFiles/boost_test.dir/build
make[2]: Entering directory `/home/test/build'
[ 50%] Building CXX object CMakeFiles/boost_test.dir/main.cpp.o
/usr/bin/c++ -I/path/to/Boost/include -std=c++11 -o CMakeFiles/boost_test.dir/main.cpp.o -c /home/test/code/main.cpp
[100%] Linking CXX executable boost_test
/path/to/CMake/bin/cmake -E cmake_link_script CMakeFiles/boost_test.dir/link.txt --verbose=1
/usr/bin/c++ -std=c++11 CMakeFiles/boost_test.dir/main.cpp.o -o boost_test /path/to/Boost/lib/libboost_program_options.so
CMakeFiles/boost_test.dir/main.cpp.o: In function `main':
main.cpp:(.text+0x6d): undefined reference to `boost::program_options::options_description::options_description(std::string const&, unsigned int, unsigned int)'
CMakeFiles/boost_test.dir/main.cpp.o: In function `boost::program_options::variables_map::operator[](std::string const&) const':
main.cpp:(.text._ZNK5boost15program_options13variables_mapixERKSs[_ZNK5boost15program_options13variables_mapixERKSs]+0x1f): undefined reference to `boost::program_options::abstract_variables_map::operator[](std::string const&) const'
CMakeFiles/boost_test.dir/main.cpp.o: In function `boost::program_options::basic_command_line_parser<char>::basic_command_line_parser(int, char const* const*)':
main.cpp:(.text._ZN5boost15program_options25basic_command_line_parserIcEC2EiPKPKc[_ZN5boost15program_options25basic_command_line_parserIcEC5EiPKPKc]+0x76): undefined reference to `boost::program_options::detail::cmdline::cmdline(std::vector<std::string, std::allocator<std::string> > const&)'
CMakeFiles/boost_test.dir/main.cpp.o: In function `boost::program_options::basic_command_line_parser<char>::extra_parser(boost::function1<std::pair<std::string, std::string>, std::string const&>)':
main.cpp:(.text._ZN5boost15program_options25basic_command_line_parserIcE12extra_parserENS_9function1ISt4pairISsSsERKSsEE[_ZN5boost15program_options25basic_command_line_parserIcE12extra_parserENS_9function1ISt4pairISsSsERKSsEE]+0x33): undefined reference to `boost::program_options::detail::cmdline::set_additional_parser(boost::function1<std::pair<std::string, std::string>, std::string const&>)'
CMakeFiles/boost_test.dir/main.cpp.o: In function `std::vector<std::string, std::allocator<std::string> > boost::program_options::to_internal<std::string>(std::vector<std::string, std::allocator<std::string> > const&)':
main.cpp:(.text._ZN5boost15program_options11to_internalISsEESt6vectorISsSaISsEERKS2_IT_SaIS5_EE[_ZN5boost15program_options11to_internalISsEESt6vectorISsSaISsEERKS2_IT_SaIS5_EE]+0x46): undefined reference to `boost::program_options::to_internal(std::string const&)'
CMakeFiles/boost_test.dir/main.cpp.o:(.rodata._ZTVN5boost15program_options11typed_valueISscEE[_ZTVN5boost15program_options11typed_valueISscEE]+0x40): undefined reference to `boost::program_options::value_semantic_codecvt_helper<char>::parse(boost::any&, std::vector<std::string, std::allocator<std::string> > const&, bool) const'
CMakeFiles/boost_test.dir/main.cpp.o: In function `boost::program_options::typed_value<std::string, char>::name() const':
main.cpp:(.text._ZNK5boost15program_options11typed_valueISscE4nameEv[_ZNK5boost15program_options11typed_valueISscE4nameEv]+0x32): undefined reference to `boost::program_options::arg'
CMakeFiles/boost_test.dir/main.cpp.o: In function `boost::program_options::typed_value<std::string, char>::xparse(boost::any&, std::vector<std::string, std::allocator<std::string> > const&) const':
main.cpp:(.text._ZNK5boost15program_options11typed_valueISscE6xparseERNS_3anyERKSt6vectorISsSaISsEE[_ZNK5boost15program_options11typed_valueISscE6xparseERNS_3anyERKSt6vectorISsSaISsEE]+0x7a): undefined reference to `boost::program_options::validate(boost::any&, std::vector<std::string, std::allocator<std::string> > const&, std::string*, int)'
collect2: error: ld returned 1 exit status
make[2]: *** [boost_test] Error 1
make[2]: Leaving directory `/home/test/build'
make[1]: *** [CMakeFiles/boost_test.dir/all] Error 2
make[1]: Leaving directory `/home/test/build'
make: *** [all] Error 2
Thank you Amadeus, Someprogrammerdude and Tsyvarev for leading me in the right direction here :)
In fact, it was a compiler issue: The default compiler on the cluster was gcc-4.8.5, which cannot handle the latest Boost versions. Therefore, we loaded a module for gcc-5.4 but /usr/bin/gcc was still gcc-4.8.5. When we set the cxx compiler for cmake to gcc-5.4 the build process still failed, since we also use CUDA and per default the host code is not forwarded to the cxx compiler but the c compiler, which was still gcc-4.8.5. So after setting CUDA_HOST_COMPILER to CMAKE_CXX_COMPILER it works fine now.

Symbol(s) not found for architecture x86_64 - Cmake - Mac sierra

Recently I have started a new project in C++. The problem is, when I try to compile it I get a linking error. I spent the whole day today trying to debug it, but I did not really find a good solution anywhere. If someone could help with it it would be amazing. I am using a Mac Sierra.
parsing/methylation.h
#ifndef EPIRL_METHYLATION_H
#define EPIRL_METHYLATION_H
#include <stdio.h>
#include <iostream>
#include <fstream>
#include <vector>
#include <sstream>
using namespace std;
namespace methylation {
struct MethLine {
string chr;
int coord;
char strand;
int methylated;
int un_methylated;
string context;
string tag;
};
string calculateMethylationByContext(
MethLine m_input[], int length,
int window_start, int window_end, int threshold);
void calculateMethylation(
const istream &methylation_stream,
const istream &coordinate_stream,
const ostream &output_stream
);
}
#endif //EPIRL_METHYLATION_H
parsing/methylation.cpp
#include "methylation.h"
namespace methylation {
string calculateMethylationByContext(
MethLine m_input[], int length,
int window_start, int window_end, int threshold) {
// rest of the code ...
}
}
main.cpp
#include <iostream>
#include <fstream>
#include "parsing/methylation.h"
using namespace std;
int main(int argc, char **argv) {
if (argc != 4) {
cout << "Invalid number of arguments..." << endl;
return 1;
}
char *methylation_file = argv[1];
char *coordinate_file = argv[2];
char *output_file = argv[3];
ifstream methylation_file_stream(methylation_file, ios::binary);
ifstream coordinate_file_stream(coordinate_file, ios::binary);
ofstream output_file_stream(output_file, ios::binary);
methylation::calculateMethylation(methylation_file_stream,
coordinate_file_stream, output_file_stream);
methylation_file_stream.close();
coordinate_file_stream.close();
output_file_stream.close();
return 0;
}
I use CLion for my coding. When I try to build it, my cmake command works fine, but when I then click 'make' I get the following error:
Undefined symbols for architecture x86_64:
"methylation::calculateMethylation(std::__1::basic_istream<char, std::__1::char_traits<char> > const&, std::__1::basic_istream<char, std::__1::char_traits<char> > const&, std::__1::basic_ostream<char, std::__1::char_traits<char> > const&)", 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[3]: *** [src] Error 1
make[2]: *** [CMakeFiles/src.dir/all] Error 2
make[1]: *** [CMakeFiles/src.dir/rule] Error 2
make: *** [src] Error 2
my CMakeLists.txt file looks like the following:
cmake_minimum_required(VERSION 3.6)
project(src)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
set(SOURCE_FILES
parsing/methylation.cpp
parsing/methylation.h
main.cpp)
add_executable(src ${SOURCE_FILES})
When I run the cmake command, my output is this:
-- The C compiler identification is AppleClang 8.0.0.8000042
-- The CXX compiler identification is AppleClang 8.0.0.8000042
-- 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
-- Configuring done
-- Generating done
-- Build files have been written to: /Users/sztankatt/Documents/University/PartIII/Project/epiRL/src
Your CMakeLists.txt is fine.
As #thomas-matthews #tsyvarev #nos pointed out in their comments, your example code is missing the definition/implementation of methylation::calculateMethylation(). What you're seeing is the expected failure with Apple/clang in this situation.
❯ make
[ 33%] Building CXX object CMakeFiles/src.dir/parsing/methylation.cpp.o
/Users/nega/foo/parsing/methylation.cpp:8:5: warning: control reaches end of non-void function [-Wreturn-type]
}
^
1 warning generated.
[ 66%] Building CXX object CMakeFiles/src.dir/main.cpp.o
[100%] Linking CXX executable src
Undefined symbols for architecture x86_64:
"methylation::calculateMethylation(std::__1::basic_istream<char, std::__1::char_traits<char> > const&, std::__1::basic_istream<char, std::__1::char_traits<char> > const&, std::__1::basic_ostream<char, std::__1::char_traits<char> > const&)", 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]: *** [src] Error 1
make[1]: *** [CMakeFiles/src.dir/all] Error 2
make: *** [all] Error 2
Adding a dummy implementation, or commenting out the call in your main.cpp, will allow make to complete successfully.
Assuming you have a correct implementation
Let's assume you do have an implementation of methylation::calculateMethylation() in your code (maybe in another file). The fist step in debugging build errors in CMake generated Makefiles is to run with the make variable VERBOSE set to a true value, ie: make VERBOSE=1.
❯ make VERBOSE=1
/usr/local/Cellar/cmake/3.7.0/bin/cmake -H/Users/nega/foo -B/Users/nega/foo/build --check-build-system CMakeFiles/Makefile.cmake 0
/usr/local/Cellar/cmake/3.7.0/bin/cmake -E cmake_progress_start /Users/nega/foo/build/CMakeFiles /Users/nega/foo/build/CMakeFiles/progress.marks
/Applications/Xcode.app/Contents/Developer/usr/bin/make -f CMakeFiles/Makefile2 all
/Applications/Xcode.app/Contents/Developer/usr/bin/make -f CMakeFiles/src.dir/build.make CMakeFiles/src.dir/depend
cd /Users/nega/foo/build && /usr/local/Cellar/cmake/3.7.0/bin/cmake -E cmake_depends "Unix Makefiles" /Users/nega/foo /Users/nega/foo /Users/nega/foo/build /Users/nega/foo/build /Users/nega/foo/build/CMakeFiles/src.dir/DependInfo.cmake --color=
/Applications/Xcode.app/Contents/Developer/usr/bin/make -f CMakeFiles/src.dir/build.make CMakeFiles/src.dir/build
[ 33%] Building CXX object CMakeFiles/src.dir/parsing/methylation.cpp.o
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/c++ -std=gnu++11 -o CMakeFiles/src.dir/parsing/methylation.cpp.o -c /Users/nega/foo/parsing/methylation.cpp
/Users/nega/foo/parsing/methylation.cpp:8:5: warning: control reaches end of non-void function [-Wreturn-type]
}
^
1 warning generated.
[ 66%] Building CXX object CMakeFiles/src.dir/main.cpp.o
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/c++ -std=gnu++11 -o CMakeFiles/src.dir/main.cpp.o -c /Users/nega/foo/main.cpp
[100%] Linking CXX executable src
/usr/local/Cellar/cmake/3.7.0/bin/cmake -E cmake_link_script CMakeFiles/src.dir/link.txt --verbose=1
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/c++ -Wl,-search_paths_first -Wl,-headerpad_max_install_names CMakeFiles/src.dir/parsing/methylation.cpp.o CMakeFiles/src.dir/main.cpp.o -o src
Undefined symbols for architecture x86_64:
"methylation::calculateMethylation(std::__1::basic_istream<char, std::__1::char_traits<char> > const&, std::__1::basic_istream<char, std::__1::char_traits<char> > const&, std::__1::basic_ostream<char, std::__1::char_traits<char> > const&)", 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]: *** [src] Error 1
make[1]: *** [CMakeFiles/src.dir/all] Error 2
make: *** [all] Error 2
Now you can look at the link step and see if you're missing items; maybe a library, or an object file. If so, now you know to go back and add it to your CMakeLists.txt
Summary
The first step in debugging unexpected build failures with CMake generated Makefiles is to run:
❯ make VERBOSE=1
This will give you insight into what CMake is doing behind the scenes.
Double check which compiler is automatically detected by CMAKE.
Can you post, what CMAKE tells you when you initially run CMAKE?
This may be a hint for your problem, too