C++调用C函数[待修改]
2010-09-19 17:54
239 查看
//test.cpp 文件 #include <iostream> #include <cstdio> //对应C中的 stdio.h [C++中调用C的库函数,有对应C++版本如,cstring] #include <cstring> //对应C中的 string.h #include "./cfun_add.h"//调用C写的函数 using namespace std; int main (void) { cout<<"Hello C++"<<endl; printf ("Hello c function.num=%d/n", 123); int ret = 0; char str1[] = "123"; char str2[] = "23"; ret = strcmp (str1, str2); if (!ret) { printf ("%s and %s are the same.", str1, str2); } else { cout<<"不一样。"<<endl; } cout<<"3+4="<<add_c(3, 4)<<endl;//[C++中调用C写的函数] return 0; } //cfun_add.h 文件 int add_c (int a, int b); // cfun_add.c 文件 #include <string.h> #include <stdio.h> int add_c(int a, int b) { puts("add_c is called./n"); return a+b; } 编译: # g++ -o test cfun_add.c test.cpp 成功生成 test, # ./test //执行 使用库方式: 静态库: # gcc -c cfun_add.c //生成了 cfun_add.o # ar -rcs libcfun_add.a cfun_add.o //使用归档命令生成静态库cfun_add.a //使用静态库 在 test.cpp中一定要有如内容, extern "C"{ #include "cfun_add.h"//调用C库中的函数 } # g++ -o test t.cpp -L. -lcfun_add //生成test -L.表示库文件libfun_add.a在当前目录中,-lcfun_add 其他:略。 //表示引用静态库文件libfun_add.a
相关文章推荐
- C++调用C函数
- 【修改版本】Python调用C函数,cffi (附具体例子)
- 如何在C中调用C++函数 与 如何在C++中调用C的函数(待修改)
- C++ 调用C函数
- extern "C"与C++中的C函数调用(4)—— 如何在C中调用C++函数
- C++与C函数的相互调用方法以及error C2732:链接规范与"f"的早期规范冲突的解决办法
- lua与c++的相互调用、C/C++修改Lua中Table的内容
- 如果C++程序要调用已经被编译后的C函数,需要extern “C”
- c++调用c函数
- Android NDK 调用c++ stl 模板库(修改android.mk文件)
- c++程序调用c函数 and otherwise zz
- 如果C++程序要调用已经被编译后的C函数,该怎么办?
- extern "C"与C++中的C函数调用(1)
- 如何在C++中调用C函数
- C++调用C函数
- 关于C++调用C函数或变量的问题
- 如果C++程序要调用已经被编译后的C函数,该怎么办?
- C++调用C函数
- 用java通过调用C++的DLL文件来修改系统分辨率(一)
- C++调用C函数