My Computer Mac, System Mavericks.
Write a C code:
#include <stdio.h>void hello () { printf ("Hello, this was from C Language ~\n");}
Compile a library file:
franklinmacmini:~ git$ gcc--shared-o libhello.so hello.c franklinmacmini:~ git$ LL-trdrwx------+ 3 git staff 102 SEP 2 01:36picturesdrwx------+ 3 git staff 102 SEP 2 01:36musicdrwx------+ 3 git staff 102 SEP 2 01:36moviesdrwx------+ + git staff 884 Sep 2 01:36librarydrwx------+ 4 git staff 136 SEP 2 01:36downloadsdrwx------+ 3 git staff 102 SEP 2 01:36documentsdrwx------+ 3 git staff 102 SEP 2 01:36DESKTOPDRWXR-xr-x+ 5 git staff Sep 2 01:36 Public-rw-r--r--1 git staff Sep 26 23:01hello.c-rwxr-xr-x 1 git staff 8392 Sep 23:08 libhello.so
Write a C + + code:
#include <iostream>#ifdef __cplusplusextern"C" {# endifvoid hello (); #ifdef __cplusplus}#endifint main () { Hello (); return 0 ;}
To compile, link to the previous dynamic library:
franklinmacmini:~ git$ g++-l/users/git test.cpp-o test-Lhellofranklinmacmini:~git$ Franklinmacmini:~git$ Franklinmacmini:~ git$ LL-trdrwx------+3GIT staff102Sep2 on: $picturesdrwx------+3GIT staff102Sep2 on: $musicdrwx------+3GIT staff102Sep2 on: $moviesdrwx------+ -GIT staff884Sep2 on: $librarydrwx------+4GIT staff136Sep2 on: $downloadsdrwx------+3GIT staff102Sep2 on: $documentsdrwx------+3GIT staff102Sep2 on: $DESKTOPDRWXR-xr-x+5GIT staff theSep2 on: $ Public-rw-r--r--1GIT staff theSep - at: onhello.c
-rwxr-xr-x 1 git staff 8392 Sep 23:06 libhello.so
-rw-r--r--1git Staff144Sep - at: -Test.cpp
-rwxr-xr-x1git Staff8472Sep - at: ,Test
Execute:
franklinmacmini:~ git$./ This was from C Language ~
Can play again, too boring:
#include <stdio.h>void bonjour () { printf ("bonjour, Petit Prince is also from C Language ~\n");}
Compile:
franklinmacmini:~ git$ gcc--shared-o libbonjour.so bonjour.c franklinmacmini:~git$ Franklinmacmini:~ git$ LL-Trtotal $drwx------+3GIT staff102Sep2 on: $picturesdrwx------+3GIT staff102Sep2 on: $musicdrwx------+3GIT staff102Sep2 on: $moviesdrwx------+ -GIT staff884Sep2 on: $librarydrwx------+4GIT staff136Sep2 on: $downloadsdrwx------+3GIT staff102Sep2 on: $documentsdrwx------+3GIT staff102Sep2 on: $DESKTOPDRWXR-xr-x+5GIT staff theSep2 on: $ Public-rw-r--r--1GIT staff theSep - at: onhello.c-rwxr-xr-x1GIT staff8392Sep - at: ,libhello.so-rw-r--r--1GIT staff AboutSep - at: -bonjour.c-rwxr-xr-x1GIT staff8400Sep - at: -libbonjour.so-rw-r--r--1GIT staff238Sep - at: toTest.cpp-rwxr-xr-x1GIT staff8528Sep - at: +Test
To modify the Test.cpp code:
#include <iostream>#ifdef __cplusplusextern "C" {#endif voidhello (); #ifdef __cplusplus}#endif#ifdef __cplusplusextern "C"{#endifvoidbonjour (); #ifdef __cplusplus}#endifintMain () {hello (); Bonjour ();return 0;}
Compile test again:
franklinmacmini:~ git$ Franklinmacmini:~ git$ g++-l/users/git test.cpp-o Test-lhello- Lbonjourfranklinmacmini:~ git$./ This is from C Languageto is from C Language ~Franklinmacmini:
In turn, C calls C + + library in a slightly more complex way, I don't swim ~
franklinmacmini:~git$ file Testtest:mach-O --bit executable X86_64franklinmacmini:~git$ file libbonjour.so libbonjour.so:Mach-O --bit dynamically linked shared library X86_64franklinmacmini:~git$ file libhello.so libhello.so:Mach-O --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 + 1344noundefs dyldlink twolevel Piefranklinmacmini:~ git$
Game over ~
In the QQ group, some people asked whether there is a C-language XML parsing, accidentally thought of this problem: C + + Call library, simple test: