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

本文介绍如何在Mac系统上使用GCC和G++编译器创建C和C++动态链接库,并实现从C++代码调用C语言编写的函数。同时展示了如何进一步扩展项目,增加更多C语言功能并通过C++进行调用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

我的电脑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 200
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 -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 ~

 

转载于:https://www.cnblogs.com/andypeker/p/3995787.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值