C++中调用C函数
2009-11-10 10:56
267 查看
下面是从《GCC:the Complete Reference》中摘出来的。 Calling C from C++ The following example is a C++ program that calls a C function named csayhello(). This call can be made directly because the function is declared in the C++ program as extern "C": /* cpp2c.cpp */ #include <iostream> extern "C" void csayhello(char *str); int main(int argc,char *argv[]) { csayhello("Hello from cpp to c"); return(0); } The C function requires no special declaration and appears as follows: /* csayhello.c */ #include <stdio.h> void csayhello(char *str) { printf("%s\n",str); } The following three commands compile the two programs and link them into an executable. The flexibility of g++ and gcc allow this to be done in different ways, but this set of commands is probably the most straightforward: $ g++ -c cpp2c.cpp -o cpp2c.o $ gcc -c csayhello.c -o csayhello.o $ gcc cpp2c.o csayhello.o -lstdc++ -o cpp2c Notice that it is necessary to specify the standard C++ library in the final link because the gcc command is used to invoke the linker instead of the g++ command. If g++ had been used, the C++ library would have been implied. It is most common to have the function declarations in a header file and to have the entire contents of the header file included as the extern "C" declaration. The syntax for this is standard C++ and looks like the following: extern "C" { int mlimitav(int lowend, int highend); void updatedesc(char *newdesc); double getpct(char *name); }; |
|
相关文章推荐
- C++与C函数的相互调用方法以及error C2732:链接规范与"f"的早期规范冲突的解决办法
- 如何在C++中调用C函数
- extern "C"与C++中的C函数调用(4)—— 如何在C中调用C++函数
- c++程序调用c函数
- C++调用C函数,为什么要加extern "C"?
- C函数的dll,C++静态调用以及C++函数的C++静态调用。
- 如果C++程序要调用已经被编译后的C函数,该怎么办?
- C函数和C++函数相互调用
- 关于C++调用C函数或变量的问题
- 中断中C函数调用C++
- C++程序调用C函数
- c++调用c函数 以及c调用c++函数
- 如果C++程序要调用已经被编译后的C函数,需要extern “C”
- C++调用C函数
- 在Anjuta 的C++项目中调用自定义C函数:undefined reference xxxxxx
- C++程序调用已经被编译后的C函数
- c++调用c函数
- C++程序调用C函数
- c++ 调用c函数
- 从C++中调用C函数报错 undefined reference to