I wrote a simple make file
test : main.cpp
g++ -Wall -o $@ $^ -lhello_ext -L. -I/usr/include/python2.7/ -lboost_regex -lpython2.7 -ggdb
libhello_ext.so : hello_ext.o
g++ -shared -o $@ $^ -Lboost-lib-path -I/usr/include/python2.7/ -lboost_regex -lpython2.7 -ggdb
hello_ext.o : hello_ext.cpp
g++ -c -Wall -Werror -fpic -o $@ $^ -I/usr/include/python2.7/ -lboost_regex -lpython2.7 -ggdb
.PHONY : clean
clean:
rm -rf test lib* *.o *.swf
But when I do a make I get an error
/usr/bin/ld: cannot find -lhello_ext
collect2: error: ld returned 1 exit status
make: *** [test] Error 1
Тhe problem in the first two rows and when I delete these commands from a make file and write the manual way to the terminal, it works fine. Тhat is, from the terminal I do
g++ -Wall -o $@ $^ -lhello_ext -L. -I/usr/include/python2.7/ -lboost_regex -lpython2.7 -ggdb
and everything works fine. What's the problem ?
When you execute your line directly, $@ $^
is probably empty (check it yourself using echo
). In that case, the argument -lhello_ext
is not interpreted as a linker command for a library to add, but as the output name for your object file. That's why it works on the command line.
As for why it doesn't work in the Makefile: my guess would be that your library hello_ext
is located in the current directory. For the linker to find it, you need to put your library path information -L.
before you add the library.