學 Linux C 的過程中,偶爾也會需要跟 Windows 上的連結庫打交道。這篇把 Visual C++ 6.0 建立靜態庫的步驟完整走一遍, conceptually 與 Linux 的 .a 完全一致,只是工具與副檔名不同(.lib)。
#include "foo.h"
int add(int a, int b)
{
return a + b;
}
#ifndef _FOO_H #define _FOO_H int add(int a, int b); #endif
include guard 的寫法與 GCC 環境一模一樣,這也是跨平台 C 程式碼得以共用的基礎。
把兩邊放在一起看,概念是相通的:ar rcs libfoobar.a 對應到 VC 的 Win32 Static Library 專案;.lib 之於 Windows,等同 .a 之於 Unix。差別在於 Windows 端通常由 IDE 代為呼叫 librarian,而 Linux 端你得自己下指令。理解任一邊,另一邊只是換一套工具名稱而已。