简体   繁体   English

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

[英]Undefined symbols for architecture arm64 after linking a static library in iOS app

I am creating a sample static library to be used in my iOS app, however, when calling the static library's methods, I ran into a linker error: I am creating a sample static library to be used in my iOS app, however, when calling the static library's methods, I ran into a linker error:

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)

Here is the static library's structure:这是 static 库的结构:

I have a header file Test.h:我有一个 header 文件 Test.h:

#import <Foundation/Foundation.h>

@interface Test : NSObject

int doMathInterface(int a, int b);

@end

and its implementation Test.m:及其实现Test.m:

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

@implementation Test

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

@end

and in PaymentAPI.h:在 PaymentAPI.h 中:

#ifndef PaymentAPI_h
#define PaymentAPI_h

int doMath(int a, int b);

#endif /* PaymentAPI_h */

Finally in PaymentAPI.cpp:最后在 PaymentAPI.cpp 中:

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

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

As you can see it's a very simple static library but I couldn't figure out why this linker error is happening, I have added the static library in the "Link Binaries with Libraries" in Build Phases in the app.正如您所看到的,这是一个非常简单的 static 库,但我无法弄清楚为什么会发生此 linker 错误,我已在“inLink Binaries”库中添加了 static 库中的 static 库。

Here is a screenshot of the app 's files:这是应用程序文件的屏幕截图:

在此处输入图像描述

and search paths configuration in build settings is also correct, I believe:并且构建设置中的搜索路径配置也是正确的,我相信:

在此处输入图像描述

Here is a screenshot of some build settings for the static library project这是static 库项目的一些构建设置的屏幕截图

Build Phases:构建阶段: 在此处输入图像描述

Architecture:建筑学: 在此处输入图像描述

Thanks a lot.非常感谢。

The problem is that your doMath function gets compiled as C++ code, which means that the function name gets mangled by the C++ compiler. The problem is that your doMath function gets compiled as C++ code, which means that the function name gets mangled by the C++ compiler. Your Test.m file however gets consumed by the (Objective-)C compiler and C doesn't use name mangling.但是,您的Test.m文件会被(Objective-)C 编译器使用,并且 C 不使用名称修饰。

This means that the linker will end up looking for the wrong symbol.这意味着 linker 最终会寻找错误的符号。 You can fix this by having the C++ compiler emit an unmangled function name.您可以通过让 C++ 编译器发出未损坏的 function 名称来解决此问题。 To do so, you'll have to use extern "C" in PaymenAPI.h like so:为此,您必须在 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 */

For a complete explanation you can take a look at this SO question and the accepted answer: Combining C++ and C - how does #ifdef __cplusplus work?要获得完整的解释,您可以查看这个 SO 问题和接受的答案: Combining C++ 和 C - #ifdef __cplusplus 如何工作?

When you are calling C++ function through Objective-C you have to create wrapper class(.mm file).当您通过 Objective-C 调用 C++ function 时,您必须创建包装类(.mm 文件)。

Please refer this link 请参考这个链接

you better use djinni if you want to do static libraries with C++ since it will handle the bridging functionalities including functions names and symbols.如果你想用 C++ 做 static 库,你最好使用 djinni,因为它将处理包括函数名称和符号在内的桥接功能。

The linker sees the symbol name "_doMath" not "doMath" - so I think you do the interop not correctly. linker 看到符号名称“_doMath”而不是“doMath” - 所以我认为您没有正确执行互操作。

I found this: mixing-objective-c-and-c And here is a good article which explains how to do interop between C++ and ObjectiveC / ObjeciveC++:我发现了这个: mixing-objective-c-and-c这是一篇很好的文章,它解释了如何在 C++ 和 ObjectiveC / ObjeciveC++ 之间进行互操作:

https://www.drdobbs.com/cpp/interoperating-between-c-and-objective-c/240165502 https://www.drdobbs.com/cpp/interoperating-between-c-and-objective-c/240165502

=> try to rename your.m file to.mm => 尝试将您的.m 文件重命名为.mm

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

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