简体   繁体   English

如何在MAC OS X的CMAKE项目中包括C静态库

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

I am trying to learn Core Foundation with C/C++. 我正在尝试使用C / C ++学习Core Foundation。 I use JetBrains CLion that uses CMAKE. 我使用使用CMAKE的JetBrains CLion。

The problem is - I dunno how to include proper static libraries in C/C++ Makefile project on Mac. 问题是-我不知道如何在Mac上的C / C ++ Makefile项目中包括适当的静态库。

I need to link static library CFNetwork to my project in order to fix linking problems. 我需要将静态库CFNetwork链接到我的项目,以解决链接问题。

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 感谢此资源: http : //raycast.net/clion-multiple-binaries

This is how my CMAKE file looks like and everything links: 这是我的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})

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. 我不确定这是否是您要解决的问题,但是您通过将-llibrary标志提供给编译器(其中库文件的名称为liblibrary.a)来链接C静态库。 If the library isn't in some default library location (idk what it is for mac) you can specify the path with -L flag. 如果该库不在某个默认库位置(idk对于Mac是什么),则可以使用-L标志指定路径。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM