从 C 文件调用 C++ 函数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12615683/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 16:25:23  来源:igfitidea点击:

Calling C++ functions from C file

c++cmergecall

提问by user1702375

I am quite new to C and C++. But I have some C++ functions which I need to call them from C. I made an example of what I need to do

我对 C 和 C++ 很陌生。但是我有一些 C++ 函数,我需要从 C 中调用它们。我举了一个我需要做的例子



main.c:

主文件

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

int main(){   
    helloWorld();
    return 0;
}


example.h:

示例.h

 #ifndef HEADER_FILE
 #define HEADER_FILE

 #ifdef __cplusplus
     extern "C" {
 #endif
         void helloWorld();
 #ifdef __cplusplus
     }
 #endif

 #endif


example.cpp:

示例.cpp

#include <iostream.h>

void helloWorld(){
    printf("hello from CPP");
} 


It just doesn't work. I still receive the error of undefined reference to _helloWorldin my main.c. Where is the the problem?

它只是不起作用。我仍然收到未定义参考错误_helloWorld在我main.c。问题出在哪里?

回答by asveikau

Short answer:

简短的回答:

example.cppshould include example.h.

example.cpp应该包括example.h.

Longer answer:

更长的答案:

When you declare a function in C++, it has C++ linkage and calling conventions. (In practice the most important feature of this is name mangling- the process by which a C++ compiler alters the name of a symbol so that you can have functions with the same name that vary in parameter types.) extern "C"(present in your header file) is your way around it - it specifies that this is a C function, callable from C code, eg. not mangled.

当您在 C++ 中声明一个函数时,它具有 C++ 链接和调用约定。(在实践中,最重要的功能是名称修改- C++ 编译器更改符号名称的过程,以便您可以使用具有不同参数类型的相同名称的函数。) extern "C"(存在于您的头文件中)是你的方式 - 它指定这是一个 C 函数,可从 C 代码调用,例如。没有被破坏。

You have extern "C"in your header file, which is a good start, but your C++ file is not including it and does not have extern "C"in the declaration, so it doesn't know to compile it as a C function.

您有extern "C"头文件,这是一个好的开始,但是您的 C++ 文件不包含它,并且extern "C"在声明中也没有,因此它不知道将其编译为 C 函数。

回答by Emilio Garavaglia

the extern "C"tells C++ that the declared function has to use the C ABI (Application Binary interface), hence, whether the language is C or C++, your void HelloWorld()is always seen externally as it is C.

extern "C"讲述C ++的声明函数必须使用C ABI(应用程序二进制接口),因此,语言是C或C ++,你void HelloWorld()总是看到外面,因为它是C.

But you implemented it in the cpp file like it is a C++ one, C is not aware of.

但是你在 cpp 文件中实现了它,就像它是一个 C++ 一样,C 不知道。

You have to make the prototype of HelloWorldcoherent for both C and C++, so the cpp file should declare it as extern "C" void Helloworld() { /*your code here*/ }, or simply, #include "example.h" from example.cpp, so that, before implementing it, the compiler already knows it has to follow the C convention.

您必须HelloWorld为 C 和 C++制作一致的原型,因此 cpp 文件应将其声明为extern "C" void Helloworld() { /*your code here*/ },或简单地声明为example.cpp 中的 #include "example.h",以便在实现它之前,编译器已经知道它必须遵循 C 约定。