在 iOS 应用程序中链接静态库后出现 arm64 架构的未定义符号

6

我正在创建一个样例静态库,用于我的iOS应用程序中,但是,在调用静态库的方法时,我遇到了链接器错误:

Undefined symbols for architecture arm64:
"_doMath", referenced from:
  _doMathInterface in libTestMain.a(Test.o)
 (maybe you meant: _doMathInterface)
ld: symbol(s) not found for architecture arm64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

这是静态库的结构:

我有一个头文件Test.h:

#import <Foundation/Foundation.h>

@interface Test : NSObject

int doMathInterface(int a, int b);

@end

以及它的实现 Test.m :

#import "Test.h"
#include "PaymentAPI.h"

@implementation Test

int doMathInterface(int a, int b){
    return doMath(a, b);
}

@end

在 PaymentAPI.h 文件中:

#ifndef PaymentAPI_h
#define PaymentAPI_h

int doMath(int a, int b);

#endif /* PaymentAPI_h */

最后在PaymentAPI.cpp文件中:

#include <stdio.h>
#include "PaymentAPI.h"

int doMath(int a, int b){
    return a + b;
}

你可以看到这是一个非常简单的静态库,但我无法找出为什么会出现链接器错误,我已经在应用程序的“Link Binaries with Libraries”中添加了静态库。

以下是应用程序文件的截图:

enter image description here

而且构建设置中的搜索路径配置也是正确的:

enter image description here

以下是一些静态库项目的构建设置截图:

构建阶段: enter image description here

架构: enter image description here

非常感谢。

4个回答

2
问题在于您的doMath函数被编译为C++代码,这意味着函数名称会被C++编译器混淆。然而,您的Test.m文件是由(Objective-)C编译器消耗的,而C不使用名称混淆。
这意味着链接器最终会寻找错误的符号。您可以通过让C++编译器发出未混淆的函数名称来解决此问题。为此,您需要在PaymenAPI.h中使用extern "C",如下所示:
#ifndef PaymentAPI_h
#define PaymentAPI_h

#ifdef __cplusplus
extern "C" {
#endif

int doMath(int a, int b);

#ifdef __cplusplus
}
#endif

#endif /* PaymentAPI_h */

如果您想了解完整的解释,可以查看这个 SO 问题和被接受的答案:Combining C++ and C - how does #ifdef __cplusplus work?

该问题解释了如何在 C++ 和 C 语言中混合使用,并介绍了预处理指令 #ifdef __cplusplus 的作用。

0

如果你想使用C++进行静态库开发,最好使用Djinni,因为它可以处理桥接功能,包括函数名称和符号。


0

当您通过Objective-C调用C++函数时,您必须创建包装类(.mm文件)。

请参考此链接


-1

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接