简体   繁体   中英

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM