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

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

Related

Undefined symbols for architecture x86_64 mac os library Clion error

I am trying to build my library and link it to the project. But I get an error. The compiler does not see the implementation of the F::method(); function in lesson6.cpp. Why? Can anyone explain what the error is?
Error
====================[ Build | lesson6 | Debug ]=================================
/Applications/CLion.app/Contents/bin/cmake/mac/bin/cmake --build /Users/mbp15/CLionProjects/lesson6/cmake-build-debug --target lesson6 -- -j 6
Scanning dependencies of target lesson6
[ 50%] Building CXX object CMakeFiles/lesson6.dir/app/main.cpp.o
[100%] Linking CXX executable lesson6
Undefined symbols for architecture x86_64:
"F::method(int, int)", 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]: *** [lesson6] Error 1
make[2]: *** [CMakeFiles/lesson6.dir/all] Error 2
make[1]: *** [CMakeFiles/lesson6.dir/rule] Error 2
make: *** [lesson6] Error 2
Cmake.h
cmake_minimum_required(VERSION 3.17)
project(lesson6 VERSION 1.0.0 DESCRIPTION "Lesson project" LANGUAGES CXX)
set(CMAKE_CXX_STANDARD 20)
add_executable(lesson6 app/main.cpp)
add_library(MyLibrary include/lesson6.h src/lesson6.cpp)
target_link_libraries(lesson6 PUBLIC ${MyLibrary})
main.cpp
#include "../include/lesson6.h"
int main(){
F object;
object.method(1,2);
return 0;
}
lesson6.h
#ifndef LESSON6_LESSON6_H
#define LESSON6_LESSON6_H
class F {
public:
int method(int one, int two);
};
#endif //LESSON6_LESSON6_H
lesson6.cpp
#include "../include/lesson6.h"
int F::method(int one, int two) {
return one + two;
}
Project tree
You don't need to dereference the library target in your CMakeLists.txt.
Try changing the last line to
target_link_libraries(lesson6 PUBLIC MyLibrary)

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.

How to fix "ld: symbol(s) not found for architecture x86_64" using C++, CMake and Tensorflow?

I created a test project in C ++ using TensorFlow with CMake. But I have an error:
ld: symbol(s) not found for architecture x86_64
I think, I have an error in my CMake files. When I try compile via the terminal with gcc tensortest.cpp -ltensorflow -o tf, everything works fine.
I have two CMake files.
FindTensorFlow.cmake:
# Locates the tensorFlow library and include directories.
include(FindPackageHandleStandardArgs)
unset(TENSORFLOW_FOUND)
find_path(TensorFlow_INCLUDE_DIR
NAMES
tensorflow
HINTS
/usr/local/include/tensorflow)
find_library(TensorFlow_LIBRARY
NAMES
libtensorflow_framework.1.14.0.dylib
libtensorflow_framework.1.dylib
libtensorflow_framework.dylib
libtensorflow.1.14.0.dylib
libtensorflow.1.dylib
libtensorflow.dylib
HINTS
/usr/local/lib)
# set TensorFlow_FOUND
find_package_handle_standard_args(TensorFlow DEFAULT_MSG TensorFlow_INCLUDE_DIR TensorFlow_LIBRARY)
# set external variables for usage in CMakeLists.txt
if(TENSORFLOW_FOUND)
set(TensorFlow_LIBRARIES ${TensorFlow_LIBRARY})
set(TensorFlow_INCLUDE_DIRS ${TensorFlow_INCLUDE_DIR})
endif()
# hide locals from GUI
mark_as_advanced(TensorFlow_INCLUDE_DIR TensorFlow_LIBRARY)
CMakeLists.txt
cmake_minimum_required(VERSION 3.15)
project(TensorFlowTest)
set(CMAKE_CXX_STANDARD 14)
list(APPEND CMAKE_MODULE_PATH "${PROJECT_SOURCE_DIR}/cmake/")
find_package(TensorFlow REQUIRED)
include_directories(${TensorFlow_INCLUDE_DIRS} ${TensorFlow_LIBRARIES})
add_executable(TensorFlowTest main.cpp)
target_link_libraries(TensorFlowTest ${TensorFlow_INCLUDE_DIRS} ${TensorFlow_LIBRARIES})
And one main.cpp
#include <stdio.h>
#include <tensorflow/c/c_api.h>
int main() {
printf("Hello from TensorFlow C library version %s\n", TF_Version());
return 0;
}
When reload project, I have a CMake message:
-- Found TensorFlow: /usr/local/include
-- Configuring done
WARNING: Target "TensorFlowTest" requests linking to directory "/usr/local/include". Targets may link only to libraries. CMake is dropping the item.
-- Generating done
-- Build files have been written to: /Users/neikr/CLionProjects/TensorFlowTest/cmake-build-debug
And when compiling:
Scanning dependencies of target TensorFlowTest
[ 50%] Building CXX object CMakeFiles/TensorFlowTest.dir/main.cpp.o
[100%] Linking CXX executable TensorFlowTest
Undefined symbols for architecture x86_64:
"_TF_Version", 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]: *** [TensorFlowTest] Error 1
make[2]: *** [CMakeFiles/TensorFlowTest.dir/all] Error 2
make[1]: *** [CMakeFiles/TensorFlowTest.dir/rule] Error 2
make: *** [TensorFlowTest] Error 2
Update
I tried make with VERBOSE=1 and I get this output:
/Applications/CLion.app/Contents/bin/cmake/mac/bin/cmake -S/Users/neikr/CLionProjects/TensorFlowTest -B/Users/neikr/CLionProjects/TensorFlowTest --check-build-system CMakeFiles/Makefile.cmake 0
/Applications/CLion.app/Contents/bin/cmake/mac/bin/cmake -E cmake_progress_start /Users/neikr/CLionProjects/TensorFlowTest/CMakeFiles /Users/neikr/CLionProjects/TensorFlowTest/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/TensorFlowTest.dir/build.make CMakeFiles/TensorFlowTest.dir/depend
cd /Users/neikr/CLionProjects/TensorFlowTest && /Applications/CLion.app/Contents/bin/cmake/mac/bin/cmake -E cmake_depends "Unix Makefiles" /Users/neikr/CLionProjects/TensorFlowTest /Users/neikr/CLionProjects/TensorFlowTest /Users/neikr/CLionProjects/TensorFlowTest /Users/neikr/CLionProjects/TensorFlowTest /Users/neikr/CLionProjects/TensorFlowTest/CMakeFiles/TensorFlowTest.dir/DependInfo.cmake --color=
/Applications/Xcode.app/Contents/Developer/usr/bin/make -f CMakeFiles/TensorFlowTest.dir/build.make CMakeFiles/TensorFlowTest.dir/build
[ 50%] Linking CXX executable TensorFlowTest
/Applications/CLion.app/Contents/bin/cmake/mac/bin/cmake -E cmake_link_script CMakeFiles/TensorFlowTest.dir/link.txt --verbose=1
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/c++ -g -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.14.sdk -Wl,-search_paths_first -Wl,-headerpad_max_install_names CMakeFiles/TensorFlowTest.dir/main.cpp.o -o TensorFlowTest -Wl,-rpath,/usr/local/lib /usr/local/lib/libtensorflow_framework.1.14.0.dylib
Undefined symbols for architecture x86_64:
"_TF_Version", 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]: *** [TensorFlowTest] Error 1
make[1]: *** [CMakeFiles/TensorFlowTest.dir/all] Error 2
make: *** [all] Error 2
Also, I tried adding message("TensorFlow_LIBRARIES: ${TensorFlow_LIBRARIES}") to the end of my CMake and I get this message:
TensorFlow_LIBRARIES: /usr/local/lib/libtensorflow_framework.1.14.0.dylib
Based on your print-out from the message() command, it is more clear what is going on. In your FindTensorFlow.cmake file, the find_library() call only finds one library (libtensorflow_framework.1.14.0.dylib), which is expected behavior:
Once one of the calls succeeds the result variable will be set and stored in the cache so that no call will search again.
When more than one value is given to the NAMES option this command by default will consider one name at a time and search every directory for it.
Thus, once find_library() finds the libtensorflow_framework library, the search stops. If you want to also find libtensorflow.dylib, which may help with your link error, you have to specify another find_library() call, using a different TensorFlow_ variable. So changing that section of your FindTensorFlow.cmake to something like this should help:
find_library(TensorFlow_LIBRARY
NAMES
libtensorflow.1.14.0.dylib
libtensorflow.1.dylib
libtensorflow.dylib
HINTS
/usr/local/lib)
find_library(TensorFlow_FRAMEWORK_LIBRARY
NAMES
libtensorflow_framework.1.14.0.dylib
libtensorflow_framework.1.dylib
libtensorflow_framework.dylib
HINTS
/usr/local/lib)
# set TensorFlow_FOUND
find_package_handle_standard_args(TensorFlow DEFAULT_MSG
TensorFlow_INCLUDE_DIR
TensorFlow_LIBRARY
TensorFlow_FRAMEWORK_LIBRARY
)

CMake build error - missing vtable for virtual method error

Our Xcode C++ project is being converted to a CMake script. When building the CMake script, a missing vtable error keeps happening, if I use a class with virtual functions in my dummy main. If I only use a class without virtual functions in my main, everything builds fine.
The project builds fine for Xcode. What am I missing with my CMake script?
The CMake file:
cmake_minimum_required(VERSION 3.0)
add_compile_options(-std=c++17)
SET (PROJECT_SOURCE_DIR "${CMAKE_SOURCE_DIR}/source")
include_directories(
"${PROJECT_SOURCE_DIR}/"
"${PROJECT_SOURCE_DIR}/main/"
"${PROJECT_SOURCE_DIR}/lib/hopscotch-map/"
"${PROJECT_SOURCE_DIR}/lib/rapidjson/"
)
add_executable(TestProject "${PROJECT_SOURCE_DIR}/main/dummy-main.cpp")
install(TARGETS TestProject DESTINATION bin)
Main file:
#include <stdio.h>
#include "Player.hpp"
int main(int argc, char **argv) {
auto player = Player{"John"};
return 0;
}
The logs:
jess$ gmake -j 8 VERBOSE=1
/usr/local/Cellar/cmake/3.13.3/bin/cmake -S/Users/jess/TestProject -B/Users/jess/TestProject --check-build-system CMakeFiles/Makefile.cmake 0
/usr/local/Cellar/cmake/3.13.3/bin/cmake -E cmake_progress_start /Users/jess/TestProject/CMakeFiles /Users/jess/TestProject/CMakeFiles/progress.marks
gmake -f CMakeFiles/Makefile2 all
gmake[1]: Entering directory '/Users/jess/TestProject'
gmake -f CMakeFiles/TestProject.dir/build.make CMakeFiles/TestProject.dir/depend
gmake[2]: Entering directory '/Users/jess/TestProject'
cd /Users/jess/TestProject && /usr/local/Cellar/cmake/3.13.3/bin/cmake -E cmake_depends "Unix Makefiles" /Users/jess/TestProject /Users/jess/TestProject /Users/jess/TestProject /Users/jess/TestProject /Users/jess/TestProject/CMakeFiles/TestProject.dir/DependInfo.cmake --color=
Dependee "source/BaseEntity.hpp" is newer than depends file "/Users/jess/TestProject/CMakeFiles/TestProject.dir/depend.internal".
Clearing dependencies in "/Users/jess/TestProject/CMakeFiles/TestProject.dir/depend.make".
Scanning dependencies of target TestProject
gmake[2]: Leaving directory '/Users/jess/TestProject'
gmake -f CMakeFiles/TestProject.dir/build.make CMakeFiles/TestProject.dir/build
gmake[2]: Entering directory '/Users/jess/TestProject'
[ 50%] Building CXX object CMakeFiles/TestProject.dir/source/main/dummy-main.cpp.o
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/c++ -I/Users/jess/TestProject -I/Users/jess/TestProject/source -I/Users/jess/TestProject/source/lib/hopscotch-map -I/Users/jess/TestProject/source/lib/rapidjson -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.14.sdk -std=c++17 -fPIC -fexceptions -g -O3 -o CMakeFiles/TestProject.dir/source/main/dummy-main.cpp.o -c /Users/jess/TestProject/source/main/dummy-main.cpp
[100%] Linking CXX executable TestProject
/usr/local/Cellar/cmake/3.13.3/bin/cmake -E cmake_link_script CMakeFiles/TestProject.dir/link.txt --verbose=1
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/c++ -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.14.sdk -Wl,-search_paths_first -Wl,-headerpad_max_install_names CMakeFiles/TestProject.dir/source/main/dummy-main.cpp.o -o TestProject
Undefined symbols for architecture x86_64:
"Player::Player(std::__1::basic_string_view<char, std::__1::char_traits<char> >)", referenced from:
_main in dummy-main.cpp.o
"vtable for BaseEntity", referenced from:
Player::~Player() in dummy-main.cpp.o
NOTE: a missing vtable usually means the first non-inline virtual member function has no definition.
"vtable for Player", referenced from:
Player::~Player() in dummy-main.cpp.o
NOTE: a missing vtable usually means the first non-inline virtual member function has no definition.
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
gmake[2]: *** [CMakeFiles/TestProject.dir/build.make:84: TestProject] Error 1
gmake[2]: Leaving directory '/Users/jess/TestProject'
gmake[1]: *** [CMakeFiles/Makefile2:73: CMakeFiles/TestProject.dir/all] Error 2
gmake[1]: Leaving directory '/Users/jess/TestProject'
gmake: *** [Makefile:130: all] Error 2
The clue is buried in the linker output:
Undefined symbols for architecture x86_64:
"Player::Player(std::__1::basic_string_view<char, std::__1::char_traits<char> >)", referenced from:
_main in dummy-main.cpp.o
"vtable for BaseEntity", referenced from:
Player::~Player() in dummy-main.cpp.o
NOTE: a missing vtable usually means the first non-inline virtual member function has no definition.
"vtable for Player", referenced from:
Player::~Player() in dummy-main.cpp.o
NOTE: a missing vtable usually means the first non-inline virtual member function has no definition.
Which immediately alerts me that you forgot to link the compilation unit containing the definitions of these functions.
Assuming that Player is defined in player.hpp and player.cpp and that they are in the subdirectory src then my suggestion would be to update the add_executable call to:
add_executable(TestProject "${PROJECT_SOURCE_DIR}/main/dummy-main.cpp" src/player.hpp src/player.cpp)
Note that the "current source or binary directory" is assumed here and there is no need to spell it out.
Also note that I have included the header file as part of the target. There are a number of advantages to this:
when generating project files for visual studio, xcode etc, these file will appear in the project tree.
When using the latest INSTALL commands, these files can be given properties to ensure that they are treated properly (e.g. public or private header files for library targets)

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.