在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 -tr
drwx------+  3 git  staff    102 Sep  2 01:36 Pictures
drwx------+  3 git  staff    102 Sep  2 01:36 Music
drwx------+  3 git  staff    102 Sep  2 01:36 Movies
drwx------+ 26 git  staff    884 Sep  2 01:36 Library
drwx------+  4 git  staff    136 Sep  2 01:36 Downloads
drwx------+  3 git  staff    102 Sep  2 01:36 Documents
drwx------+  3 git  staff    102 Sep  2 01:36 Desktop
drwxr-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 __cplusplus
extern "C" {
#endif
  void hello();
#ifdef __cplusplus
}
#endif

int main(){
    hello();
    return 0;
}  

 

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

 

franklinMacmini:~ git$ g++ -L/Users/git test.cpp -o test -lhello
franklinMacmini:~ git$ 
franklinMacmini:~ git$ 
franklinMacmini:~ git$ ll -tr
drwx------+  3 git  staff    102 Sep  2 01:36 Pictures
drwx------+  3 git  staff    102 Sep  2 01:36 Music
drwx------+  3 git  staff    102 Sep  2 01:36 Movies
drwx------+ 26 git  staff    884 Sep  2 01:36 Library
drwx------+  4 git  staff    136 Sep  2 01:36 Downloads
drwx------+  3 git  staff    102 Sep  2 01:36 Documents
drwx------+  3 git  staff    102 Sep  2 01:36 Desktop
drwxr-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 -tr
total 200drwx------+  3 git  staff    102 Sep  2 01:36 Pictures
drwx------+  3 git  staff    102 Sep  2 01:36 Music
drwx------+  3 git  staff    102 Sep  2 01:36 Movies
drwx------+ 26 git  staff    884 Sep  2 01:36 Library
drwx------+  4 git  staff    136 Sep  2 01:36 Downloads
drwx------+  3 git  staff    102 Sep  2 01:36 Documents
drwx------+  3 git  staff    102 Sep  2 01:36 Desktop
drwxr-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 __cplusplus
extern "C" {
#endif
    void hello();
#ifdef __cplusplus
}
#endif

#ifdef __cplusplus
extern "C"{
#endif
    void bonjour();
#ifdef __cplusplus
}
#endif


int main()
{
    hello();
    bonjour();
    return 0;
}

 

再次编译test:

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

 

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

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

 

Game Over ~

 

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。