3

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.

2 Answers 2

6

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).

Sign up to request clarification or add additional context in comments.

1 Comment

it's also a good idea to shift all the linker-related options to the end of the command line, after the name of your output, in this case after -o main .
1

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.