简体   繁体   English

C:如何制作一个允许链接到外部库的生成文件?

[英]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. 假设libfann.o位于/ usr / local / lib中-如果实际上在其他地方,请更改-L路径。

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. -L允许您指定(多个)路径来搜索库,而-l列出库名称。 The lib is normally prefixed for you, as are the possible extensions such as .a or .so . lib通常带有前缀,可能带有.a.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. 您原始的makefile所做的是尝试将main.o作为命令运行 ,而不是应运行的gcc

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行

cfann : main.c
    gcc -c main.c -lfann

clean: 
    rm -rf *o cfann

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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