如何在c++中使用C代码

时间:2022-08-03 15:05:14

Just a small question: Can C++ use C header files in a program?

一个小问题:c++能在程序中使用C头文件吗?

This might be a weird question, basically I need to use the source code from other program (made in C language) in a C++ one. Is there any difference between both header files in general? Maybe if I change some libraries... I hope you can help me.

这可能是一个奇怪的问题,基本上我需要在c++中使用其他程序(用C语言编写)的源代码。这两个头文件通常有什么不同吗?如果我换一些图书馆……我希望你能帮助我。

3 个解决方案

#1


31  

Yes, you can include C headers in C++ code. It's normal to add this:

是的,您可以在c++代码中包含C标头。这是很正常的:

#ifdef __cplusplus
extern "C"
{
#endif

// C header here

#ifdef __cplusplus
}
#endif

so that the C++ compiler knows that function declarations etc. should be treated as C and not C++.

这样c++编译器就知道函数声明等应该被当作C而不是c++。

#2


12  

If you are compiling the C code together, as part of your project, with your C++ code, you should just need to include the header files as per usual, and use the C++ compiler mode to compile the code - however, some C code won't compile "cleanly" with a C++ compiler (e.g. use of malloc will need casting).

如果你编译C代码结合在一起,作为项目的一部分,你的c++代码,你应该只需要包含头文件按照往常一样,并使用c++编译器编译代码模式——然而,一些C代码不会编译“干净”c++编译器(如使用malloc需要铸造)。

If on, the other hand, you have a library or some other code that isn't part of your project, then you do need to make sure the headers are marked as extern "C", otherwise C++ naming convention for the compiled names of functions will apply, which won't match the naming convention used by the C compiler.

如果,另一方面,你的图书馆或其他代码不是项目的一部分,那么您需要确保标题标记为外来的“C”,否则c++编译函数的名字命名约定将适用,它不会匹配使用的命名约定的C编译器。

There are two options here, either you edit the header file itself, adding

这里有两个选项,要么编辑头文件本身,添加

#ifdef __cplusplus 
extern "C" {
#endif

... original content of headerfile goes here. 

#ifdef __cplusplus 
}
#endif

Or, if you haven't got the possibility to edit those headers, you can use this form:

或者,如果你没有编辑这些标题的可能性,你可以使用以下形式:

#ifdef __cplusplus 
extern "C" {
#endif
#include <c_header.h>"
#ifdef __cplusplus 
}
#endif

#3


7  

Yes, but you need to tell the C++ compiler that the declarations from the header are C:

是的,但是您需要告诉c++编译器来自header的声明是C:

extern "C" {
#include "c-header.h"
}

Many C headers have these included already, wrapped in #if defined __cplusplus. That is arguably a bit weird (C++ syntax in a C header) but it's often done for convenience.

许多C标头已经包含了这些内容,如果定义为__cplusplus,则包装为#。这可能有点奇怪(C头中的c++语法),但这通常是为了方便。

#1


31  

Yes, you can include C headers in C++ code. It's normal to add this:

是的,您可以在c++代码中包含C标头。这是很正常的:

#ifdef __cplusplus
extern "C"
{
#endif

// C header here

#ifdef __cplusplus
}
#endif

so that the C++ compiler knows that function declarations etc. should be treated as C and not C++.

这样c++编译器就知道函数声明等应该被当作C而不是c++。

#2


12  

If you are compiling the C code together, as part of your project, with your C++ code, you should just need to include the header files as per usual, and use the C++ compiler mode to compile the code - however, some C code won't compile "cleanly" with a C++ compiler (e.g. use of malloc will need casting).

如果你编译C代码结合在一起,作为项目的一部分,你的c++代码,你应该只需要包含头文件按照往常一样,并使用c++编译器编译代码模式——然而,一些C代码不会编译“干净”c++编译器(如使用malloc需要铸造)。

If on, the other hand, you have a library or some other code that isn't part of your project, then you do need to make sure the headers are marked as extern "C", otherwise C++ naming convention for the compiled names of functions will apply, which won't match the naming convention used by the C compiler.

如果,另一方面,你的图书馆或其他代码不是项目的一部分,那么您需要确保标题标记为外来的“C”,否则c++编译函数的名字命名约定将适用,它不会匹配使用的命名约定的C编译器。

There are two options here, either you edit the header file itself, adding

这里有两个选项,要么编辑头文件本身,添加

#ifdef __cplusplus 
extern "C" {
#endif

... original content of headerfile goes here. 

#ifdef __cplusplus 
}
#endif

Or, if you haven't got the possibility to edit those headers, you can use this form:

或者,如果你没有编辑这些标题的可能性,你可以使用以下形式:

#ifdef __cplusplus 
extern "C" {
#endif
#include <c_header.h>"
#ifdef __cplusplus 
}
#endif

#3


7  

Yes, but you need to tell the C++ compiler that the declarations from the header are C:

是的,但是您需要告诉c++编译器来自header的声明是C:

extern "C" {
#include "c-header.h"
}

Many C headers have these included already, wrapped in #if defined __cplusplus. That is arguably a bit weird (C++ syntax in a C header) but it's often done for convenience.

许多C标头已经包含了这些内容,如果定义为__cplusplus,则包装为#。这可能有点奇怪(C头中的c++语法),但这通常是为了方便。