【问题标题】:How do I link a archive file with C code?如何将存档文件与 C 代码链接?
【发布时间】:2020-11-12 21:38:27
【问题描述】:

我正在尝试将静态库存档文件 (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

我发现了类似的问题here。但这并没有帮助。好像我不能使链接工作。对我做错了什么有帮助吗?

我想实现以下命令生成的相同结果,

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 $(@)

【问题讨论】:

    标签: makefile gnu-make


    【解决方案1】:

    您的libx.a 似乎位于libx,但-L 引用.libx 目录。无论如何,既然你直接引用libx/libx.a,你可以跳过-L.libx/ -lx,它应该链接就好了。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-03-13
      • 2011-06-03
      • 2010-11-03
      • 2012-12-19
      • 1970-01-01
      • 2015-01-13
      • 1970-01-01
      相关资源
      最近更新 更多