首页 > 代码库 > 在QQ群里有人提问有没有C语言的XML解析,偶然想到了这个问题:C++调用C库,简单试验:

在QQ群里有人提问有没有C语言的XML解析,偶然想到了这个问题:C++调用C库,简单试验:

我的电脑Mac,系统MaveRicks。

 

写一个C代码:

#include<stdio.h>void hello(){        printf("Hello, this is from C Language ~\n");}

 

编译一个库文件:

franklinMacmini:~ git$ gcc --shared -o libhello.so hello.c franklinMacmini:~ git$ ll -trdrwx------+  3 git  staff    102 Sep  2 01:36 Picturesdrwx------+  3 git  staff    102 Sep  2 01:36 Musicdrwx------+  3 git  staff    102 Sep  2 01:36 Moviesdrwx------+ 26 git  staff    884 Sep  2 01:36 Librarydrwx------+  4 git  staff    136 Sep  2 01:36 Downloadsdrwx------+  3 git  staff    102 Sep  2 01:36 Documentsdrwx------+  3 git  staff    102 Sep  2 01:36 Desktopdrwxr-xr-x+  5 git  staff    170 Sep  2 01:36 Public-rw-r--r--   1 git  staff     82 Sep 26 23:01 hello.c-rwxr-xr-x   1 git  staff   8392 Sep 26 23:08 libhello.so

 

写一个C++代码:

#include<iostream>#ifdef __cplusplusextern "C" {#endif  void hello();#ifdef __cplusplus}#endifint main(){    hello();    return 0;}  

 

编译一下,要链接前面的动态库:

 

franklinMacmini:~ git$ g++ -L/Users/git test.cpp -o test -lhellofranklinMacmini:~ git$ franklinMacmini:~ git$ franklinMacmini:~ git$ ll -trdrwx------+  3 git  staff    102 Sep  2 01:36 Picturesdrwx------+  3 git  staff    102 Sep  2 01:36 Musicdrwx------+  3 git  staff    102 Sep  2 01:36 Moviesdrwx------+ 26 git  staff    884 Sep  2 01:36 Librarydrwx------+  4 git  staff    136 Sep  2 01:36 Downloadsdrwx------+  3 git  staff    102 Sep  2 01:36 Documentsdrwx------+  3 git  staff    102 Sep  2 01:36 Desktopdrwxr-xr-x+  5 git  staff    170 Sep  2 01:36 Public-rw-r--r--   1 git  staff     82 Sep 26 23:01 hello.c
-rwxr-xr-x 1 git staff 8392 Sep 26 23:06 libhello.so
-rw-r--r-- 1 git staff 144 Sep 26 23:07 test.cpp
-rwxr-xr-x 1 git staff 8472 Sep 26 23:08 test

 

执行一下:

franklinMacmini:~ git$ ./test Hello, this is from C Language ~

 

可以再玩玩,太无聊:

#include<stdio.h>void bonjour(){        printf("Bonjour, petit prince is also from C Language ~\n");}

 

编译一下:

franklinMacmini:~ git$ gcc --shared -o libbonjour.so bonjour.c franklinMacmini:~ git$ franklinMacmini:~ git$ ll -trtotal 200drwx------+  3 git  staff    102 Sep  2 01:36 Picturesdrwx------+  3 git  staff    102 Sep  2 01:36 Musicdrwx------+  3 git  staff    102 Sep  2 01:36 Moviesdrwx------+ 26 git  staff    884 Sep  2 01:36 Librarydrwx------+  4 git  staff    136 Sep  2 01:36 Downloadsdrwx------+  3 git  staff    102 Sep  2 01:36 Documentsdrwx------+  3 git  staff    102 Sep  2 01:36 Desktopdrwxr-xr-x+  5 git  staff    170 Sep  2 01:36 Public-rw-r--r--   1 git  staff     82 Sep 26 23:01 hello.c-rwxr-xr-x   1 git  staff   8392 Sep 26 23:08 libhello.so-rw-r--r--   1 git  staff     99 Sep 26 23:30 bonjour.c-rwxr-xr-x   1 git  staff   8400 Sep 26 23:30 libbonjour.so-rw-r--r--   1 git  staff    238 Sep 26 23:31 test.cpp-rwxr-xr-x   1 git  staff   8528 Sep 26 23:32 test

 

修改test.cpp代码:

#include <iostream>#ifdef __cplusplusextern "C" {#endif    void hello();#ifdef __cplusplus}#endif#ifdef __cplusplusextern "C"{#endif    void bonjour();#ifdef __cplusplus}#endifint main(){    hello();    bonjour();    return 0;}

 

再次编译test:

franklinMacmini:~ git$ franklinMacmini:~ git$ g++ -L/Users/git test.cpp -o test -lhello -lbonjourfranklinMacmini:~ git$ ./test Hello, this is from C Language ~Bonjour, petit prince is also from C Language ~franklinMacmini:~ git$ 

 

反过来,C调用C++库的方式稍微复杂一点点,我就不班门弄斧了~

franklinMacmini:~ git$ file testtest: Mach-O 64-bit executable x86_64franklinMacmini:~ git$ file libbonjour.so libbonjour.so: Mach-O 64-bit dynamically linked shared library x86_64franklinMacmini:~ git$ file libhello.so libhello.so: Mach-O 64-bit dynamically linked shared library x86_64franklinMacmini:~ git$ franklinMacmini:~ git$ otool -hv testtest:Mach header      magic cputype cpusubtype  caps    filetype ncmds sizeofcmds      flagsMH_MAGIC_64  X86_64        ALL LIB64     EXECUTE    19       1344   NOUNDEFS DYLDLINK TWOLEVEL PIEfranklinMacmini:~ git$ 

 

Game Over ~

 

在QQ群里有人提问有没有C语言的XML解析,偶然想到了这个问题:C++调用C库,简单试验: