在 xcode 中创建和使用静态库

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2533580/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 19:14:27  来源:igfitidea点击:

Creating and using a static lib in xcode

c++xcodemacosstatic-libraries

提问by Alasdair Morrison

I am trying to create a static library in xcode and link to that static library from another program.

我正在尝试在 xcode 中创建一个静态库并从另一个程序链接到该静态库。

So as a test i have created a BSD static C library project and just added the following code:

因此,作为测试,我创建了一个 BSD 静态 C 库项目,并添加了以下代码:

//Test.h

//测试.h

int testFunction();

//Test.cpp

//测试.cpp

#include "Test.h"
int testFunction() {
return 12;
}

This compiles fine and create a .a file (libTest.a).

这可以很好地编译并创建一个 .a 文件 (libTest.a)。

Now i want to use it in another program so I create a new xcode project (cocoa application) Have the following code:

现在我想在另一个程序中使用它,所以我创建了一个新的 xcode 项目(可可应用程序)有以下代码:

//main.cpp

//main.cpp

#include <iostream>
#include "Testlib.h"

int main (int argc, char * const argv[]) {
    // insert code here...
    std::cout << "Result:\n" <<testFunction();
    return 0;
}

//Testlib.h

//测试库.h

extern int testFunction();

I right clicked on the project -> add -> existing framework -> add other Selected the .a file and it added it into the project view.

我右键单击项目 -> 添加 -> 现有框架 -> 添加其他 选择了 .a 文件并将其添加到项目视图中。

I always get this linker error:

我总是收到此链接器错误:

Build TestUselibrary of project TestUselibrary with configuration Debug

Ld build/Debug/TestUselibrary normal x86_64
cd /Users/myname/location/TestUselibrary
setenv MACOSX_DEPLOYMENT_TARGET 10.6
/Developer/usr/bin/g++-4.2 -arch x86_64 -isysroot /Developer/SDKs/MacOSX10.6.sdk 
-L/Users/myname/location/TestUselibrary/build/Debug  
 -L/Users/myname/location/TestUselibrary/../Test/build/Debug 
-F/Users/myname/location/TestUselibrary/build/Debug 
-filelist /Users/myname/location/TestUselibrary/build/TestUselibrary.build/Debug/TestUselibrary.build/Objects-normal/x86_64/TestUselibrary.LinkFileList 
-mmacosx-version-min=10.6 -lTest -o /Users/myname/location/TestUselibrary/build/Debug/TestUselibrary



Undefined symbols:
  "testFunction()", referenced from:
      _main in main.o
ld: symbol(s) not found
collect2: ld returned 1 exit status

I am new to macosx development and fairly new to c++. I am probably missing something fairly obvious, all my experience comes from creating dlls on the windows platform. I really appreciate any help.

我是 macosx 开发的新手,也是 C++ 的新手。我可能遗漏了一些相当明显的东西,我所有的经验都来自在 Windows 平台上创建 dll。我真的很感激任何帮助。

采纳答案by Georg Fritzsche

Are you sure that the libraries source file is named Test.cppand not Test.c? With .ci get exactly the same error.

您确定库源文件已命名Test.cpp而不是Test.c? 随着.c我得到完全相同的错误。

If it is Test.cyou need to add extern "C"to the header for C++. E.g.:

如果是,Test.c您需要添加extern "C"到 C++ 的头文件中。例如:

#ifdef __cplusplus
extern "C" {
#endif

int testFunction();

#ifdef __cplusplus
}
#endif

See e.g. the C++ FAQ lite entryfor more details.

有关更多详细信息,请参见例如C++ FAQ lite 条目

回答by Paul R

You don't add the library (.a file) as a framework - it's just a library - add it to the project like you would add a source file.

您不会将库(.a 文件)添加为框架 - 它只是一个库 - 将其添加到项目中,就像添加源文件一样。

Also note that you don't need Testlib.h- just #includethe original Test.hin main.cpp.

还要注意的是,你并不需要Testlib.h-只是#include原来Test.hmain.cpp