ライブラリーファイルの作成には ar コマンドを用いる。
ライブラリーファイル名は頭3文字が lib 拡張子が .a である必要がある。
以下を実行すると libMylib.a というファイルが出来る。
func1.h
int func1(int a,int b); |
func1.cpp
int func1(int a,int b)
{ return (a+b); } |
Makefile
DEBUG=1 OBJS = func1.o # リンカーオプション LDFLAGS = TARGET = Mylib.a CXXFLAGS = -c -Wall -fmessage-length=0 ifeq ($(DEBUG),1) CXXFLAGS += -O0 -g -D_DEBUG else CXXFLAGS += -Os endif $(TARGET): $(OBJS) ar rv lib$(TARGET) $(OBJS) all: $(TARGET) clean: del *.bak del *.o del *.exe |
次にライブラリーファイルを使用する準備をする。
func1.h を c:\mylib\include に置き、 libMylib.a を c:\mylib\lib に置く。
test.cpp
#include |
DEBUG=1 OBJS = test.o # ライブラリーの指定 LIBS = -Lc:\mylib\lib -lMylib # リンカーオプション LDFLAGS = TARGET = test.exe CXXFLAGS = -c -Wall -fmessage-length=0 CXXFLAGS += -Ic:\mylib\include ifeq ($(DEBUG),1) CXXFLAGS += -O0 -g -D_DEBUG else CXXFLAGS += -Os endif $(TARGET): $(OBJS) $(CXX) -o $(TARGET) $(OBJS) $(LIBS) all: $(TARGET) clean: del *.bak del *.o del *.exe |
主な変更点
コンパイラオプション
リンカーオプション