我正在尝试使用C / C ++学习Core Foundation。 我使用的是使用CMAKE的JetBrains CLion。
问题是 - 我不知道如何在Mac上的C / C ++ Makefile项目中包含适当的静态库。
我需要将静态库CFNetwork链接到我的项目以修复链接问题。
你可以给我一个快速提示吗?我的情况:
#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;
}
我可以编译它,但链接器输出以下内容:
"/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
答案 0 :(得分:1)
终于解决了。
感谢此资源:http://raycast.net/clion-multiple-binaries
这就是我的CMAKE文件的样子和所有链接:
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})
答案 1 :(得分:0)
我不是百分之百确定这是你的想法,但你通过给你的编译器链接C静态库,你的编译器库文件的名称是liblibrary.a。如果库不在某个默认库位置(idk是什么用于mac),则可以使用-L标志指定路径。