0

I can't link my program "set-manipulation" with the libraries it needs.

Here is the message:

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

Here is the content of -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

Here is the makefile which give the error (the makefile of set-manipulation program):

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

Can someone please tell me how to link my program with it's library?

too honest for this site
  • 12,050
  • 4
  • 30
  • 52
Jenia Ivanov
  • 2,485
  • 3
  • 41
  • 69

3 Answers3

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

The library must be named libname.a/.so

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

If you don't have named library libname.a this dont't link the lib.

libtest.a

An corect link syntax :

g++ (LINK) -ltest

You see ? -l test don't include lib. l - lib

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

Name of lib must be libset_theory.a

Ramy
  • 172
  • 1
  • 17
  • Okay thanks. I made this change: ` gcc $(LDFLAGS)/set_theory.a -lset_theory $(CFLAGS) -o "$@" main.o`. The error has change. I changed the question to reflected your advice. thanks a lot!! – Jenia Ivanov Nov 01 '15 at 19:15
  • 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 – Ramy Nov 01 '15 at 19:22
  • You don't need to set to the path -L set_theory.a , and rename set_theory to libset_theory.a – Ramy Nov 01 '15 at 19:23
  • thanks again man. Same message thought. I made the changes to the question to reflect this change. – Jenia Ivanov Nov 01 '15 at 19:34
  • Check again my comment, edited. Now i was more clear. – Ramy Nov 01 '15 at 19:41
  • cool. thank Ramy. That was exactly the problem. Nikita Kakuev said that you can specify the lib name using a colon. Who knows, maybe it'll be useful to you too ;) Thanks again for your kind help – Jenia Ivanov Nov 04 '15 at 00:13
4

The -l argument expects the filename of specified library to be in a specific format. Namely, -lset_theory tells the linker to look for a file named libset_theory.a (or libset_theory.so).

Notice that your libraries don't have this prefix, so you either have to rename them, or use the semicolon and specify a filename:

gcc -L/home/jenia/learn-c-the-hard-way/lib -l:set_theory.a ...
Nikita Kakuev
  • 1,096
  • 9
  • 13
  • thanks very very much. I love the option to use the colon to specify the lib name. Thanks so much. I wish SO had the option to make multiple answers as the good one!!! – Jenia Ivanov Nov 04 '15 at 00:11
1

I might be wrong here, but you don't seem to be including the libraries. I mean, you are icluding the paths, but not the libraries you whish to use.

Jonathan
  • 129
  • 4