LINUX C/C++ C++中是如何使用C開發的API(o/a/so/dll)
LINUX C/C++ C++中是如何使用C開發的API(o/a/so/dll)
資料來源: https://mp.weixin.qq.com/s/NNJyZtQaLkbGSbhLkDZZIg
test.c
#include"test.h" void testCfun() { printf("I am c fun\n"); return; }
test.h
#include<stdio.h> extern "C"{ void testCfun(); }
main.cpp
#include"test.h" #include<iostream> using namespace std; int main(void) { /*调用C接口*/ cout<<"start to call c function"<<endl; testCfun(); cout<<"end to call c function"<<endl; return 0; }
編譯:
$ gcc -c test.c $ g++ -o main main.cpp test.o $ ./main start to call c function I am c fun end to call c function
——-
test.h
#include<stdio.h> #ifdef __cplusplus extern "C"{ #endif void testCfun(); #ifdef __cplusplus } #endif
main.c
#include"test.h" int main(void) { /*调用C接口*/ testCfun(); return 0; }