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.
Related
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!
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'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
I'm working on compiling a SCIP dynamic library. SCIP is implemented in C, and our optimization project is all Java, so we're wanting to use JNA to access it. We've written a C++ wrapper to build the dynamic library, and I'm having issues compiling it on my Mac (running Yosemite 10.10.2). Here's my version of gcc and g++:
> gcc --version
Configured with: --prefix=/Applications/Xcode.app/Contents/Developer/usr --with-gxx-include-dir=/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.10.sdk/usr/include/c++/4.2.1
Apple LLVM version 6.0 (clang-600.0.57) (based on LLVM 3.5svn)
Target: x86_64-apple-darwin14.1.0
Thread model: posix
> g++ --version
Configured with: --prefix=/Applications/Xcode.app/Contents/Developer/usr --with-gxx-include-dir=/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.10.sdk/usr/include/c++/4.2.1
Apple LLVM version 6.0 (clang-600.0.57) (based on LLVM 3.5svn)
Target: x86_64-apple-darwin14.1.0
Thread model: posix
We're using CMake (version 3.2.1) to build the project. Here's our main CMakeLists.txt:
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++0x -O2 -Wall -DNDEBUG")
if(CMAKE_COMPILER_IS_GNUCXX)
if("${CMAKE_SYSTEM_PROCESSOR}" STREQUAL "x86_64")
set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -fPIC" )
set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -fPIC" )
endif()
endif()
if(APPLE)
set(CMAKE_XCODE_ATTRIBUTE_CLANG_CXX_LANGUAGE_STANDARD "c++0x")
set(CMAKE_XCODE_ATTRIBUTE_CLANG_CXX_LIBRARY "libc++")
set(CMAKE_XCODE_ATTRIBUTE_GCC_VERSION "com.apple.compilers.llvm.clang.1_0")
set(CMAKE_MACOSX_RPATH 1)
set(CMAKE_OSX_ARCHITECTURES "x86_64")
endif()
if(WIN32)
add_definitions(-DUNICODE -D_UNICODE)
set(TARGET_ARCHITECTURE "x86")
endif()
set(LIB_TYPE SHARED)
include_directories("/path/to/scipoptsuite-3.1.1/scip-3.1.1/src")
include_directories("${CMAKE_CURRENT_SOURCE_DIR}")
add_library(scipLibrary ${LIB_TYPE} "stdafx.cpp")
Here's the output of make VERBOSE=1:
/usr/local/Cellar/cmake/3.2.1/bin/cmake -H/path/to/scip -B/path/to/scip/build --check-build-system CMakeFiles/Makefile.cmake 0
/usr/local/Cellar/cmake/3.2.1/bin/cmake -E cmake_progress_start /path/to/scip/build/CMakeFiles /path/to/scip/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 source/CMakeFiles/scipLibrary.dir/build.make source/CMakeFiles/scipLibrary.dir/depend
cd /path/to/scip/build && /usr/local/Cellar/cmake/3.2.1/bin/cmake -E cmake_depends "Unix Makefiles" /path/to/scip /path/to/scip/source /path/to/scip/build /path/to/scip/build/source /path/to/scip/build/source/CMakeFiles/scipLibrary.dir/DependInfo.cmake --color=
/Applications/Xcode.app/Contents/Developer/usr/bin/make -f source/CMakeFiles/scipLibrary.dir/build.make source/CMakeFiles/scipLibrary.dir/build
/usr/local/Cellar/cmake/3.2.1/bin/cmake -E cmake_progress_report /path/to/scip/build/CMakeFiles 1
[100%] Building CXX object source/CMakeFiles/scipLibrary.dir/stdafx.cpp.o
cd /path/to/scip/build/source && /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/c++ -DscipLibrary_EXPORTS -std=c++0x -O2 -Wall -DNDEBUG -arch x86_64 -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.10.sdk -fPIC -I/path/to/scipoptsuite-3.1.1/scip-3.1.1/src -I/path/to/scip/source -o CMakeFiles/scipLibrary.dir/stdafx.cpp.o -c /path/to/scip/source/stdafx.cpp
Linking CXX shared library ../output/libscipLibrary.dylib
cd /path/to/scip/build/source && /usr/local/Cellar/cmake/3.2.1/bin/cmake -E cmake_link_script CMakeFiles/scipLibrary.dir/link.txt --verbose=1
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/c++ -std=c++0x -O2 -Wall -DNDEBUG -arch x86_64 -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.10.sdk -dynamiclib -Wl,-headerpad_max_install_names -o ../output/libscipLibrary.dylib -install_name #rpath/libscipLibrary.dylib CMakeFiles/scipLibrary.dir/stdafx.cpp.o
Undefined symbols for architecture x86_64:
"_SCIPaddBilinTermQuadratic", referenced from:
_LibSCIPaddBilinTermQuadratic in stdafx.cpp.o
"_SCIPaddCoefLinear", referenced from:
_LibSCIPaddCoefLinear in stdafx.cpp.o
"_SCIPaddCons", referenced from:
_LibSCIPaddCons in stdafx.cpp.o
"_SCIPaddQuadVarLinearCoefQuadratic", referenced from:
_LibSCIPaddQuadVarLinearCoefQuadratic in stdafx.cpp.o
"_SCIPaddSquareCoefQuadratic", referenced from:
_LibSCIPaddSquareCoefQuadratic in stdafx.cpp.o
"_SCIPaddVar", referenced from:
_LibSCIPaddVar in stdafx.cpp.o
"_SCIPblkmem", referenced from:
_LibSCIPexprCreateLinear in stdafx.cpp.o
_LibSCIPexprCreate in stdafx.cpp.o
_LibSCIPexprCreateMonomial in stdafx.cpp.o
_LibSCIPexprCreatePolynomial in stdafx.cpp.o
_LibSCIPexprtreeCreate in stdafx.cpp.o
"_SCIPchgVarLb", referenced from:
_LibSCIPchgVarLb in stdafx.cpp.o
"_SCIPchgVarObj", referenced from:
_LibSCIPchgVarObj in stdafx.cpp.o
"_SCIPchgVarUb", referenced from:
_LibSCIPchgVarUb in stdafx.cpp.o
"_SCIPcreate", referenced from:
_LibSCIPcreate in stdafx.cpp.o
"_SCIPcreateConsBasicAbspower", referenced from:
_LibSCIPcreateConsBasicAbspower in stdafx.cpp.o
"_SCIPcreateConsBasicLinear", referenced from:
_LibSCIPcreateConsBasicLinear in stdafx.cpp.o
"_SCIPcreateConsBasicNonlinear", referenced from:
_LibSCIPcreateConsBasicNonlinear in stdafx.cpp.o
"_SCIPcreateConsBasicQuadratic", referenced from:
_LibSCIPcreateConsBasicQuadratic in stdafx.cpp.o
"_SCIPcreateConsBasicSetpart", referenced from:
_LibSCIPcreateConsBasicSetpart in stdafx.cpp.o
"_SCIPcreateProbBasic", referenced from:
_LibSCIPcreateProbBasic in stdafx.cpp.o
"_SCIPcreateVarBasic", referenced from:
_LibSCIPcreateVarBasic in stdafx.cpp.o
"_SCIPexprCreate", referenced from:
_LibSCIPexprCreate in stdafx.cpp.o
"_SCIPexprCreateLinear", referenced from:
_LibSCIPexprCreateLinear in stdafx.cpp.o
"_SCIPexprCreateMonomial", referenced from:
_LibSCIPexprCreateMonomial in stdafx.cpp.o
"_SCIPexprCreatePolynomial", referenced from:
_LibSCIPexprCreatePolynomial in stdafx.cpp.o
"_SCIPexprtreeCreate", referenced from:
_LibSCIPexprtreeCreate in stdafx.cpp.o
"_SCIPexprtreeFree", referenced from:
_LibSCIPexprtreeFree in stdafx.cpp.o
"_SCIPexprtreeSetVars", referenced from:
_LibSCIPexprtreeSetVars in stdafx.cpp.o
"_SCIPfree", referenced from:
_LibSCIPfree in stdafx.cpp.o
"_SCIPfreeTransform", referenced from:
_LibSCIPfreeTransform in stdafx.cpp.o
"_SCIPgetBestSol", referenced from:
_LibSCIPprintSolution in stdafx.cpp.o
_LibSCIPgetBestSol in stdafx.cpp.o
"_SCIPgetNSols", referenced from:
_LibSCIPgetNSols in stdafx.cpp.o
"_SCIPgetStatus", referenced from:
_LibSCIPgetStatus in stdafx.cpp.o
"_SCIPincludeDefaultPlugins", referenced from:
_LibSCIPincludeDefaultPlugins in stdafx.cpp.o
"_SCIPmajorVersion", referenced from:
_LibSCIPmajorVersion in stdafx.cpp.o
"_SCIPmessagePrintError", referenced from:
_LibSCIPincludeDefaultPlugins in stdafx.cpp.o
_LibSCIPcreateProbBasic in stdafx.cpp.o
_LibSCIPchgVarLb in stdafx.cpp.o
_LibSCIPchgVarUb in stdafx.cpp.o
_LibSCIPaddVar in stdafx.cpp.o
_LibSCIPexprtreeSetVars in stdafx.cpp.o
_LibSCIPexprtreeFree in stdafx.cpp.o
...
"_SCIPmessagePrintErrorHeader", referenced from:
_LibSCIPincludeDefaultPlugins in stdafx.cpp.o
_LibSCIPcreateProbBasic in stdafx.cpp.o
_LibSCIPchgVarLb in stdafx.cpp.o
_LibSCIPchgVarUb in stdafx.cpp.o
_LibSCIPaddVar in stdafx.cpp.o
_LibSCIPexprtreeSetVars in stdafx.cpp.o
_LibSCIPexprtreeFree in stdafx.cpp.o
...
"_SCIPminorVersion", referenced from:
_LibSCIPminorVersion in stdafx.cpp.o
"_SCIPpresolve", referenced from:
_LibSCIPpresolve in stdafx.cpp.o
"_SCIPprintOrigProblem", referenced from:
_LibSCIPprintProblemCIP in stdafx.cpp.o
_LibSCIPprintProblemMPS in stdafx.cpp.o
_LibSCIPprintProblemLP in stdafx.cpp.o
_LibSCIPprintProblemCIPToFile in stdafx.cpp.o
_LibSCIPprintProblemMPSToFile in stdafx.cpp.o
_LibSCIPprintProblemLPToFile in stdafx.cpp.o
"_SCIPprintSol", referenced from:
_LibSCIPprintSolution in stdafx.cpp.o
"_SCIPreleaseCons", referenced from:
_LibSCIPreleaseCons in stdafx.cpp.o
"_SCIPreleaseVar", referenced from:
_LibSCIPreleaseVar in stdafx.cpp.o
"_SCIPsetObjsense", referenced from:
_LibSCIPsetObjsense in stdafx.cpp.o
"_SCIPsolve", referenced from:
_LibSCIPsolve in stdafx.cpp.o
"_SCIPsubversion", referenced from:
_LibSCIPsubversion in stdafx.cpp.o
"_SCIPtechVersion", referenced from:
_LibSCIPtechVersion in stdafx.cpp.o
"_SCIPvarGetLPSol_rec", referenced from:
solGetArrayVal(SCIP_Sol*, SCIP_Var*) in stdafx.cpp.o
"_SCIPvarGetNLPSol_rec", referenced from:
solGetArrayVal(SCIP_Sol*, SCIP_Var*) in stdafx.cpp.o
"_SCIPvarGetOrigvarSum", referenced from:
_LibSCIPsolGetVal in stdafx.cpp.o
"_SCIPvarGetPseudoSol", referenced from:
solGetArrayVal(SCIP_Sol*, SCIP_Var*) in stdafx.cpp.o
"_SCIPversion", referenced from:
_LibSCIPversion in stdafx.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]: *** [output/libscipLibrary.dylib] Error 1
make[1]: *** [source/CMakeFiles/scipLibrary.dir/all] Error 2
make: *** [all] Error 2
You can see that the problem is that the linker is giving us an ld: symbol(s) not found for architecture x86_64 error. I've searched a lot to try and figure out how to solve this, but I haven't had any luck.
I'm not sure it's necessary to provide the contents of stdafx.h or stdafx.cpp, so I'll leave them out for now, but if it's necessary, let me know, and I can provide them.
Ok, I had made a mistake where I didn't build scipoptlib, so there was no library to build against (sorry, I've never created a dynamic library before). Running make scipoptlib IPOPT=true SHARED=true in /path/to/scipoptsuite-3.1.1 was all that was needed to do that.
Here's the updated CMakeLists.txt that references the single shared scipoptlib:
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++0x -O2 -Wall -DNDEBUG")
if(CMAKE_COMPILER_IS_GNUCXX)
if("${CMAKE_SYSTEM_PROCESSOR}" STREQUAL "x86_64")
set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE}")
set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG}")
endif()
endif()
if(APPLE)
set(CMAKE_XCODE_ATTRIBUTE_CLANG_CXX_LANGUAGE_STANDARD "c++0x")
set(CMAKE_XCODE_ATTRIBUTE_CLANG_CXX_LIBRARY "libc++")
set(CMAKE_XCODE_ATTRIBUTE_GCC_VERSION "com.apple.compilers.llvm.clang.1_0")
set(CMAKE_MACOSX_RPATH 1)
endif()
if(WIN32)
add_definitions(-DUNICODE -D_UNICODE)
set(TARGET_ARCHITECTURE "x86")
endif()
include_directories("/path/to/scipoptsuite-3.1.1/scip-3.1.1/src/")
add_library(scipLibrary SHARED "stdafx.cpp")
target_link_libraries(scipLibrary "/path/to/scipoptsuite-3.1.1/lib/libscipopt-3.1.1.darwin.x86_64.gnu.opt.so")
I can actually generate the dylib now, which is great! However, now JNA is throwing up (saying it can't find the dylib), but that's probably the topic of another SO thread.
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.