在QQ群里有人提问有没有C语言的XML解析,偶然想到了这个问题:C++调用C库,简单试验:
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了在QQ群里有人提问有没有C语言的XML解析,偶然想到了这个问题:C++调用C库,简单试验:,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3711字,纯文字阅读大概需要6分钟。
内容图文
我的电脑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" { #endifvoid hello(); #ifdef __cplusplus } #endifint main(){ hello(); return0; }
编译一下,要链接前面的动态库:
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 201:36 Pictures drwx------+ 3 git staff 102 Sep 201:36 Music drwx------+ 3 git staff 102 Sep 201:36 Movies drwx------+ 26 git staff 884 Sep 201:36 Library drwx------+ 4 git staff 136 Sep 201:36 Downloads drwx------+ 3 git staff 102 Sep 201:36 Documents drwx------+ 3 git staff 102 Sep 201:36 Desktop drwxr-xr-x+ 5 git staff 170 Sep 201:36 Public -rw-r--r-- 1 git staff 82 Sep 2623: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, thisisfrom 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 201:36 Pictures drwx------+ 3 git staff 102 Sep 201:36 Music drwx------+ 3 git staff 102 Sep 201:36 Movies drwx------+ 26 git staff 884 Sep 201:36 Library drwx------+ 4 git staff 136 Sep 201:36 Downloads drwx------+ 3 git staff 102 Sep 201:36 Documents drwx------+ 3 git staff 102 Sep 201:36 Desktop drwxr-xr-x+ 5 git staff 170 Sep 201:36 Public -rw-r--r-- 1 git staff 82 Sep 2623:01 hello.c -rwxr-xr-x 1 git staff 8392 Sep 2623:08 libhello.so -rw-r--r-- 1 git staff 99 Sep 2623:30 bonjour.c -rwxr-xr-x 1 git staff 8400 Sep 2623:30 libbonjour.so -rw-r--r-- 1 git staff 238 Sep 2623:31 test.cpp -rwxr-xr-x 1 git staff 8528 Sep 2623:32 test
修改test.cpp代码:
#include <iostream> #ifdef __cplusplus extern"C" { #endifvoid hello(); #ifdef __cplusplus } #endif #ifdef __cplusplus extern"C"{ #endif void bonjour(); #ifdef __cplusplus } #endifint main() { hello(); bonjour(); return0; }
再次编译test:
franklinMacmini:~ git$ franklinMacmini:~ git$ g++ -L/Users/git test.cpp -o test -lhello -lbonjour franklinMacmini:~ git$ ./test Hello, thisisfrom 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 191344 NOUNDEFS DYLDLINK TWOLEVEL PIE franklinMacmini:~ git$
Game Over ~
原文:http://www.cnblogs.com/andypeker/p/3995787.html
内容总结
以上是互联网集市为您收集整理的在QQ群里有人提问有没有C语言的XML解析,偶然想到了这个问题:C++调用C库,简单试验:全部内容,希望文章能够帮你解决在QQ群里有人提问有没有C语言的XML解析,偶然想到了这个问题:C++调用C库,简单试验:所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。