简体   繁体   中英

Makefile link shared (dynamic) libraries

I have a problem with linking shared (dynamic) libraries .so or .dylib from my one project with another project.

I am using such 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

The directories and libraries are in place as on the below picture.

在此处输入图片说明

Linking with static library compiles and links correctly:

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

The error I get:

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

You have a typo, your $(COMPARERS_LIB_DIR) contains:

../comparers/output/debug/lib/

but must be:

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

concerning to your figure.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM