本文介绍了从Objective-C ++调用C函数的链接器错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个奇怪的链接器问题.我有看起来像这样的代码:

I've got a weird linker issue. I have code that looks like so:

    double given_amount = self.modelController.levelCompleteRewardAmount;
    swrve_currency_given(swrve, (CFStringRef)@"currencyName", given_amount);

我在两个不同的地方都有这段代码:在Objective-C和Objective-C ++文件中.它可以在Objective-C领域中正常编译,但是swrve_currency_given()函数在WGController.mm文件中导致以下情况:

I have this code in two separate places: In an objective-c and an objective-c++ file. It compiles fine in objective-C land, but the swrve_currency_given() function causes the following in my WGController.mm file:

Undefined symbols for architecture armv7:
  "swrve_currency_given(Swrve*, __CFString const*, double)", referenced from:
      -[WGController giveTheUserSomeCashForPlayingThisLevel] in WGController.o
ld: symbol(s) not found for architecture armv7
collect2: ld returned 1 exit status

我不确定这个错误是否与Obj-C vs. C ++有关,但是像这样感觉.我的理论是,也许它认为它是Obj-C类的函数? "swrve"代码是第三方代码,一个.h和.c文件,我正在这样导入:

I'm not entirely sure if this error is related to the Obj-C vs. C++ thing, but it feels like it. My theory is that it perhaps thinks that it is a function on the Obj-C class? The 'swrve' code is 3rd party code, one .h and .c file and I'm importing like so:

#import "swrve.h"

感谢您的帮助!谢谢

推荐答案

您可能需要在函数原型周围加上以下内容:

You may need to surround the function prototype with:

#if defined __cplusplus
extern "C" {
#endif

void swrve_currency_given (...whatever goes here...);

#if defined __cplusplus
};
#endif

这告诉编译器它是C函数,而不是C ++函数.

That tells the compiler that it's a C function and not a C++ function.

这篇关于从Objective-C ++调用C函数的链接器错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-25 01:21