C: How do I make a makefile that allows linking to an external library?
Like, I think I'm close... just not sure what I'm doing wrong,
cfann : main.o
main.o -l libfann
main.o : main.c
gcc -c main.c
clean:
rm -rf *o cfann
开发者_开发知识库
I get this error:
main.o -l libfann
make: main.o: No such file or directory
make: *** [cfann] Error 1
Change:
cfann : main.o
main.o -l libfann
to:
cfann : main.o
gcc main.o -lfann -L/usr/local/lib -o cfann
This assumes that libfann.o is in /usr/local/lib - change the -L path above if it's actually somewhere else.
You need to replace:
cfann : main.o
main.o -l libfann
with something like:
cfann : main.o
gcc -o cfann -L/path/to/libs main.o -lfann
-L
allows you to specify (multiple) paths to search for the libraries and -l
lists the library names. The lib
is normally prefixed for you, as are the possible extensions such as .a
or .so
.
What your original makefile is doing is trying to run main.o
as a command, rather than the gcc
that it should be running.
Try this
cfann : main.o
main.o : main.c
gcc -c main.c -lfann
clean:
rm -rf *o cfann
Or, maybe without the empty cfann line
cfann : main.c
gcc -c main.c -lfann
clean:
rm -rf *o cfann
精彩评论