【问题标题】:Using xcode to create static library with C, Error: "Undefined symbols for architecture i386"!使用 xcode 用 C 创建静态库,错误:“架构 i386 的未定义符号”!
【发布时间】:2013-12-02 11:11:05
【问题描述】:

我正在使用 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 Device”和“iPhone Retina(4-inch)”构建时,我得到了两个 x.a 文件。

使用lipo工具和-create参数输出新的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 文件?

【问题讨论】:

    标签: ios c xcode i386


    【解决方案1】:

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

    作为

    extern "C" void test_c_fun();
    

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-08-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-10-23
      • 2011-08-14
      相关资源
      最近更新 更多