gpt4 book ai didi

c++ - 在 "C"代码中使用 "C++"函数和变量

转载 作者:搜寻专家 更新时间:2023-10-31 01:21:39 24 4
gpt4 key购买 nike

我需要从“C++”文件调用“C”函数,它有一个参数变量 “struct”(在“C”文件中定义)。

我使用了 'extern "C"',但出现链接错误。

请给我一些建议?

谢谢普里亚舒

最佳答案

由于您还没有回来,这里是您可能应该查看的可能的文件排列和头文件。

//这是头文件(myh.h)

struct S{};

#ifdef __cplusplus
extern "C" {
#endif

void fn(S s);

#ifdef __cplusplus
}
#endif

//这是CPP文件(ZCPP.cpp)

#include "myh.h"

int main(){
S s;
fn(s);
}

//这是C文件(ZC.c)

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

void fn(S s){
printf("Hi\n");
}

[prompt@test ~]$ g++ zcpp.cpp zc.c

[prompt@test ~]$ ./a.out

Hi

[prompt@test ~]$

关于c++ - 在 "C"代码中使用 "C++"函数和变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3650114/

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