2009年4月2日 星期四

[C++] 如何用 gcc 編出 library 讓其他人使用-- DLL

若要看懂別人寫的 Makefile 內容, 最簡單的學習路徑可能是自己寫一個來玩玩看. 我們的環境是 Cygwin (Linux-like environment for Windows).

Makefile 簡單規則你可以參考 Ref[2], 這份文件內容專注於如何建立 static library 與 share library.

 

 文章內容

        1. 如何建立 static link  的程式庫

       2. 如何 link static 程式庫

       3. 如何建立 dynamic link 的程式庫

       4. 如何 link 動態連結程式庫

 

開始

// 函式標頭檔  (fun.h)

int add(int a,int b);

// 我們的函式  (fun.c)

int add(int a,int b){
    return a+b;
}

// 使用 library 的範例  (main.c)

#include <stdio.h>
#include "fun.h"

void main(){
    printf("1+2=%d",add(1,2));
    getchar();
}

 

1. 如何產生 static library 呢? 兩步驟完成

      Step 1: 先編成 .o 檔  (注意: –c )

       指令:  gcc –mno-cygwin –c 副程式(.c)

        例如:

                 snap003

      (-mno-cygwin 指令說明請參考 [3])

         Step 2: 用 ar 指令組合成 static library. 
                                                                         (注意: 你的程式庫一定要加 lib名稱)

                     -r:  新增 fun.o 到 libfun.a 中

                      -c:  建立新的程式庫

                      -s:  將一個 object 檔的 index 寫入程式庫

        指令ar rcs lib程式庫名稱 a.o b.o c.o

        例如:

snap003

2. 如何 link 你的程式庫?

指令:  gcc –mno-cygwin –static 測試程式.c –L程式庫位置  -l程式庫名稱

            (注意: 程式庫名稱不用加 lib, gcc 會自動抓有 lib開頭的程式庫檔)

例如:

// ---------- main.c -----

#include <stdio.h>
#include "fun.h"

void main(){
    printf("1+2=%d",add(1,2));
}

snap003

 

3. 如何產生 share library 呢?  (在 Windows 稱為 DLL)

     Step 1: 使用 gcc 的 –fPIC 指令產生相對索引位址的程式碼

     例如:

snap003

(注意: 剛好我的平台 –fPIC 會被忽略, 不管  我們還是最好加上參數)

 

   Step 2: 建立 DLL 檔 (使用 share 指令, Wl 後面接的是 link 指令)

                    注意: 你的程式庫名稱必須是 lib 開始

   指令: gcc –shared –Wl –o lib程式庫.dll 一堆fun.o

   例如:

snap003

 

4. 如何 link 剛剛產生的程式庫?

     直接用 –l 即可連結 (不用加 static)

指令: gcc –mno-cygwin 測試主程式.c –L程式庫位置 -l程式庫

            (注意: 程式庫名稱不用加 lib, gcc 會自動抓有 lib開頭的程式庫檔)

     例如:

snap003

 

Enjoy.

by Jing

 

References

[1]  Creating a shared and static library with the gnu compiler [gcc], “http://www.adp-gmbh.ch/cpp/gcc/create_lib.html“.

[2] GNU make Makefile 最簡單說明, “http://mqjing.blogspot.com/2007/08/mingw.html, “ 2007, 8.

[3] 如何在 Cygwin 下編出 Windows 可以直接執行的 code, “http://mqjing.blogspot.com/2009/03/c-cygwin-windows-code.html, “ 2009, 3.

 

 

English

If you want to understand what’s going on of a project’s Makefile, a possible way is to write it by yourself.

For Makefile rules, you can refer a document in [2].  Here, we focus on how to make a static/share library under Cygwin (Linux-like environment for Windows).

 

Contents

        1. How to create a static linked library?

       2. How to link a static library?

       3. How to create a shared library?

       4. How to link a shared library?

    

Let’s do it.

// fun.h

int add(int a,int b);

// fun.c

int add(int a,int b){
    return a+b;
}

// main.c

#include <stdio.h>
#include "fun.h"

void main(){
    printf("1+2=%d",add(1,2));
    getchar();
}

 

1. How to create a static linked library? (Two steps)

     Step 1: at first, build object files (.o files)   (Note: –c )

      Command

gcc –mno-cygwin –c Your_Function_Files (.c)

       Ex:

                 snap003

      (-mno-cygwin: please refer the ducument [3])

         Step 2: using ar instruct to create your libarary archieve.  
                           (Note: Your library name must begin with “lib”)

                      -r:  increasing the fun.o to libfun.a

                      -c:  create a new library

                      -s:  write a object file index to libfun.a

        Command:

  ar rcs libYourLibraryName a.o b.o c.o

        Ex:

snap003

2. How to link a static library?

Command:

             gcc –mno-cygwin –static Main.c –LYourLibraryLocation  -lLibraryName

              (Note: The LibraryName without “lib” prefix string).

Ex:

// ---------- main.c -----

#include <stdio.h>
#include "fun.h"

void main(){
    printf("1+2=%d",add(1,2));
}

snap003

 

3. How to create a shared library?  (If your are in Windows, this is a DLL library)

     Step 1: you must use –fPIC argument to generate independent code

     Ex:

snap003

(Note: In my system, the –fPIC is ignored. However, we should add this argument to ensure everything is ok).

 

   Step 2: Next, create a DLL file.

You can use a simple argument, shared, to tell gcc generating a DLL library. I given an example shown as below where the command followed the Wl is the options used for linker.

(Note: your library name should be named begin with “lib”).

                   

   Comm: gcc –shared –Wl –o libYourLibraryName.dll fun.o

   Ex:

snap003

 

4. How to link a shared library?

     It is simple. Directly use –l  (no static).

 

     Comm: gcc –mno-cygwin Main.c –LLibraryLocation –lYourLibraryName

     (Note:  without prefix string “lib” in your library name to reference the library file)

     Ex:

snap003

 

Enjoy.

by Jing

 

References

[1]  Creating a shared and static library with the gnu compiler [gcc], “http://www.adp-gmbh.ch/cpp/gcc/create_lib.html“.

[2] GNU make Makefile 最簡單說明, “http://mqjing.blogspot.com/2007/08/mingw.html, “ 2007, 8.

[3] 如何在 Cygwin 下編出 Windows 可以直接執行的 code, “http://mqjing.blogspot.com/2009/03/c-cygwin-windows-code.html, “ 2009, 3.

4 則留言:

  1. 您好,引用您的文章,在此說聲感謝...
    http://october388.blogspot.com/2009/04/mingwdll.html

    回覆刪除
  2. 那請問MS VS 2008編出來的dll檔可以在
    cygwin中使用嗎

    回覆刪除
  3. Dear Nick,
    我想應該是不能呼叫 VS 編出來 DLL, 因為 Microsoft 編出來的 DLL 格式好像不一樣. 例如: Borland 的編譯器編出來的 DLL 就無法給 VS 使用. 除非動一些手腳.

    by Jing

    回覆刪除
  4. 請問一下
    如果我想要將兩個lib.a 變成一個lib.a

    使用ar有其他的參數可以作到嗎?

    回覆刪除