简体   繁体   English

Xcode:使用C ++静态库

[英]Xcode: Using C++ static library

I want to use C++ static library which was built in Eclipse in Xcode 4: 我想使用在Xcode 4中的Eclipse中构建的C ++静态库:

For example, I have a very simple library: 例如,我有一个非常简单的库:

hello.h 你好

class Test{ public: static int getX();};

hello.cpp 你好

#include "hello.h"
int Test::getX(){return 10;}

So after building in Eclipse, I have a library .a. 因此,在Eclipse中构建之后,我有了一个库.a。 Now, I import .a file and the header to an Xcode app. 现在,我将.a文件和标头导入Xcode应用程序。 The problem is that in the .mm file (I created), I cannot call "int x = Test::getX();", it show 2 errors: 问题是,在.mm文件(我创建的)中,我无法调用“ int x = Test :: getX();”,它显示2个错误:

Undefined symbols for architecture i386: "Test::getX()", referenced from: -[LibFetching onlyfortest] in LibFetching.o 体系结构i386的未定义符号:“ Test :: getX()”,引用自:LibFetching.o中的-[LibFetching onlyfortest]

ld: symbol(s) not found for architecture i386 clang: error: linker command failed with exit code 1 (use -v to see invocation) ld:找不到体系结构i386铛的符号:错误:链接器命令失败,退出代码为1(使用-v查看调用)

Do you know how can I fix it? 你知道我该怎么解决吗? Thank you very much. 非常感谢你。

From the Question it seems that you are trying to link C++ static library to an Objective C++ project 从问题看来,您似乎正在尝试将C ++静态库链接到Objective C ++项目

Go to project -> Build Settings ,search for C++ Language Dialect and C++ Standard Library. 转到项目->构建设置,搜索C ++语言方言和C ++标准库。 Select options "Compiler Default" for both of them and it should work 为它们两个都选择选项“ Compiler Default”,它应该可以工作

Also check if you have to set the other linker flag to -lstdc++ 还要检查是否必须将另一个链接器标志设置为-lstdc ++

You will need to drag your static library to your Xcode project. 您将需要将静态库拖到Xcode项目中。 Then select the project you want to add the library to in the left navigator bar, the select the project target (or the target that needs the library if you have more than one). 然后在左侧导航器栏中选择要添加库的项目,选择项目目标(如果有多个库,则选择需要库的目标)。

Then click on Build Phases, and in the Link Binary with Libraries section (create one if it doesn't already exist), add your library, also, add it to your target dependency section as well. 然后单击“构建阶段”,然后在“使用库链接二进制文件”部分(如果尚不存在,则创建一个库)中,添加您的库,并将其也添加到目标依赖项部分。

You may need to to enter the path to you library headers in Header Search Path of your build settings. 您可能需要在构建设置的“标题搜索路径”中输入库标题的路径。

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

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