linuxでのダイナミックライブラリと静的ライブラリのコンパイル方法(例説明)


「一日二十四被蹴www.1024 it.net」ステーション記事は、特別な説明なしにオリジナル記事としてデフォルト化された.
正式な書面による授権がない場合は、転載しないでください.ありがとうございます
例のソースコード、ここではhello.h,hello.c,main.c 3つのファイルでデモを行います.
robin@ubuntu:~/workspace/1024it.net$ cat hello.h 
#ifndef __HELLO_H__
#define __HELLO_H__

void sayHello();
#endif // #ifndef __HELLO_H__

robin@ubuntu:~/workspace/1024it.net$ cat hello.c 
/***************************************************
* Author: Robin
* Mail: [email protected]
* Description:
***************************************************/

#include <stdio.h>

void sayHello(){
    printf("Hello!!!\t (www.1024it.net)
"); } robin@ubuntu:~/workspace/1024it.net$ cat main.c /*************************************************** * Author: Robin * Mail: [email protected] * Description: ***************************************************/ #include <stdio.h> #include <hello.h> int main(int argc, char** argv, char** envp){     sayHello();     return 0; }    

ダイナミックコンパイル:
robin@ubuntu:~/workspace/1024it.net$ ls     // 
hello.c  hello.h  main.c
robin@ubuntu:~/workspace/1024it.net$ gcc -shared -fPIC hello.c -o libhello.so   // 
robin@ubuntu:~/workspace/1024it.net$ ls     // ,libhello.so 
hello.c  hello.h  libhello.so  main.c
robin@ubuntu:~/workspace/1024it.net$ 


robin@ubuntu:~/workspace/1024it.net$ gcc main.c -o main -L./ -lhello -I./       // main , libhello.so
robin@ubuntu:~/workspace/1024it.net$ ls                                         // , 
hello.c  hello.h  libhello.so  main  main.c


robin@ubuntu:~/workspace/1024it.net$ ./main                   // main , Hello!!!     (www.1024it.net)
Hello!!!     (www.1024it.net)
robin@ubuntu:~/workspace/1024it.net$                        // , libhello.so,
                                                            // export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:./

静的コンパイルの全プロセス:
robin@ubuntu:~/workspace/1024it.net$ rm libhello.so main      // 
robin@ubuntu:~/workspace/1024it.net$ ls
hello.c  hello.h  main.c

robin@ubuntu:~/workspace/1024it.net$ gcc -c  hello.c            // obj 
robin@ubuntu:~/workspace/1024it.net$ ls
hello.c  hello.h  hello.o  main.c
robin@ubuntu:~/workspace/1024it.net$ ar cr libhello.a hello.o   //  libhello.a 
robin@ubuntu:~/workspace/1024it.net$ ls
hello.c  hello.h  hello.o  libhello.a  main.c
robin@ubuntu:~/workspace/1024it.net$ gcc main.c -L./ -lhello -I./ -o main        // main 
robin@ubuntu:~/workspace/1024it.net$ ls
hello.c  hello.h  hello.o  libhello.a  main.c main 
robin@ubuntu:~/workspace/1024it.net$ ./main                  // 
Hello!!!     (www.1024it.net)