<function>引自; 符号未找到

Jus*_*kva 4 c c++ compiler-construction xcode

我有一段C代码,用于C++函数.在我的C++文件的顶部,我有一行:#include "prediction.h"

prediction.h我有这样的:

#ifndef prediction  
#define prediction  

#include "structs.h"  

typedef struct {  
    double estimation;  
    double variance;  
} response;

response runPrediction(int obs, location* positions, double* observations,
                        int targets, location* targetPositions);

#endif
Run Code Online (Sandbox Code Playgroud)

我也有prediction.c,它有:

#include "prediction.h"  

response runPrediction(int obs, location* positions, double* observations,
                        int targets, location* targetPositions) {  
    // code here  
}
Run Code Online (Sandbox Code Playgroud)

现在,在我的C++文件中(正如我所说的包括prediction.h)我调用该函数,然后编译(通过Xcode)我得到这个错误:

"runPrediction(int,location*,double*,int,location*)",引自:
MainFrame中的mainFrame :: respondTo(char*,int)
ld:未找到符号
collect2:ld返回1退出状态

prediction.c被标记为当前目标的编译.我没有任何其他.cpp文件没有被编译的问题.这有什么想法?

GMa*_*ckG 6

可能这个函数的名称正在被破坏*.您需要执行以下操作:

extern "C" response runPrediction(int obs, location* positions,
                   double* observations, int targets, location* targetPositions);
Run Code Online (Sandbox Code Playgroud)

这告诉它将其视为C函数声明.

*C++破坏函数名称,以便在链接阶段为它们提供唯一的名称,以实现函数重载.C没有函数重载所以没有这样的事情.


你知道,extern "C"如果你有多个东西,你也可以做一个块:

extern "C"
{
    response runPrediction(int obs, location* positions,
                   double* observations, int targets, location* targetPositions);

    // other stuff
}
Run Code Online (Sandbox Code Playgroud)

保罗建议的那样,允许标题在两者中用于__cplusplus调节它:

#ifdef __cplusplus
    #define EXTERN_C extern "C"
#else
    #define EXTERN_C
#endif

EXTERN_C response runPrediction(int obs, location* positions,
                   double* observations, int targets, location* targetPositions);
Run Code Online (Sandbox Code Playgroud)