CMake - undefined symbols encountered when building application depends on PcapPlusPlus - c++

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.

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)

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
)

How to setup a C++ project with MySQL Connector and Make

despite having developed applications in C++ for about two to three years, I never had to setup a project on my own. Most of the project were preconfigured thus I never learned to do it myself. Having lecture-free time at the moment, I thought to myself: "I am going to create my first own CMake C++ project".
Because I knew I wanted to store information in a database,I set off to create a simple CMake project with one executable linked to the MySQL Connector for C++, and instantly failed...
Since I - oddly - could not find helpful information anywhere else, I hope one of you could be my savior.
My project is setup as follows:
Root
- CMakeLists.txt
- build
- src
- tests
-- main.cpp
- include
-- mysql_connection.h
-- cppconn
--- driver.h
--- exception.h
--- resultset.h
--- statement.h
--- ...
-- ...
- libs
-- libmysqlcppconn.dylib -> libmysqlcppconn.7.8.0.12.dylib (symlink)
-- libmysqlcppconn.7.8.0.12.dylib
-- libmysqlcppconn8.1.8.0.12.dylib
-- libmysqlcppconn8.1.dylib -> libmysqlcppconn8.1.8.0.12.dylib (symlink)
-- libmysqlcppconn8.dylib -> libmysqlcppconn8.1.8.0.12.dylib (symlink)
-- libssl.dylib
-- libcrypto.dylib
-- ...
My main.cpp contains:
#include "mysql_connection.h"
#include <cppconn/driver.h>
#include <cppconn/exception.h>
#include <cppconn/resultset.h>
#include <cppconn/statement.h>
int main(int argc, char const *argv[])
{
sql::Driver *driver;
sql::Connection *con;
driver = get_driver_instance();
con = driver->connect("tcp://127.0.0.1:3306","root","securepw");
return 0;
}
And my CMakeLists.txt:
cmake_minimum_required(VERSION 3.0)
project(EconSim)
add_executable(EconSim ${PROJECT_SOURCE_DIR}/tests/main.cpp)
target_include_directories(EconSim PUBLIC ${PROJECT_SOURCE_DIR}/include)
target_link_libraries(EconSim ${PROJECT_SOURCE_DIR}/libs/libmysqlcppconn.dylib)
target_compile_features(EconSim PUBLIC cxx_std_17)
I am able to compile the application but I get the following error when executing it:
dyld: Library not loaded: libmysqlcppconn.7.dylib
Referenced from: /Users/aosterthun/Documents/Programming/EconSim/build/./EconSim
Reason: image not found
Abort trap: 6
When instead using libmysqlcppconn8.dylib in the CMakeLists.txt:
And my CMakeLists.txt:
cmake_minimum_required(VERSION 3.0)
project(EconSim)
add_executable(EconSim ${PROJECT_SOURCE_DIR}/tests/main.cpp)
target_include_directories(EconSim PUBLIC ${PROJECT_SOURCE_DIR}/include)
target_link_libraries(EconSim ${PROJECT_SOURCE_DIR}/libs/libmysqlcppconn8.dylib)
target_compile_features(EconSim PUBLIC cxx_std_17)
I get a compile error:
[build] Starting build
[proc] Executing command: /usr/local/bin/cmake --build /Users/aosterthun/Documents/Programming/EconSim/build --config Debug --target all -- -j 6
[build] [ 50%] Linking CXX executable EconSim
[build] Undefined symbols for architecture x86_64:
[build] "_get_driver_instance", referenced from:
[build] _main in main.cpp.o
[build] ld: symbol(s) not found for architecture x86_64
[build] clang: error: linker command failed with exit code 1 (use -v to see invocation)
[build] make[2]: *** [EconSim] Error 1
[build] make[1]: *** [CMakeFiles/EconSim.dir/all] Error 2
[build] make: *** [all] Error 2
[build] Build finished with exit code 2
I found this: How do I link C++ MySQL Connector Libraries to Cmake? but that sadly did not solve my problem.
I also found CLion: undefined “_get_driver_instance” which resulted in this CMakeLists.txt:
cmake_minimum_required(VERSION 3.0)
project(EconSim)
include_directories(${PROJECT_SOURCE_DIR}/include)
add_library(libmysqlcppconn STATIC IMPORTED)
add_executable(EconSim ${PROJECT_SOURCE_DIR}/tests/main.cpp)
set_property(TARGET libmysqlcppconn PROPERTY IMPORTED_LOCATION ${PROJECT_SOURCE_DIR}/libs/libmysqlcppconn-static.a)
target_compile_features(EconSim PUBLIC cxx_std_17)
target_link_libraries(EconSim libmysqlcppconn)
Which then results in this error:
[build] Starting build
[proc] Executing command: /usr/local/bin/cmake --build /Users/aosterthun/Documents/Programming/EconSim/build --config Debug --target all -- -j 6
[build] [ 50%] Linking CXX executable EconSim
[build] Undefined symbols for architecture x86_64:
[build] "_BIO_free", referenced from:
[build] sha256_password_auth_client(MYSQL_PLUGIN_VIO*, MYSQL*) in libmysqlcppconn-static.a(client_authentication.cc.o)
[build] caching_sha2_password_auth_client(MYSQL_PLUGIN_VIO*, MYSQL*) in libmysqlcppconn-static.a(client_authentication.cc.o)
[build] "_BIO_new_bio_pair", referenced from:
[build] dummy_function_needed_by_xplugin() in libmysqlcppconn-static.a(my_aes_openssl.cc.o)
[build] "_BIO_new_mem_buf", referenced from:
[build] sha256_password_auth_client(MYSQL_PLUGIN_VIO*, MYSQL*) in libmysqlcppconn-static.a(client_authentication.cc.o)
[build] caching_sha2_password_auth_client(MYSQL_PLUGIN_VIO*, MYSQL*) in libmysqlcppconn-static.a(client_authentication.cc.o)
[build] "_BN_bin2bn", refer...
I would appreciate any help. Even a hint to an obvious solution I have not found. I am still a bit confused why I did not find helpful information on this topic. Cause there should be lots of people developing with this technology.
Just ask for further information if I missed any.
You need install openssl and link libraries libcrypto, libssl, libresolv.
I solve this problem as follows:
target_link_libraries(${PROJECT_NAME}
PUBLIC
mysqlclient
mysqlcppconn-static
crypto
ssl
resolv
)

Minimal example with GLFW fails because of undefined symbols for architecture x86_64

I am trying to compile the following minimal example for GLFW:
#include <GLFW/glfw3.h>
#include <thread>
int main() {
glfwInit();
std::this_thread::sleep_for(std::chrono::seconds(1));
glfwTerminate();
return 0;
}
My CMakeLists.txt file is:
cmake_minimum_required(VERSION 3.9)
project(viewer)
set(CMAKE_CXX_STANDARD 11)
find_package(glfw3 3.2 REQUIRED)
include_directories(${GLFW_INCLUDE_DIRS})
add_executable(viewer main.cpp)
target_link_libraries(viewer ${GLFW_LIBRARIES})
If I try to compile the code, it fails with the following error message:
[ 50%] Linking CXX executable visualiser
Undefined symbols for architecture x86_64:
"_glfwInit", referenced from:
_main in main.cpp.o
"_glfwTerminate", 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]: *** [visualiser] Error 1
make[2]: *** [CMakeFiles/visualiser.dir/all] Error 2
make[1]: *** [CMakeFiles/visualiser.dir/rule] Error 2
make: *** [visualiser] Error 2
I see the similar questions, e.g.:
Trouble compiling GLFW, undefined symbols
http://dudandan.com/2017/02/15/Setup-glfw-and-glew/
But their solutions did not really help.
UPD: Following the comment of #thomas_f, I modified my CMakeLists.txt file as follows:
cmake_minimum_required(VERSION 3.9)
project(viewer)
set(CMAKE_CXX_STANDARD 11)
find_package(glfw3 3.2 REQUIRED)
include_directories(${GLFW3_INCLUDE_DIR})
add_executable(viewer main.cpp)
target_link_libraries(viewer ${GLFW3_LIBRARY})
message(GLFW LIB: ${GLFW3_LIBRARY})
I also made sure that there is no CMakeCache.txt in my build directory:
$ ls -a
. .. .idea CMakeLists.txt cmake-build-debug main.cpp
However, I still get the same error message. It seems, that
/Applications/CLion.app/Contents/bin/cmake/bin/cmake -DCMAKE_BUILD_TYPE=Debug -G "CodeBlocks - Unix Makefiles" ........./viewer
GLFWLIB:
-- Configuring done
-- Generating done
-- Build files have been written to: /Users/denis/Documents/projects/theia/code/visualiser/cmake-build-debug
[Finished]
So it seems that ${GLFW3_LIBRARY} is not defined.
From glfw3Config.cmake:
# - Config file for the glfw3 package
# It defines the following variables
# GLFW3_INCLUDE_DIR, the path where GLFW headers are located
# GLFW3_LIBRARY_DIR, folder in which the GLFW library is located
# GLFW3_LIBRARY, library to link against to use GLFW
It seems that you are referencing the wrong variables. Also, there is no need to invoke include_directories() in this case, since GLFW is obviously installed in a standard location, i.e. your compiler already knows where to find it.
Edit: I was able to reproduce the linker error and changing the variable name to GLFW3_LIBRARY fixed it.
Clarification: Change your link command to: target_link_libraries(viewer ${GLFW3_LIBRARY})
Update if you're on Mac OS: If you're experiencing the same issues as OP, it may be because glfw3Config.cmake does not export the variables mentioned in my answer. Instead it creates an imported library, glfw. In this case, the correct way to link to glfw would be to simply do this: target_link_libraries(<target> glfw).
If glfw was installed using brew, you should find the .cmake-files under: /usr/local/Cellar/glfw/<version>/lib/cmake/glfw3.

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