[英]Using (ugly) C code in C++ with extern “C”
I face some problems with ugly written old C code. 我在编写难看的旧C代码时遇到了一些问题。 I want to use a C++ compiler and use parts of this old code.
我想使用C ++编译器并使用部分旧代码。
header.h 头文件
extern "C"{
int header();
}
header.c 头文件
#include "header.h"
int header(){
const int i=20;
int *ptr = &i;
}
This is compiling using gcc (4.9.3) with a warning (without the extern "C") but in g++ there is the error : "invalid conversion from 'const int*' to 'int*' [-fpermissive]" 这是使用带有警告的gcc(4.9.3)进行编译的(没有extern“ C”),但是在g ++中存在错误:“从'const int *'到'int *'的无效转换[-fpermissive]”
I am not directly interested in this code but want to know if there is a way of getting any working C code compiling with a C++ compiler? 我对这段代码不是直接感兴趣,但是想知道是否有办法用C ++编译器来编译任何有效的C代码? (this is just an example what is "working" in C but not in C++ like the real problem is compiling with gcc but not with g++)
(这只是一个示例,它在C中“起作用”,但在C ++中不起作用,因为真正的问题是使用gcc而不是g ++进行编译)
You have two alternatives: 您有两种选择:
extern "C"
only makes the C++ compiler to use C function names when it is calling the functions. extern "C"
仅使C ++编译器在调用函数时使用C函数名。 If you choose to compile those functions with C++ compiler, it does not make those functions to actually compile with C rules. 如果选择使用C ++编译器编译这些函数,则不会使这些函数实际使用C规则进行编译。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.