Makefile链接共享(动态)库

时间:2017-08-11 10:56:31

标签: c makefile shared-libraries

我在将一个项目中的共享(动态)库.so或.dylib与另一个项目链接时遇到问题。

我正在使用这样的Makefile:

#libraries 
#custom
COMPARERS_STATIC_LIB_PATH=../comparers/output/debug/lib/libcomparers.a
COMPARERS_LIB_DIR=../comparers/output/debug/lib/

$(SHARED_LIBRARY): assertion.o
    $(CC) $(CFLAGS) -L$(COMPARERS_LIB_DIR) -shared -o $(OUTPUTS_LIB_DIR)/$(SHARED_LIBRARY) $(OUTPUTS_DIR)/assertion.o -lcomparers

$(DYNAMIC_LIBRARY): assertion.o
    $(CC) $(CFLAGS) -L$(COMPARERS_LIB_DIR) -dynamiclib -o $(OUTPUTS_LIB_DIR)/$(DYNAMIC_LIBRARY) $(OUTPUTS_DIR)/assertion.o -lcomparers

目录和库已经到位,如下图所示。

enter image description here

正确链接静态库编译和链接:

$(TARGET): $(LIBRARY)
    $(CC) $(CFLAGS) -o $(OUTPUTS_BIN_DIR)/$(TARGET) src/main.c $(OUTPUTS_LIB_DIR)/$(LIBRARY) $(COMPARERS_STATIC_LIB_PATH)

我得到的错误:

gcc  -g -Wall -D__USE_FIXED_PROTOTYPES__ -std=c99   -I./include  -I../comparers/include -L../comparers/output/debug/lib/ -shared -o outputs/debug/lib/libunit_tests.so outputs/debug/assertion.o -lcomparers
ld: warning: directory not found for option '-L../comparers/output/debug/lib/'
ld: library not found for -lcomparers
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make: *** [libunit_tests.so] Error 1

1 个答案:

答案 0 :(得分:1)

你有一个错字,你的$(COMPARERS_LIB_DIR)包含:

../comparers/output/debug/lib/

但必须是:

../comparers/outputs/debug/lib/
                   ^

关于你的身材。