<;功能>;引用自;找不到符号 我有一个C代码,它是从C++函数中使用的。在我的C++文件的顶部,我有一行:包含“预测h”/
在<;功能>;引用自;找不到符号 我有一个C代码,它是从C++函数中使用的。在我的C++文件的顶部,我有一行:包含“预测h”/ ,c++,c,xcode,compiler-construction,C++,C,Xcode,Compiler Construction,在prediction.h中,我有: #ifndef prediction #define prediction #include "structs.h" typedef struct { double estimation; double variance; } response; response runPrediction(int obs, location* positions, double* observations,
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
我还有prediction.c
,它有:
#include "prediction.h"
response runPrediction(int obs, location* positions, double* observations,
int targets, location* targetPositions) {
// code here
}
现在,在我的C++文件中(我说的包括预测.h),我调用那个函数,然后编译(通过xCal码),我得到这个错误:
“运行预测(int,location*,double*,int,location*)”,引用自:mainFrame::在mainFrame.o中响应(char*,int)
ld:找不到符号
collect2:ld返回了1个退出状态
c标记为当前目标的编译。我对其他未编译的.cpp文件没有任何问题。有什么想法吗?函数的名称可能是*。您需要执行以下操作:
extern "C" response runPrediction(int obs, location* positions,
double* observations, int targets, location* targetPositions);
它告诉它将其视为C函数声明
*在函数重载的链接阶段,C++会对函数名进行修改,使其具有唯一的名称。C没有函数重载,所以没有这样的事情
正如您所知,如果有多个东西需要外部处理,您还可以创建一个
extern“C”
块:
extern "C"
{
response runPrediction(int obs, location* positions,
double* observations, int targets, location* targetPositions);
// other stuff
}
和建议的一样,要允许在两种情况下都使用头,请使用\uuucplusplus
对其进行调节:
#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);
更改prediction.h,使其如下所示:
#ifndef prediction
#define prediction
#include "structs.h"
#ifdef __cplusplus
extern "C" {
#endif
typedef struct {
double estimation;
double variance;
} response;
response runPrediction(int obs, location* positions, double* observations,
int targets, location* targetPositions);
#ifdef __cplusplus
}
#endif
#endif
是“预测”C而不是C++?根据文件扩展名,您将有不同的默认编译,GMan对此进行了介绍
如果是C++,你改变文件名,我仍然会在头部中预测运行预测,在本地C++框架中定义类宏。在4个空格中预先定义它。您可以通过在代码周围加上反勾来执行内联代码(注释中的唯一方法),这样“some code”就变成了
some code
。我用反斜杠避开了前面的反斜杠。啊,我明白了,我想我试着同时使用了块引号和4个空格;难怪它不起作用对于混合C/C++项目,最好用“C++”代码“>代码”>“IFDEF”、“CPLUS PLUS”、“代码> >代码> >“NEXF ”,使标题可以同时包含C和C++源。files@Paul:我忘了那个细节如果你不介意的话,我想补充一下。我不知道这是怎么回事。我把代码>外部“C”响应运行预测…<代码>,我得到这个错误:<代码>预期标识符或''在字符串常量 @ JFM429:是在C或C++中编译的吗?C没有代码>外部“C”,因此预处理器定义了切换它。HUH,条件Extn工作。是否有可能某种程度上它被编译成C,并且曾经作为C++?无论如何,我把条件IFDEF放入其中,它解决了问题。谢谢!如果我能接受多个答案,那就好了,这也和先前的答案都有帮助。谢谢。伙计们!