I'm learning about c++ by following a tutorial, I'm using cMake right now and I have my glfw library in my external folder but I can't get it to link correctly. Here is my cMake txt file
cmake_minimum_required(VERSION 2.8.4)
project(dunjun)
set(dunjun_src src/main.cpp)
set(EXECUTABLE_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/bin/)
set(LIBRARY_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/bin/)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
set(SOURCE_FILES src/main.cpp)
include_directories(
${PROJECT_SOURCE_DIR}/include
${PROJECT_SOURCE_DIR}/external/glfw-3.1/include
)
link_directories(
${PROJECT_SOURCE_DIR}/external/glfw-3.1/src
)
add_executable(dunjun ${dunjun_src})
target_link_libraries(dunjun ${PROJECT_SOURCE_DIR}/external/glfw-3.1/src)
The errors it gives me are the following.
[100%] Building CXX object CMakeFiles/dunjun.dir/src/main.cpp.o
Linking CXX executable dunjun
Undefined symbols for architecture x86_64:
"_glfwCreateWindow", referenced from:
_main in main.cpp.o
"_glfwInit", referenced from:
_main in main.cpp.o
"_glfwMakeContextCurrent", referenced from:
_main in main.cpp.o
"_glfwPollEvents", referenced from:
_main in main.cpp.o
"_glfwSwapBuffers", referenced from:
_main in main.cpp.o
"_glfwTerminate", referenced from:
_main in main.cpp.o
"_glfwWindowShouldClose", 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]: *** [dunjun] Error 1
make[2]: *** [CMakeFiles/dunjun.dir/all] Error 2
make[1]: *** [CMakeFiles/dunjun.dir/rule] Error 2
make: *** [dunjun] Error 2
Related
After a list of depreciation warnings for OpenGL, I get the following:
: && /Library/Developer/CommandLineTools/usr/bin/c++ -g -arch arm64 -isysroot /Library/Developer/CommandLineTools/SDKs/MacOSX13.1.sdk -Wl,-search_paths_first -Wl,-headerpad_max_install_names CMakeFiles/Project_1.dir/main.cpp.o -o Project_1 /Library/Developer/CommandLineTools/SDKs/MacOSX13.1.sdk/System/Library/Frameworks/OpenGL.framework/OpenGL.tbd && :
Undefined symbols for architecture arm64:
"_glfwCreateWindow", referenced from:
_main in main.cpp.o
"_glfwInit", referenced from:
_main in main.cpp.o
"_glfwMakeContextCurrent", referenced from:
_main in main.cpp.o
"_glfwPollEvents", referenced from:
_main in main.cpp.o
"_glfwSwapBuffers", referenced from:
_main in main.cpp.o
"_glfwTerminate", referenced from:
_main in main.cpp.o
"_glfwWindowShouldClose", referenced from:
_main in main.cpp.o
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 current CMake file:
cmake_minimum_required(VERSION 3.24)
project(Project_1)
set(CMAKE_CXX_STANDARD 17)
add_executable(Project_1 main.cpp)
find_package(OpenGL REQUIRED)
link_directories("/opt/homebrew/lib")
include_directories("/opt/homebrew/include")
target_link_libraries(Project_1 OpenGL::GL)
I've tried to explicitly tell the compiler to build for Apple Silicon with set(CMAKE_OSX_ARCHITECTURES "x86_64") to no avail.
I have created a small C++ SDL2 project with CMake under macOS Big Sur (m1).
This is the CMakeLists.txt:
cmake_minimum_required(VERSION 3.22.2)
project(sdl2gamepad)
add_library(sdl2lib SHARED IMPORTED)
set_property(TARGET sdl2lib PROPERTY IMPORTED_LOCATION "/opt/homebrew/lib/libSDL2.dylib")
include_directories("/opt/homebrew/include")
add_executable(${PROJECT_NAME} main.cpp)
target_link_libraries(${PROJECT_NAME} PRIVATE ${sdl2lib})
I installed SDL2 and CMake with homebrew.
The project was initially compiled and linked with the Makefile without problems and I was able to run it.
I then wanted to structure my project with an src and build folder and had adapted the CMakeLists.txt. Unfortunately, CMake/Make was no longer able to link the SDL2 lib.
Linking error message:
[ 50%] Linking CXX executable sdl2gamepad
Undefined symbols for architecture arm64:
"_SDL_CreateWindow", referenced from:
_main in main.cpp.o
"_SDL_DestroyWindow", referenced from:
_main in main.cpp.o
"_SDL_GetError", referenced from:
_main in main.cpp.o
"_SDL_GetWindowSurface", referenced from:
_main in main.cpp.o
"_SDL_Init", referenced from:
_main in main.cpp.o
"_SDL_LoadBMP_RW", referenced from:
_main in main.cpp.o
"_SDL_PollEvent", referenced from:
_main in main.cpp.o
"_SDL_Quit", referenced from:
_main in main.cpp.o
"_SDL_RWFromFile", referenced from:
_main in main.cpp.o
"_SDL_UpdateWindowSurface", referenced from:
_main in main.cpp.o
"_SDL_UpperBlit", referenced from:
_main in main.cpp.o
ld: symbol(s) not found for architecture arm64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make[2]: *** [sdl2gamepad] Error 1
make[1]: *** [CMakeFiles/sdl2gamepad.dir/all] Error 2
make: *** [all] Error 2
I then reset the CMakeLists.txt to the status where it had worked (code above). But unfortunately, the problem still exists.
But I can compile the code with this command, it is probably due to a CMake misconfiguration.
$ clang++ main.cpp -o main /opt/homebrew/lib/libSDL2.dylib -I /opt/homebrew/include
CMake Version is 3.22.2.
SDL2 Version is 2.0.20.
Many thanks for your help!
Introduction
I am trying to connect and interact with a mysql db running on my local, in a c++ project. I am also using cmake to compile my project.
Preparation
I installed mysqlconnector cpp by using mysql-connector-odbc-8.0.25-macos11-x86-64bit.dmg from their community downloads. I have the include and lib files available at /usr/local/mysql-connector-c+
CmakeLists.txt
Part of my CMakeLists.txt which deals with mysqlconnector is referenced below:
cmake_minimum_required(VERSION 3.13.4 FATAL_ERROR)
set(CMAKE_CXX_STANDARD 11)
set(FULL_PATH_TO_MYSQL_CONNECTOR_CPP_DIR "/usr/local/mysql-connector-c++")
include_directories(${FULL_PATH_TO_MYSQL_CONNECTOR_CPP_DIR}/include)
link_directories(${FULL_PATH_TO_MYSQL_CONNECTOR_CPP_DIR}/lib64)
add_executable(runservice main.cpp)
target_link_libraries(runservice mysqlcppconn)
The cmake command runs fine.
Build Error
When I try to build using '''make``` command, I get the following error :
Undefined symbols for architecture x86_64:
"mysqlx::abi2::r0::common::Value::print(std::__1::basic_ostream<char, std::__1::char_traits<char> >&) const", referenced from:
mysqlx::abi2::r0::Value::print(std::__1::basic_ostream<char, std::__1::char_traits<char> >&) const in main.cpp.o
construction vtable for mysqlx::abi2::r0::common::Value-in-mysqlx::abi2::r0::Value in main.cpp.o
"typeinfo for mysqlx::abi2::r0::common::Value", referenced from:
construction vtable for mysqlx::abi2::r0::common::Value-in-mysqlx::abi2::r0::Value in main.cpp.o
typeinfo for mysqlx::abi2::r0::Value in main.cpp.o
"vtable for mysqlx::abi2::r0::DbDoc", referenced from:
mysqlx::abi2::r0::Value::~Value() in main.cpp.o
__GLOBAL__sub_I_main.cpp 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 x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make[2]: *** [runservice] Error 1
make[1]: *** [CMakeFiles/runservice.dir/all] Error 2
make: *** [all] Error 2
I can't understand this error or how to go about solving this.
Thank you for your precious time
I am trying to complete a simple GLFW tutorial on mac using cmake, where I encounter a series of undefined symbol linking errors. I did my research regarding this issue and found no help. Below is my CMakeLists.txt.
cmake_minimum_required(VERSION 3.3)
project(GL_Template)
find_package(PkgConfig REQUIRED)
pkg_search_module(GLFW REQUIRED glfw3)
include_directories(${GLFW_INCLUDE_DIRS})
find_package(GLM REQUIRED)
find_package(GLEW REQUIRED STATIC)
include_directories(${GLM_INCLUDE_DIR})
include_directories(/usr/local/include)
include_directories(./include)
find_library(COCOA_LIBRARY Cocoa REQUIRED)
find_library(IOKIT_LIBRARY IOKit REQUIRED)
find_library(COREVID_LIBRARY CoreVideo REQUIRED)
message(${COCOA_LIBRARY})
message(${IOKIT_LIBRARY})
message(${COREVID_LIBRARY})
file(GLOB A_SOURCE ./src/*.cpp)
add_executable(Hello example/main.cpp ${A_SOURCE})
target_link_libraries(Hello ${GLEW_LIBRARY} ${GLFW3_LIBRARIES})
target_link_libraries(Hello ${COCOA_LIBRARY} ${COREVID_LIBRARY} ${IOKIT_LIBRARY})
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -framework Cocoa -framework OpenGL -framework IOKit")
Any help would be helpful.
Below is the error message
Undefined symbols for architecture x86_64:
"_glfwCreateWindow", referenced from:
_main in main.cpp.o
"_glfwGetTime", referenced from:
_main in main.cpp.o
"_glfwInit", referenced from:
_main in main.cpp.o
"_glfwMakeContextCurrent", referenced from:
_main in main.cpp.o
"_glfwPollEvents", referenced from:
_main in main.cpp.o
"_glfwSetCursorPosCallback", referenced from:
_main in main.cpp.o
"_glfwSetInputMode", referenced from:
_main in main.cpp.o
"_glfwSetKeyCallback", referenced from:
_main in main.cpp.o
"_glfwSetScrollCallback", referenced from:
_main in main.cpp.o
"_glfwSetWindowShouldClose", referenced from:
key_callback(GLFWwindow*, int, int, int, int) in main.cpp.o
"_glfwSwapBuffers", referenced from:
_main in main.cpp.o
"_glfwTerminate", referenced from:
_main in main.cpp.o
"_glfwWindowHint", referenced from:
_main in main.cpp.o
"_glfwWindowShouldClose", 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]: *** [Hello] Error 1
make[1]: *** [CMakeFiles/Hello.dir/all] Error 2
make: *** [all] Error 2
I found the solution. Obviously the GLFW3_LIBRARY is not defined and the defined variable should be GLFW_LIBRARY.
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.