Linking errors when building c++ project using mongo-cxx-driver - c++

I am currently developing a C++ application which requires the use of the mongo-cxx-driver for accessing a MongoDB instance. I attempted a couple of methods of installation, and am met with the same linker issues each time.
Initially, I attempted to install mongo-cxx-drivers and mongod-c-driver as detailed here: https://mongodb.github.io/mongo-cxx-driver/mongocxx-v3/installation/
Using the following portion of my CMake configuration, I was able to get auto-completion working and my IDE to recognize the libraries:
. . .
set(CMAKE_CXX_STANDARD 17)
set(BUILD_DIR "cmake-build-debug")
set(BUILD_PATH "${CMAKE_SOURCE_DIR}/${BUILD_DIR}")
find_package(libmongocxx REQUIRED)
find_package(libbsoncxx REQUIRED)
message("LIBMONGOCXX_INCLUDE_DIRS = ${LIBMONGOCXX_INCLUDE_DIRS}")
message("LIBMONGOCXX_LIBRARIES = ${LIBMONGOCXX_LIBRARIES}")
message("LIBBSONCXX_INCLUDE_DIRS = ${LIBBSONCXX_INCLUDE_DIRS}")
message("LIBBSONCXX_LIBRARIES = ${LIBBSONCXX_LIBRARIES}")
file(GLOB COMMON_LIBRARIES ${LIBMONGOCXX_LIBRARIES} ${LIBBSONCXX_LIBRARIES})
SET(APP_SOURCE source/App/main.cpp)
SET(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${BUILD_PATH}/App)
add_executable(App ${APP_SOURCE})
target_include_directories(App PUBLIC ${LIBMONGOCXX_INCLUDE_DIRS})
target_include_directories(App PUBLIC ${LIBBSONCXX_INCLUDE_DIRS})
target_link_libraries(App ${COMMON_LIBRARIES})
. . .
Unfortunately during the linking stage, I get these errors:
[100%] Linking CXX executable App/App
Undefined symbols for architecture x86_64:
"mongocxx::v_noabi::uri::uri(bsoncxx::v_noabi::string::view_or_value)", referenced from:
App::Initialize(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&) in main.cpp.o
"mongocxx::v_noabi::uri::~uri()", referenced from:
App::Initialize(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&) in main.cpp.o
"mongocxx::v_noabi::client::client(mongocxx::v_noabi::uri const&, mongocxx::v_noabi::options::client const&)", referenced from:
App::Initialize(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&) in main.cpp.o
"mongocxx::v_noabi::client::~client()", referenced from:
App::Initialize(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&) in .cpp.o
"mongocxx::v_noabi::instance::instance()", referenced from:
App::Initialize(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&) in main.cpp.o
"mongocxx::v_noabi::instance::~instance()", referenced from:
App::Initialize(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&) 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]: *** [App/App] Error 1
make[2]: *** [CMakeFiles/App.dir/all] Error 2
make[1]: *** [CMakeFiles/App.dir/rule] Error 2
make: *** [App] Error 2
I tried building using different c++17 polyfills just in case, no go. I also tried manually uninstalling mongo-cxx-driver and mongo-c-driver this time installing thru homebrew, but was met with the same errors.
In my research, the most relatable StackOverflow post is Using the mongodb cxx driver in a cmake c++ project, yet none of the solutions there work for me.
Operating System: macOS Sierra 10.12.6
IDE: CLion 2017.2.2 Build #CL-172.3968.17, built on August 22, 2017
CMake: 3.8.2
mongo-cxx-driver: 3.1.3
mongo-c-driver: 1.8.0
Any help or insight would be much appreciated, please feel free to ask me to clarify or add information where it may be unclear or missing.
EDIT: Here is the code which is causing the error:
#include <cstdint>
#include <iostream>
#include <vector>
#include <bsoncxx/json.hpp>
#include <mongocxx/client.hpp>
#include <mongocxx/instance.hpp>
#include <mongocxx/uri.hpp>
#include <mongocxx/stdx.hpp>
using bsoncxx::builder::stream::close_array;
using bsoncxx::builder::stream::close_document;
using bsoncxx::builder::stream::document;
using bsoncxx::builder::stream::finalize;
using bsoncxx::builder::stream::open_array;
using bsoncxx::builder::stream::open_document;
mongocxx::instance instance{}; // This should be done only once.
mongocxx::uri uri("mongodb://localhost:27017");
mongocxx::client client(uri);
EDIT: I went ahead and created a simple project from scratch and went through all the steps again just to be sure I didn't flub anything. Still in the same boat at the end though.
/Applications/CLion.app/Contents/bin/cmake/bin/cmake -DCMAKE_BUILD_TYPE=Debug -G "CodeBlocks - Unix Makefiles" /Users/user000/Projects/mongo-cxx-driver-test
-- CMAKE_SOURCE_DIR: /Users/user000/Projects/mongo-cxx-driver-test
-- BUILD_PATH: /Users/user000/Projects/mongo-cxx-driver-test/cmake-build-debug
LIBMONGOCXX_INCLUDE_DIRS = /usr/local/include/mongocxx/v_noabi
LIBMONGOCXX_LIBRARIES = mongocxx
LIBBSONCXX_INCLUDE_DIRS = /usr/local/include/bsoncxx/v_noabi
LIBBSONCXX_LIBRARIES = bsoncxx
-- Configuring done
-- Generating done
-- Build files have been written to: /Users/user000/Projects/mongo-cxx-driver-test/cmake-build-debug
[Finished]
Here is the full output when building with verbose mode enabled for CMake:
/Applications/CLion.app/Contents/bin/cmake/bin/cmake --build /Users/user000/Projects/mongo-cxx-driver-test/cmake-build-debug --target App -- -j 2
/Applications/CLion.app/Contents/bin/cmake/bin/cmake -H/Users/user000/Projects/mongo-cxx-driver-test -B/Users/user000/Projects/mongo-cxx-driver-test/cmake-build-debug --check-build-system CMakeFiles/Makefile.cmake 0
/Applications/Xcode.app/Contents/Developer/usr/bin/make -f CMakeFiles/Makefile2 App
/Applications/CLion.app/Contents/bin/cmake/bin/cmake -H/Users/user000/Projects/mongo-cxx-driver-test -B/Users/user000/Projects/mongo-cxx-driver-test/cmake-build-debug --check-build-system CMakeFiles/Makefile.cmake 0
/Applications/CLion.app/Contents/bin/cmake/bin/cmake -E cmake_progress_start /Users/user000/Projects/mongo-cxx-driver-test/cmake-build-debug/CMakeFiles 2
/Applications/Xcode.app/Contents/Developer/usr/bin/make -f CMakeFiles/Makefile2 CMakeFiles/App.dir/all
/Applications/Xcode.app/Contents/Developer/usr/bin/make -f CMakeFiles/App.dir/build.make CMakeFiles/App.dir/depend
cd /Users/user000/Projects/mongo-cxx-driver-test/cmake-build-debug && /Applications/CLion.app/Contents/bin/cmake/bin/cmake -E cmake_depends "Unix Makefiles" /Users/user000/Projects/mongo-cxx-driver-test /Users/user000/Projects/mongo-cxx-driver-test /Users/user000/Projects/mongo-cxx-driver-test/cmake-build-debug /Users/user000/Projects/mongo-cxx-driver-test/cmake-build-debug /Users/user000/Projects/mongo-cxx-driver-test/cmake-build-debug/CMakeFiles/App.dir/DependInfo.cmake --color=
Scanning dependencies of target App
/Applications/Xcode.app/Contents/Developer/usr/bin/make -f CMakeFiles/App.dir/build.make CMakeFiles/App.dir/build
[ 50%] Building CXX object CMakeFiles/App.dir/source/App/main.cpp.o
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/c++ -I/usr/local/include/mongocxx/v_noabi -I/usr/local/include/bsoncxx/v_noabi -g -std=gnu++1z -o CMakeFiles/App.dir/source/App/main.cpp.o -c /Users/user000/Projects/mongo-cxx-driver-test/cmake-build-debug/source/App/main.cpp
[100%] Linking CXX executable App
/Applications/CLion.app/Contents/bin/cmake/bin/cmake -E cmake_link_script CMakeFiles/App.dir/link.txt --verbose=1
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/c++ -g -Wl,-search_paths_first -Wl,-headerpad_max_install_names CMakeFiles/App.dir/source/App/main.cpp.o -o App
Undefined symbols for architecture x86_64:
"mongocxx::v_noabi::uri::uri(bsoncxx::v_noabi::string::view_or_value)", referenced from:
_main in main.cpp.o
"mongocxx::v_noabi::uri::~uri()", referenced from:
_main in main.cpp.o
"mongocxx::v_noabi::client::client(mongocxx::v_noabi::uri const&, mongocxx::v_noabi::options::client const&)", referenced from:
_main in main.cpp.o
"mongocxx::v_noabi::client::~client()", referenced from:
_main in main.cpp.o
"mongocxx::v_noabi::instance::instance()", referenced from:
_main in main.cpp.o
"mongocxx::v_noabi::instance::~instance()", 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]: *** [App] Error 1
make[2]: *** [CMakeFiles/App.dir/all] Error 2
make[1]: *** [CMakeFiles/App.dir/rule] Error 2
make: *** [App] Error 2
The full CMakeLists.txt:
cmake_minimum_required(VERSION 3.8)
project(App)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_VERBOSE_MAKEFILE on)
set(BUILD_DIR "cmake-build-debug")
set(BUILD_PATH "${CMAKE_SOURCE_DIR}/${BUILD_DIR}")
set(BUILD_DIR "cmake-build-debug")
set(BUILD_PATH "${CMAKE_SOURCE_DIR}/${BUILD_DIR}")
message(STATUS "CMAKE_SOURCE_DIR: ${CMAKE_SOURCE_DIR}")
message(STATUS "BUILD_PATH: ${BUILD_PATH}")
find_package(libmongocxx REQUIRED)
find_package(libbsoncxx REQUIRED)
message("LIBMONGOCXX_INCLUDE_DIRS = ${LIBMONGOCXX_INCLUDE_DIRS}")
message("LIBMONGOCXX_LIBRARIES = ${LIBMONGOCXX_LIBRARIES}")
message("LIBBSONCXX_INCLUDE_DIRS = ${LIBBSONCXX_INCLUDE_DIRS}")
message("LIBBSONCXX_LIBRARIES = ${LIBBSONCXX_LIBRARIES}")
file(GLOB COMMON_LIBRARIES ${LIBMONGOCXX_LIBRARIES} ${LIBBSONCXX_LIBRARIES})
set(SOURCE_FILES cmake-build-debug/source/App/main.cpp)
add_executable(App ${SOURCE_FILES})
target_include_directories(App PUBLIC ${LIBMONGOCXX_INCLUDE_DIRS})
target_include_directories(App PUBLIC ${LIBBSONCXX_INCLUDE_DIRS})
target_link_libraries(App ${COMMON_LIBRARIES})
The code producing the errors:
#include <cstdint>
#include <iostream>
#include <vector>
#include <bsoncxx/json.hpp>
#include <mongocxx/client.hpp>
#include <mongocxx/instance.hpp>
#include <mongocxx/uri.hpp>
#include <mongocxx/stdx.hpp>
using bsoncxx::builder::stream::close_array;
using bsoncxx::builder::stream::close_document;
using bsoncxx::builder::stream::document;
using bsoncxx::builder::stream::finalize;
using bsoncxx::builder::stream::open_array;
using bsoncxx::builder::stream::open_document;
int main() {
std::cout << "Hello, World!" << std::endl;
mongocxx::instance instance{}; // This should be done only once.
mongocxx::uri uri("mongodb://localhost:27017");
mongocxx::client client(uri);
return 0;
}

Turns out I was using CMake's GLOB wrong.
Changing
target_link_libraries(App ${COMMON_LIBRARIES})
to
target_link_libraries(App ${LIBMONGOCXX_LIBRARIES} ${LIBBSONCXX_LIBRARIES})
fixed the issue.

Related

Undefined symbol error occurred while linking LLVM to my own project

I am learning how to write a compiler, and I used llvm for code generating. But it seems that some error occurred while linking.
The C++ compiler is the default one of XCode.
LLVM version: LLVM 16.0.0git
cmake version: 3.24.0
My CMakeList.txt(Cmake configure succeeded):
cmake_minimum_required(VERSION 3.21)
project(Seserot)
set(CMAKE_CXX_STANDARD 23)
find_package(LLVM REQUIRED CONFIG)
message(STATUS "Found LLVM ${LLVM_PACKAGE_VERSION}")
message(STATUS "Using LLVMConfig.cmake in: ${LLVM_DIR}")
# Set your project compile flags.
# E.g. if using the C++ header files
# you will need to enable C++11 support
# for your compiler.
include_directories(${LLVM_INCLUDE_DIRS})
separate_arguments(LLVM_DEFINITIONS_LIST NATIVE_COMMAND ${LLVM_DEFINITIONS})
add_definitions(${LLVM_DEFINITIONS_LIST})
# Find the libraries that correspond to the LLVM components
# that we wish to use
llvm_map_components_to_libnames(llvm_libs support core irreader)
# Link against LLVM libraries
add_executable(Seserot
src/main.cpp
src/Lexer.cpp
src/ErrorTable.cpp
src/CompilerError.cpp
src/Parser.cpp
src/BuildIn.cpp
src/AbstractSyntaxTreeNode.cpp
src/ByteCodeWriter.cpp
src/Symbol.cpp)
Compiler errors:
====================[ Build | Seserot | Debug ]=================================
/Applications/CLion.app/Contents/bin/cmake/mac/bin/cmake --build /Users/zhaoliyan/CLionProjects/Seserot-gen0/cmake-build-debug --target Seserot -j 6
[1/1] Linking CXX executable Seserot
FAILED: Seserot
: && /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/c++ -g -arch arm64 -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX12.3.sdk -Wl,-search_paths_first -Wl,-headerpad_max_install_names CMakeFiles/Seserot.dir/src/main.cpp.o CMakeFiles/Seserot.dir/src/Lexer.cpp.o CMakeFiles/Seserot.dir/src/ErrorTable.cpp.o CMakeFiles/Seserot.dir/src/CompilerError.cpp.o CMakeFiles/Seserot.dir/src/Parser.cpp.o CMakeFiles/Seserot.dir/src/BuildIn.cpp.o CMakeFiles/Seserot.dir/src/AbstractSyntaxTreeNode.cpp.o CMakeFiles/Seserot.dir/src/ByteCodeWriter.cpp.o CMakeFiles/Seserot.dir/src/Symbol.cpp.o -o Seserot && :
Undefined symbols for architecture arm64:
"llvm::LLVMContext::LLVMContext()", referenced from:
_main in main.cpp.o
"llvm::LLVMContext::~LLVMContext()", referenced from:
_main in main.cpp.o
"llvm::IRBuilderFolder::~IRBuilderFolder()", referenced from:
llvm::ConstantFolder::~ConstantFolder() in main.cpp.o
"llvm::IRBuilderDefaultInserter::~IRBuilderDefaultInserter()", referenced from:
llvm::IRBuilder<llvm::ConstantFolder, llvm::IRBuilderDefaultInserter>::~IRBuilder() in main.cpp.o
"llvm::Module::~Module()", referenced from:
std::__1::default_delete<llvm::Module>::operator()(llvm::Module*) const in main.cpp.o
"vtable for llvm::ConstantFolder", referenced from:
llvm::ConstantFolder::ConstantFolder() in main.cpp.o
NOTE: a missing vtable usually means the first non-inline virtual member function has no definition.
"vtable for llvm::IRBuilderFolder", referenced from:
llvm::IRBuilderFolder::IRBuilderFolder() in main.cpp.o
NOTE: a missing vtable usually means the first non-inline virtual member function has no definition.
"vtable for llvm::IRBuilderDefaultInserter", referenced from:
llvm::IRBuilderDefaultInserter::IRBuilderDefaultInserter() in 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 arm64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
ninja: build stopped: subcommand failed.
My code:
#include <iostream>
#include <string>
#include <vector>
#include <fstream>
#include <filesystem>
#include <llvm/IR/LLVMContext.h>
#include "Lexer.h"
#include "Parser.h"
#include "utils/ByteOrder.h"
//...
int main(int _argc, char **_argv) {
//...
static llvm::LLVMContext thisContext;
static llvm::IRBuilder<> Builder(thisContext);
static std::unique_ptr<llvm::Module> thisModule;
static std::map<std::string, llvm::Value *> NamedValues;
//...
}
My Project:
https://github.com/zly2006/Seserot
I want to know where is wrong and how can I fix this error, Thank you!

Curl with Catalina C++ failing and getting "Undefined symbols for architecture x86_64"

I am trying to run an example c++ program with Catalina 10.15:
#include </usr/local/opt/curl/include/curl/curl.h>
int main(void) {
CURL* curl;
CURLcode result;
curl = curl_easy_init();
curl_easy_setopt(curl, CURLOPT_URL, "https://www.wikipedia.org");
result = curl_easy_perform(curl);
curl_easy_cleanup(curl);
return 0;
}
I have installed curl with:
brew install curl
I don't understand why this is not working and I am getting:
Scanning dependencies of target 2700
[ 50%] Building CXX object CMakeFiles/2700.dir/main.cpp.o
[100%] Linking CXX executable 2700
Undefined symbols for architecture x86_64:
"_curl_easy_cleanup", referenced from:
_main in main.cpp.o
"_curl_easy_init", referenced from:
_main in main.cpp.o
"_curl_easy_perform", referenced from:
_main in main.cpp.o
"_curl_easy_setopt", 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]: *** [2700] Error 1
make[2]: *** [CMakeFiles/2700.dir/all] Error 2
make[1]: *** [CMakeFiles/2700.dir/rule] Error 2
make: *** [2700] Error 2
My CMakeLists.txt is as follows:
cmake_minimum_required(VERSION 3.15.3)
project(2700)
include_directories(/usr/local/opt/curl/include/)
set(CMAKE_CXX_STANDARD 17)
add_executable(2700 main.cpp )
I am using CLion and I am fairly new to C++, especially on MacOS.
What can I do do get the curl working and not get the issue?
I have realised that I need to do the linking in CMakeLists.txt:
This works:
cmake_minimum_required(VERSION 3.15.3)
project(2700)
include_directories(/usr/local/opt/curl/include/)
set(CMAKE_CXX_STANDARD 17)
set(CURL_LIBRARY "-lcurl")
find_package(CURL REQUIRED)
add_executable(2700 main.cpp )
include_directories(${CURL_INCLUDE_DIR})
target_link_libraries(2700 ${CURL_LIBRARIES})
I was getting the same error on MacOS Big Sur... exact same. What fixed it for me was this answer: lib curl symbol not found
All you need to do is link CURL: -lcurl
I too am using CLion but didn't want to bother with the CMakeLists.txt file.
Adding the flag via the compile command was much easier for me:
c++ main.cpp -o main -lcurl

Updating CMake (seems to have) broke my program

I updated from CMake 3.6 to whatever the latest version is (3.12.0-rc2) and now one of my programs won't compile.
The weird thing is, the error message shows undefined symbols from in the standard library itself. Here's the error message:
Undefined symbols for architecture x86_64:
"std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >::basic_string(char const*)", referenced from:
GetName() in ncc.cpp.o
Expression() in ncc.cpp.o
Term() in ncc.cpp.o
GetNum() in ncc.cpp.o
"std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >::basic_string()", referenced from:
GetName() in ncc.cpp.o
GetNum() in ncc.cpp.o
"std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >::operator+=(char)", referenced from:
GetName() in ncc.cpp.o
GetNum() in ncc.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]: *** [ncc] Error 1
make[1]: *** [CMakeFiles/ncc.dir/all] Error 2
make: *** [all] Error 2
How do I fix this? Do I need to link to a different version of the standard library? And if so, how I tell CMake that?
Here's my CMakeLists.txt file:
cmake_minimum_required(VERSION 3.0)
project(ncc)
add_definitions(-std=c++14)
add_subdirectory(${CMAKE_SOURCE_DIR}/src/)
add_executable(ncc ${CompSource})
add_executable(ncc-opt ${OptSource})
add_executable(nci ${IntSource})
I tried it with the add_definitions line and without it, which didn't seem to make a difference.
This is the relevant output from running make VERBOSE=1:
Scanning dependencies of target ncc
/Applications/Xcode.app/Contents/Developer/usr/bin/make -f CMakeFiles/ncc.dir/build.make CMakeFiles/ncc.dir/build
[ 83%] Building CXX object CMakeFiles/ncc.dir/src/ncc.cpp.o
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/c++ -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.12.sdk -std=c++14 -o CMakeFiles/ncc.dir/src/ncc.cpp.o -c /Users/nepps/Dropbox/Projects/ncc/src/ncc.cpp
[100%] Linking CXX executable ncc
/usr/local/bin/cmake -E cmake_link_script CMakeFiles/ncc.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.12.sdk -Wl,-search_paths_first -Wl,-headerpad_max_install_names CMakeFiles/ncc.dir/src/ncc.cpp.o -o ncc
Undefined symbols for architecture x86_64:
(same errors as above)
Feel free to comment if you need more information. I'm using a MacBook pro running MacOS 10.13.3.
Well, I'm not sure why it worked, but all of the functions in the program were marked static and removing that made the errors go away. Go figure.

CMAKE, Am I linking the wrong lib?

I made a working C++ project with boost and std on windows with visual studio.
After some hours of coding I realise that was stupide of me to use a VS2017 since I will need to test the software on mac and linux.
So I went on my mac, and now I am trying to just compile everything.
I built boost on mac :
./bootstrap.sh && ./b2 threading=multi link=static
runtime-link=static install
Then I took the lib I need (chrono, thread, regex, system, thread, date_time) and copy the libboost_xxx.a files into a folder of my project.
Now this is my current makefile :
cmake_minimum_required(VERSION 3.8)
project(AuthServer)
include_directories(${CMAKE_SOURCE_DIR}/Include)
set(CMAKE_VERBOSE_MAKEFILE ON)
set(CMAKE_CXX_STANDARD 11)
set(SOURCE_FILES
AuthServer/Client.cpp
AuthServer/Client.h
AuthServer/main.cpp
AuthServer/NetworkManager.cpp
AuthServer/NetworkManager.h)
find_library(BOOST_LIBRARY
NAMES libboost_atomic boost_chrono boost_date_time boost_regex boost_thread boost_system
PATHS ${CMAKE_SOURCE_DIR}/Lib/mac/x64
NO_DEFAULT_PATH)
add_executable(AuthServer ${SOURCE_FILES})
target_link_libraries(AuthServer ${BOOST_LIBRARY})
As you can see I don't have much going on but nonetheless it cannot be built :
/Applications/CLion.app/Contents/bin/cmake/bin/cmake --build "/Users/rxp/Code Project/MagesBattleAuthServerCPP/AuthServer/cmake-build-debug"
--target AuthServer -- -j 4 /Applications/CLion.app/Contents/bin/cmake/bin/cmake
-H"/Users/rxp/Code Project/MagesBattleAuthServerCPP/AuthServer" -B"/Users/rxp/Code Project/MagesBattleAuthServerCPP/AuthServer/cmake-build-debug"
--check-build-system CMakeFiles/Makefile.cmake 0 /Applications/Xcode.app/Contents/Developer/usr/bin/make -f CMakeFiles/Makefile2 AuthServer /Applications/CLion.app/Contents/bin/cmake/bin/cmake
-H"/Users/rxp/Code Project/MagesBattleAuthServerCPP/AuthServer" -B"/Users/rxp/Code Project/MagesBattleAuthServerCPP/AuthServer/cmake-build-debug"
--check-build-system CMakeFiles/Makefile.cmake 0 /Applications/CLion.app/Contents/bin/cmake/bin/cmake -E cmake_progress_start "/Users/rxp/Code Project/MagesBattleAuthServerCPP/AuthServer/cmake-build-debug/CMakeFiles" 4 /Applications/Xcode.app/Contents/Developer/usr/bin/make -f CMakeFiles/Makefile2 CMakeFiles/AuthServer.dir/all /Applications/Xcode.app/Contents/Developer/usr/bin/make -f CMakeFiles/AuthServer.dir/build.make CMakeFiles/AuthServer.dir/depend cd "/Users/rxp/Code Project/MagesBattleAuthServerCPP/AuthServer/cmake-build-debug" && /Applications/CLion.app/Contents/bin/cmake/bin/cmake -E cmake_depends "Unix Makefiles" "/Users/rxp/Code Project/MagesBattleAuthServerCPP/AuthServer" "/Users/rxp/Code Project/MagesBattleAuthServerCPP/AuthServer" "/Users/rxp/Code Project/MagesBattleAuthServerCPP/AuthServer/cmake-build-debug" "/Users/rxp/Code Project/MagesBattleAuthServerCPP/AuthServer/cmake-build-debug" "/Users/rxp/Code Project/MagesBattleAuthServerCPP/AuthServer/cmake-build-debug/CMakeFiles/AuthServer.dir/DependInfo.cmake"
--color= /Applications/Xcode.app/Contents/Developer/usr/bin/make -f CMakeFiles/AuthServer.dir/build.make CMakeFiles/AuthServer.dir/build [ 25%] Building CXX object CMakeFiles/AuthServer.dir/AuthServer/Client.cpp.o [ 50%] Building CXX object CMakeFiles/AuthServer.dir/AuthServer/main.cpp.o /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/c++
-I"/Users/rxp/Code Project/MagesBattleAuthServerCPP/AuthServer/Include" -g -arch x86_64
-mmacosx-version-min=10.12 -std=gnu++11 -o CMakeFiles/AuthServer.dir/AuthServer/Client.cpp.o -c "/Users/rxp/Code Project/MagesBattleAuthServerCPP/AuthServer/AuthServer/Client.cpp" /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/c++
-I"/Users/rxp/Code Project/MagesBattleAuthServerCPP/AuthServer/Include" -g -arch x86_64
-mmacosx-version-min=10.12 -std=gnu++11 -o CMakeFiles/AuthServer.dir/AuthServer/main.cpp.o -c "/Users/rxp/Code Project/MagesBattleAuthServerCPP/AuthServer/AuthServer/main.cpp" [ 75%] Building CXX object CMakeFiles/AuthServer.dir/AuthServer/NetworkManager.cpp.o /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/c++
-I"/Users/rxp/Code Project/MagesBattleAuthServerCPP/AuthServer/Include" -g -arch x86_64
-mmacosx-version-min=10.12 -std=gnu++11 -o CMakeFiles/AuthServer.dir/AuthServer/NetworkManager.cpp.o -c "/Users/rxp/Code Project/MagesBattleAuthServerCPP/AuthServer/AuthServer/NetworkManager.cpp" [100%] Linking CXX executable AuthServer /Applications/CLion.app/Contents/bin/cmake/bin/cmake -E cmake_link_script CMakeFiles/AuthServer.dir/link.txt --verbose=1 /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/c++
-g -arch x86_64 -mmacosx-version-min=10.12 -Wl,-search_paths_first -Wl,-headerpad_max_install_names CMakeFiles/AuthServer.dir/AuthServer/Client.cpp.o CMakeFiles/AuthServer.dir/AuthServer/main.cpp.o CMakeFiles/AuthServer.dir/AuthServer/NetworkManager.cpp.o -o AuthServer ../Lib/mac/x64/libboost_chrono.a Undefined symbols for architecture x86_64: "boost::this_thread::interruption_point()", referenced from:
boost::condition_variable::wait(boost::unique_lock<boost::mutex>&) in NetworkManager.cpp.o "boost::this_thread::disable_interruption::disable_interruption()", referenced from:
boost::shared_mutex::lock_shared() in NetworkManager.cpp.o
boost::shared_mutex::lock() in NetworkManager.cpp.o "boost::this_thread::disable_interruption::~disable_interruption()", referenced from:
boost::shared_mutex::lock_shared() in NetworkManager.cpp.o
boost::shared_mutex::lock() in NetworkManager.cpp.o "boost::detail::thread_data_base::~thread_data_base()", referenced from:
boost::detail::thread_data<boost::_bi::bind_t<void, boost::_mfi::mf1<void, NetworkManager, boost::shared_ptr<boost::asio::io_service> >, boost::_bi::list2<boost::_bi::value<NetworkManager*>, boost::_bi::value<boost::shared_ptr<boost::asio::io_service> > > >
>::~thread_data() in NetworkManager.cpp.o "boost::detail::get_current_thread_data()", referenced from:
boost::detail::interruption_checker::interruption_checker(_opaque_pthread_mutex_t*,
_opaque_pthread_cond_t*) in NetworkManager.cpp.o "boost::system::system_category()", referenced from:
boost::asio::error::get_system_category() in Client.cpp.o
boost::system::error_code::error_code() in Client.cpp.o
___cxx_global_var_init.2 in Client.cpp.o
boost::asio::error::get_system_category() in main.cpp.o
___cxx_global_var_init.2 in main.cpp.o
boost::asio::error::get_system_category() in NetworkManager.cpp.o
boost::system::error_code::error_code() in NetworkManager.cpp.o
... "boost::system::generic_category()", referenced from:
boost::system::error_category::std_category::equivalent(int, std::__1::error_condition const&) const in Client.cpp.o
boost::system::error_category::std_category::equivalent(std::__1::error_code const&, int) const in Client.cpp.o
boost::thread_exception::thread_exception(int, char const*) in Client.cpp.o
___cxx_global_var_init in Client.cpp.o
___cxx_global_var_init.1 in Client.cpp.o
boost::system::error_category::std_category::equivalent(int, std::__1::error_condition const&) const in main.cpp.o
boost::system::error_category::std_category::equivalent(std::__1::error_code const&, int) const in main.cpp.o
... "boost::thread::join_noexcept()", referenced from:
boost::thread::join() in NetworkManager.cpp.o "boost::thread::native_handle()", referenced from:
boost::thread::get_id() const in NetworkManager.cpp.o "boost::thread::hardware_concurrency()", referenced from:
_main in main.cpp.o "boost::thread::start_thread_noexcept()", referenced from:
boost::thread::start_thread() in NetworkManager.cpp.o "boost::thread::detach()", referenced from:
boost::thread::~thread() in NetworkManager.cpp.o "boost::thread::joinable() const", referenced from:
boost::thread_group::join_all() in NetworkManager.cpp.o "typeinfo for boost::detail::thread_data_base", referenced from:
typeinfo for boost::detail::thread_data<boost::_bi::bind_t<void, boost::_mfi::mf1<void, NetworkManager, boost::shared_ptr<boost::asio::io_service> >, boost::_bi::list2<boost::_bi::value<NetworkManager*>, boost::_bi::value<boost::shared_ptr<boost::asio::io_service> > > > > in NetworkManager.cpp.o "vtable for boost::detail::thread_data_base", referenced from:
boost::detail::thread_data_base::thread_data_base() in NetworkManager.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) make[3]: *** [AuthServer] Error 1 make[2]: *** [CMakeFiles/AuthServer.dir/all] Error 2 make[1]:
*** [CMakeFiles/AuthServer.dir/rule] Error 2 make: *** [AuthServer] Error 2
I've been working on windows only with C++ until now and so I might have made so giant mistake with the Cmake file but I don't know what.
The libs I look for are found and everything seems to work. I know the libs are the right ones since on the windows version, they are the one I use.
Down the line I want to make one makefile for windows and unix so I can stop working with the SLN.
I needed to use
find_package
for boost (FindBoost) or
find_library
for EACH file.

CMake finds boost libraries but Make fails to link them

After coming back to a project after a month I am able to run CMake successfully with the following output
-- Boost version: 1.61.0
-- Found the following Boost libraries:
-- system
-- thread
-- filesystem
-- chrono
-- date_time
-- atomic
-- Configuring done
-- Generating done
-- Build files have been written to: /Users/LittleNewt/gitness/MangaMeCLI/build
but for some reason when running Make on the generated MakeFile I get the following output
[ 50%] Building CXX object CMakeFiles/mangaMeCLI.dir/src/mangaMeCLI.cpp.o
[100%] Linking CXX executable mangaMeCLI
Undefined symbols for architecture x86_64:
"boost::filesystem::path::operator/=(char const*)", referenced from:
_main in mangaMeCLI.cpp.o
"boost::filesystem::path::operator/=(boost::filesystem::path const&)", referenced from:
boost::filesystem::path::operator/=(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >&) in mangaMeCLI.cpp.o
boost::filesystem::path::operator/=(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&) in mangaMeCLI.cpp.o
"boost::filesystem::detail::current_path(boost::system::error_code*)", referenced from:
boost::filesystem::current_path() in mangaMeCLI.cpp.o
"boost::filesystem::detail::create_directory(boost::filesystem::path const&, boost::system::error_code*)", referenced from:
boost::filesystem::create_directory(boost::filesystem::path const&) in mangaMeCLI.cpp.o
"boost::filesystem::detail::status(boost::filesystem::path const&, boost::system::error_code*)", referenced from:
boost::filesystem::exists(boost::filesystem::path const&) in mangaMeCLI.cpp.o
boost::filesystem::is_directory(boost::filesystem::path const&) in mangaMeCLI.cpp.o
"boost::system::system_category()", referenced from:
___cxx_global_var_init.75 in mangaMeCLI.cpp.o
"boost::system::generic_category()", referenced from:
___cxx_global_var_init.73 in mangaMeCLI.cpp.o
___cxx_global_var_init.74 in mangaMeCLI.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]: *** [mangaMeCLI] Error 1
make[1]: *** [CMakeFiles/mangaMeCLI.dir/all] Error 2
make: *** [all] Error 2
I did some research and have found others having the same issues due to not linking the correct version of the libraries for there architecture (64bit vs 32bit) but am not sure how to identify if this is the issue for me.
Here is my unchanged CMakeLists.txt file
CMAKE_MINIMUM_REQUIRED(VERSION 3.4.1)
PROJECT(MangaMeCLI)
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
INCLUDE_DIRECTORIES(includes)
ADD_EXECUTABLE(mangaMeCLI src/mangaMeCLI.cpp)
MESSAGE("${CMAKE_CXX_FLAGS}")
SET(Boost_USE_MULTITHREADED ON)
FIND_PACKAGE(Boost REQUIRED COMPONENTS system thread filesystem)
INCLUDE_DIRECTORIES(${BOOST_INCLUDE_DIRS})
TARGET_LINK_LIBRARIES(mangaMeCLI ${BOOST_LIBRARIES})
SET(OPENSSL_ROOT_DIR /usr/local/Cellar/openssl/*)
FIND_PACKAGE(OpenSSL REQUIRED)
INCLUDE_DIRECTORIES(${OPENSSL_INCLUDE_DIR})
TARGET_LINK_LIBRARIES(mangaMeCLI ${OPENSSL_LIBRARIES})
FIND_PACKAGE(cppnetlib REQUIRED)
INCLUDE_DIRECTORIES(${CPPNETLIB_INCLUDE_DIRS})
TARGET_LINK_LIBRARIES(mangaMeCLI ${CPPNETLIB_LIBRARIES})
After reading a useful comment I found that the BOOST_LIBRARIES variable was empty even though cmake prints that it found the boost libraries I was looking for. I am assuming this is the reason for my error.
CMake variables are case-sensitive. As per the find module's documentation, you want Boost_LIBRARIES, not BOOST_LIBRARIES:
INCLUDE_DIRECTORIES(${Boost_INCLUDE_DIRS})
TARGET_LINK_LIBRARIES(mangaMeCLI ${Boost_LIBRARIES})