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:
#ifndef HEADER_FILE
#define HEADER_FILE
#ifdef __cplusplus
extern "C" {
#endif
void helloWorld();
#ifdef __cplusplus
}
#endif
#endif
example.cpp:
#include <iostream.h>
void helloWorld(){
printf("hello from CPP");
}
It just doesn't work. I still receive the error of undefined reference to _helloWorld
in my main.c
. Where is the the problem?
它只是不起作用。我仍然在main.c中收到了对_helloWorld的未定义引用的错误。这个问题在哪里?
2 个解决方案
#1
13
Short answer:
example.cpp
should 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函数。
#2
2
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 HelloWorld
coherent 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(){/ *你的代码在这里* /},或简单地说,#include“example.h” “来自example.cpp,因此,在实现它之前,编译器已经知道它必须遵循C约定。
#1
13
Short answer:
example.cpp
should 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函数。
#2
2
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 HelloWorld
coherent 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(){/ *你的代码在这里* /},或简单地说,#include“example.h” “来自example.cpp,因此,在实现它之前,编译器已经知道它必须遵循C约定。