gpt4 book ai didi

c++ - c和c++混合

转载 作者:行者123 更新时间:2023-11-28 03:32:31 26 4
gpt4 key购买 nike

我需要从 c 文件调用 cpp 方法。我为此编写了这个界面..

cpp文件

extern "C" void C_Test(int p){
Class::CPP_Test(p);
}

c文件

extern void C_Test(int p);


void C_Function(){
C_Test(10); //error
}

我在 c 文件中遇到错误“未定义对 C_Test(int) 的引用”

知道哪里出了问题吗?

最佳答案

您必须只为函数原型(prototype)声明extern,并确保正确链接。对此上瘾,CPP_Test(p) 必须是 Class 的静态成员,否则您的代码将无法运行。最后,extern "C"必须将其内容括在括号中,更像是

extern "C"
{
void C_Test(int p)
{
Class::CPP_Test(p);
}
}

告诉我们这是否有效。

关于c++ - c和c++混合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12072502/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com