makefile - 如何将存档文件与 C 代码链接?
问题描述
我正在尝试将静态库存档文件 (libx/libx.a) 与 C 代码链接。该库需要 2 个标志 (-lx -lpthread)。链接静态库后,我的最终目标是创建一个共享库。我有以下 Make 文件,
rsa-engine: rsa/rsa.c rsa/bignum.c rsa/aes.c rsa/x509parse.c rsa/pem.c
gcc -fPIC -o rsa/rsa.o -c rsa/rsa.c
gcc -fPIC -o rsa/bignum.o -c rsa/bignum.c
gcc -fPIC -o rsa/aes.o -c rsa/aes.c
gcc -fPIC -o rsa/x509parse.o -c rsa/x509parse.c
gcc -fPIC -o rsa/pem.o -c rsa/pem.c
gcc -fPIC rsa-engine.c libx/libx.a -L.libx/ -lx -lpthread -o rsa-engine.o
gcc -shared -o librsa_engine.so -lcrypto rsa-engine.o rsa/rsa.o rsa/bignum.o rsa/aes.o rsa/x509parse.o rsa/pem.o
clean:
rm -f *.o rsa/*.o *.so rsa-engine
使用 make 命令后,它会产生以下输出,
/usr/bin/ld: cannot find -lx
collect2: error: ld returned 1 exit status
Makefile:2: recipe for target 'rsa-engine' failed
make: *** [rsa-engine] Error 1
我在这里发现了类似的问题。但这并没有帮助。好像我不能使链接工作。对我做错了什么有帮助吗?
我想实现以下命令生成的相同结果,
CC := gcc
CFLAGS := -Wall -g -MD -O2 -I ../
LDFLAGS := -lx -lpthread
tests_files := hello
all: $(tests_files)
hello: hello.o ../libx/libx.a
$(CC) $(CFLAGS) -static $(<) -L../libx/ $(LDFLAGS) -o $(@)
解决方案
您似乎libx.a
位于libx
, 但-L
引用.libx
目录中。无论如何,由于您libx/libx.a
直接引用,您可以跳过两者-L.libx/ -lx
,它应该链接得很好。