OiciTrap
OiciTrap

Reputation: 169

A Makefile linker error

I have a problem that is causing me a headache, it is assumed that the command-L searches the libraries in the current directory of the Makefile, but it's not working for me, for example I have the following command in my Makefile:

...
LDLIBS  = -L/libs -lmatrix

main: main.o operations.o display.o
    $(CC) $(LDLIBS) $^ -o $@
...

And when I try to compile it literally says:

gcc  -L/libs -lmatrix main.o operations.o display.o -o main
/usr/bin/ld: cannot find -lmatrix
collect2: ld returned 1 exit status
make: *** [main] Error 1

But when I simply change "-L/libs" for "-L$(PWD)/libs" it compiles perfectly and my program works fine...

But using "-L$(PWD)" I get another problem, if the name of any directory has a space It doesn't work again, actually I don't know if this problem is irremediable (using $(PWD) or not), but I still have the doubt that why it doesn't work without the $(PWD) because apparently (seeing A LOT of examples on the internet) using -L, the $(PWD) shouldn't be needed.

Upvotes: 3

Views: 294

Answers (2)

dreamer
dreamer

Reputation: 91

The path /PATHNAME means the directory at the root of your filesystem. If you want to use $(PWD) for your current directory then you can quote the shell variables like: "$(PWD)". Do not escape the quotes.

Upvotes: 1

MadScientist
MadScientist

Reputation: 100781

You're wrong that it's not needed. The path /libs means the libs directory at the root of your filesystem. Just like /bin doesn't mean "the bin directory under my current directory", so /libs doesn't mean the libs directory under the current directory.

If you want to look in libs in the current directory, just use -Llibs or if you want to be more specific, -L./libs (the directory . is the current directory, so cd . changes to the current directory, just like cd .. changes to the parent directory).

Upvotes: 6

Related Questions