ios - 使用xcode创建带C的静态库,报错: "Undefined symbols for architecture i386"!

标签 ios c xcode i386

我正在使用 xcode 通过 C 创建一个静态库,我似乎得到了一个错误 Undefined symbols for architecture i386

静态库工程,包括三个文件:fun.ctestFun.cpptestFun.h

这是testFun.cpp

#include "testFun.h"
extern void test_c_fun();
void TestFun::test()
{
    printf("# TestFun c++ # ");
    test_c_fun();
}

这里是fun.c

#include <stdio.h>
void test_c_fun() 
{ 
    printf("# test_c_fun #"); 
}

当我使用“IOS 设备”和“iPhone Retina(4 英寸)”构建时,我得到两个 x.a 文件。

使用带有-create参数的lipo工具输出新的x.a,支持armi386

将 x.a 添加到我的项目中,现在包含 testFun 头文件代码:

TestFun tf;
tf.test();

然后构建它,我得到这些错误

Undefined symbols for architecture i386: "test_c_fun()", referenced from: TestFun::test() in libstatistic.a(testFun.o) ld: symbol(s) not found for architecture i386

当我隐藏c-fun调用(test_c_fun),那么构建就成功了!

看起来像:

#include "testFun.h"
extern void test_c_fun();
void TestFun::test()
{
    printf("# TestFun c++ # ");
    //test_c_fun();
}

为什么不能用C文件?

最佳答案

在testFun.cpp中,使用extern C声明C函数

作为

extern "C" void test_c_fun();

C 风格函数有不同的名称修改规则。当您在 .cpp 文件中声明 C 函数时,它将将该函数视为 C++ 函数。

当您在声明前添加 extern "C" 时,它将将该函数视为 C 函数。

关于ios - 使用xcode创建带C的静态库,报错: "Undefined symbols for architecture i386"!,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20326860/

相关文章:

ios - 使用 AVAudioSession 更改音频输入源会导致崩溃

ios - mas_updateConstraints 没有移除 Masonry 中的约束

c - 释放指向字符数组的指针的内存

ios - 尝试在 Xcode 中使用嵌入式框架会给出 "Use of undeclared type error"

ios - 如何知道最后一次异步服务器调用何时结束

iphone - 将 UILabels 从任何位置旋转到直立

c++ - 如何创建变量数组 Arduino

c - 为什么打印 unsigned char 会输出这么大的数字?

ios - 无法更新 XCode : Not enough space

ios - XCode 5.1 模拟器 - 无法实例化名为 UIStoryboardUnwindSegueTemplate 的类'