链接器无法找到库

时间:2015-11-01 18:59:31

标签: c gcc makefile linker

我无法关联我的程序" set-manipulation"与它需要的库。

以下是信息:

gcc -L/home/jenia/learn-c-the-hard-way/lib -lset_theory -g -Wall -I/home/jenia/learn-c-the-hard-way/lib/include -o "set-manipulation" main.o
/usr/bin/ld: cannot find -lset_theory
collect2: error: ld returned 1 exit status
Makefile:9: recipe for target 'set-manipulation' failed
make: *** [set-manipulation] Error 1

以下是-L/home/jenia/learn-c-the-hard-way/lib的内容:

  /home/jenia/learn-c-the-hard-way/lib:
  total used in directory 29 available 216513716
  drwxr-xr-x  3 jenia jenia 4096 Nov  1 12:47 .
  drwxr-xr-x  8 jenia jenia 4096 Oct 31 11:44 ..
  drwxr-xr-x  2 jenia jenia 4096 Nov  1 12:47 include
  -rwxr-xr-x  1 jenia jenia 6804 Nov  1 12:47 set_theory.a -- 50
  -rwxr-xr-x  1 jenia jenia 9664 Nov  1 12:47 set_theory.so -- 11

这是给出错误的makefile(set-manipulation程序的makefile):

PREFIX?=/home/jenia/learn-c-the-hard-way
CFLAGS=-g -Wall -I${PREFIX}/lib/include
LDFLAGS=-L${PREFIX}/lib
INSTALL_DIR=${PREFIX}/apps

all: set-manipulation

set-manipulation: main.o
    gcc $(LDFLAGS) -lset_theory $(CFLAGS) -o "$@" main.o


install: all
    install -d $(INSTALL_DIR)/set-manipulation
    install set-manipulation $(INSTALL_DIR)/set-manipulation

clean:
    rm -f *.o
    rm -f set-manipulation
    rm -rf *.dSYM

有人可以告诉我如何将我的程序与它的图书馆联系起来吗?

3 个答案:

答案 0 :(得分:2)

-L../../PATCH_to_library.a/set_theory.a

该库必须命名为libname.a / .so

示例:g++ Set.cpp -L../../libset_theory.a -lset_theory

如果您没有命名库libname.a,则不要链接lib。

libtest.a

核心链接语法:

g++ (LINK) -ltest
你知道吗? -l test不包含lib。 l - lib

g++ -g -Wall -L/lib_dir/xx Foo.o Test.o -lset_theory -o test

lib的名称必须为libset_theory.a

答案 1 :(得分:2)

-l参数要求指定库的文件名采用特定格式。即,-lset_theory告诉链接器查找名为libset_theory.a(或libset_theory.so)的文件。

请注意,您的库没有此前缀,因此您必须重命名它们,或使用分号并指定文件名:

gcc -L/home/jenia/learn-c-the-hard-way/lib -l:set_theory.a ...

答案 2 :(得分:1)

我可能在这里错了,但你好像不包括这些库。我的意思是,你是在追踪路径,而不是你想要使用的库。

相关问题