使用extern "C" 主要是因为C编译器编译函数时不带参数的类型信息,只包含函数的符号名字。
如int foo( float x ),C编译器会将此函数编译成类似_foo的符号,C连接器只要找到了调用函数的符号,就认为连接成功。 而C++编译器为了实现函数重载,会在编译时带上函数的参数信息。如它可以把上面的函数编译成类似于_foo_float这样的符号。 所以,C调用C++,使用extern "C"则是告诉编译器依照C的方式来编译封装接口,当然接口函数里面的C++语法还是按C++方式编译。 如: // C++ Code extern "C" int foo( int x ); int foo( int x ) { //... } 这样,编译器会将foo函数编译成类似_foo符号,而不会编译成类似_foo_int符号; 则C可以这样调用C++函数 // C Code int foo( int x ); void cc( int x ) { foo( x ); //... }
如果想调用重载的C++函数,则须封装单独的接口共C调用。 如 // C++ Code void foo( int x ); void foo( float x ); extern "C" void foo_i( int x ) { foo( x ); } extern "C" void foo_f( float x ) { foo( x ); } 则C中可这样调用 // C Code void foo_i( int x ); void foo_f( float x ); void ccc( int x1, float x2 ) { foo_i( x1 ); foo_f( x2 ); // ... }
而C++调用C编译好的模块,如DLL或.o,这时C++会用C++的方式解释函数名字,但C编译后的名字是C规则的。
在C++中,在被调用的C函数的声明前加上extern "C" 的作用是:让C++连接器在调用函数的符号时采用C的方式 如: // C Code void foo( int x ); C++这样调用C函数 // C++ Code extern "C" void foo( int x ); 就是让C++连接器能过类似于_foo来查找此函数,而非类似于_foo_int这样的符号。