我正在使用 xcode 通过 C 创建一个静态库,我似乎得到了一个错误 Undefined symbols for architecture i386
。
静态库工程,包括三个文件:fun.c
、testFun.cpp
、testFun.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,支持arm
和i386
。
将 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/