Undefined symbol error occurred while linking LLVM to my own project - c++

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!

Related

building for macOS-x86_64 but attempting to link with file built for macOS-arm64

I wrote a code using c++ and OpenCV:
#include <iostream>
#include <time.h>
#include <opencv2/opencv.hpp>
#include <opencv2/highgui.hpp>
using namespace std;
int main()
{
...
return 0;
}
then I trying to Running my code on the terminal and build using g++:
g++ $(pkg-config --cflags --libs opencv) -std=c++11 yourFile.cpp -o yourFileProgram
but I get this error:
...
ld: warning: ignoring file /opt/homebrew/Cellar/opencv/4.5.1_2/lib/libopencv_core.dylib, building for macOS-x86_64 but attempting to link with file built for macOS-arm64
ld: warning: ignoring file /opt/homebrew/Cellar/opencv/4.5.1_2/lib/libopencv_photo.dylib, building for macOS-x86_64 but attempting to link with file built for macOS-arm64
Undefined symbols for architecture x86_64:
"cv::Mat::Mat()", referenced from:
_main in cv_test-ff1014.o
"cv::Mat::~Mat()", referenced from:
_main in cv_test-ff1014.o
"cv::Mat::operator=(cv::Mat&&)", referenced from:
_main in cv_test-ff1014.o
"cv::imread(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, int)", referenced from:
_main in cv_test-ff1014.o
"cv::imwrite(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, cv::_InputArray const&, std::__1::vector<int, std::__1::allocator<int> > const&)", referenced from:
_main in cv_test-ff1014.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
I had the same problem, and the short answer is that Apple switched to using libc++ instead of libstdc++. If you want to compile it as you mention (via console g++) then the answer is here.
Also, you have mention g++ in tag, but be sure that you understand that gcc is alias of clang, and for using g++ compiler you have to print g++-10.
Way to compile OpenCV whith g++ via terminal as you provide in the example is described here
The best way to include OpenCV lib on macOS is to install via homebrew opencv lib, and just generate CMakeLists.txt. Possible example for it:
cmake_minimum_required(VERSION 3.17)
project(PROJECT_NAME)
find_package(OpenCV REQUIRED )
include_directories( ${OpenCV_INCLUDE_DIRS} )
set(CMAKE_CXX_STANDARD 17)
#set(CMAKE_CXX_COMPILER "/usr/local/bin/g++-10" CACHE STRING "C++ compiler" FORCE)
#set(CMAKE_C_COMPILER "/usr/local/bin/gcc-10" CACHE STRING "C compiler" FORCE)
add_executable(PROJECT_NAME main.cpp)
target_link_libraries(PROJECT_NAME ${OpenCV_LIBS} )
Note, that if you will force to use g++-10 (uncomment set structure) then there will be problem like described here.
I think there is no OpenCV update for arm, then just ignore the warning and try something like that.

Firebase : Undefined symbols for architecture x86_64 (Configure Multiple project)

I am trying to compile main.ccp to configure my c++ project manually and have it connect to my own firebase.
#include "main.hpp"
#include <iostream>
#include "firebase/app.h"
#include "firebase/database.h"
int main()
{
firebase::AppOptions secondary_app_options;
secondary_app_options.set_api_key("API_KEY");
secondary_app_options.set_app_id("APP_ID");
secondary_app_options.set_project_id("PROJ_ID");
firebase::App* secondary_app = firebase::App::Create(secondary_app_options, "Secondary");
firebase::database::Database* secondary_database = firebase::database::Database::GetInstance(secondary_app);
return 0;
}
I use the command line to compile : g++ -std=c++11 main.cpp -o main.o
And it output:
Undefined symbols for architecture x86_64:
"firebase::App::Create(firebase::AppOptions const&, char const*)", referenced from:
_main in main-9988c0.o
"firebase::database::Database::GetInstance(firebase::App*, firebase::InitResult*)", referenced from:
_main in main-9988c0.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
I did correctly download and locate all of the framework and library for firebase already. Can anybody help me with this ?

c++: Undefined symbols for architecture x86_64 in Xcode

At first I am trying to use glad.h in my project. Xcode 7.2.1. gives me the error :
glad.h:26:2: OpenGL header already included, remove this include, glad already provides it.
These are the headers in my main.cpp :
#include "glad/glad.h"
#include <iostream>
#include "GLFW/glfw3.h"
I add GLFW_INCLUDE_NONE to packaging - preprocessor definitions and LLVM - preprocessor macros(similar to preprocessor definitions in Visual Studio I guess?). Then the errors turn to:
Undefined symbols for architecture x86_64:
"_glfwCreateWindow", referenced from:
_main in main.o
"_glfwGetPrimaryMonitor", referenced from:
_main in main.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
If I remove the code #include "glad/glad.h”, the errors remain the same.
Any help is appreciated.

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

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.

How to include C static libraries in CMAKE project on MAC OS X

I am trying to learn Core Foundation with C/C++.
I use JetBrains CLion that uses CMAKE.
The problem is - I dunno how to include proper static libraries in C/C++ Makefile project on Mac.
I need to link static library CFNetwork to my project in order to fix linking problems.
Could you give me a quick hint?
My case:
#include <CFNetwork/CFNetwork.h>
#include <iostream>
int main() {
CFStringRef bodyString = CFSTR(""); // Usually used for POST data
CFDataRef bodyData = CFStringCreateExternalRepresentation(kCFAllocatorDefault,
bodyString, kCFStringEncodingUTF8, 0);
CFStringRef headerFieldName = CFSTR("X-My-Favorite-Field");
CFStringRef headerFieldValue = CFSTR("Dreams");
CFStringRef url = CFSTR("http://www.apple.com");
CFURLRef myURL = CFURLCreateWithString(kCFAllocatorDefault, url, NULL);
CFStringRef requestMethod = CFSTR("GET");
CFHTTPMessageRef myRequest =
CFHTTPMessageCreateRequest(kCFAllocatorDefault, requestMethod, myURL,
kCFHTTPVersion1_1);
CFHTTPMessageSetBody(myRequest, bodyData);
CFHTTPMessageSetHeaderFieldValue(myRequest, headerFieldName, headerFieldValue);
CFDataRef mySerializedRequest = CFHTTPMessageCopySerializedMessage(myRequest);
return 0;
}
I can compile this but linker outputs me the following:
"/Applications/CLion EAP.app/Contents/bin/cmake/bin/cmake" --build /Users/nickolay/Library/Caches/clion10/cmake/generated/3546f185/3546f185/Debug --target test001 -- -j 8
Scanning dependencies of target test001
[100%] Building CXX object CMakeFiles/test001.dir/main.cpp.o
Linking CXX executable test001
Undefined symbols for architecture x86_64:
"_CFHTTPMessageCopySerializedMessage", referenced from:
_main in main.cpp.o
"_CFHTTPMessageCreateRequest", referenced from:
_main in main.cpp.o
"_CFHTTPMessageSetBody", referenced from:
_main in main.cpp.o
"_CFHTTPMessageSetHeaderFieldValue", referenced from:
_main in main.cpp.o
"_CFStringCreateExternalRepresentation", referenced from:
_main in main.cpp.o
"_CFURLCreateWithString", referenced from:
_main in main.cpp.o
"___CFConstantStringClassReference", referenced from:
CFString in main.cpp.o
CFString in main.cpp.o
CFString in main.cpp.o
CFString in main.cpp.o
CFString in main.cpp.o
"_kCFAllocatorDefault", referenced from:
_main in main.cpp.o
"_kCFHTTPVersion1_1", 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]: *** [test001] Error 1
make[2]: *** [CMakeFiles/test001.dir/all] Error 2
make[1]: *** [CMakeFiles/test001.dir/rule] Error 2
make: *** [test001] Error 2
Finally resolved.
Thanks to this resource: http://raycast.net/clion-multiple-binaries
This is how my CMAKE file looks like and everything links:
cmake_minimum_required(VERSION 2.8.4)
project(test001)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
set(SOURCE_FILES main.cpp)
find_library(corefoundation_lib CoreFoundation)
find_library(cfnetwork_lib CFNetwork)
set(frameworks
${cfnetwork_lib}
${corefoundation_lib})
add_executable(test001 ${SOURCE_FILES})
target_link_libraries(test001 ${frameworks})
I'm not 100% sure if this is what you ment but you link C static libraries by giving -llibrary flag to your compiler where the name of the library file is liblibrary.a. If the library isn't in some default library location (idk what it is for mac) you can specify the path with -L flag.