C语言 Makefile 包含头文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15440183/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Makefile include header
提问by para
I am new to Linux programming I tried to compile a simple test construction. But I'm getting an error when compiling. Adding inc.c as well (in the app: line) doesn't work. How should I include the file correct?
我是 Linux 编程的新手,我试图编译一个简单的测试结构。但是我在编译时遇到错误。添加 inc.c 以及(在 app: 行中)不起作用。我应该如何正确包含文件?
Makefile:
生成文件:
app: main.c inc.h
cc -o app main.c
Terminal:
终端:
make
cc -o app main.c
/tmp/ccGgdRNy.o: In function `main':
main.c:(.text+0x14): undefined reference to `test'
collect2: error: ld returned 1 exit status
make: *** [app] Error 1
main.c:
主文件:
#include <stdio.h>
#include "inc.h"
int main()
{
printf("Kijken of deze **** werkt:\n");
test();
getchar();
return 0;
}
inc.h
英寸
#ifndef INCLUDE_H
#define INCLUDE_H
void test();
#endif
inc.c
公司
#include <stdio.h>
void test()
{
printf("Blijkbaar wel!");
}
采纳答案by bitmask
You must link against the compilation unit inc.owhich you obtain by compiling inc.c.
您必须链接到inc.o通过编译获得的编译单元inc.c。
In general that means that you must supply all object files that contain functions that are used in main.c(transitively). You can compile these with implicit rules of make, no need to specify extra rules.
通常,这意味着您必须提供所有包含用于main.c(传递)中的函数的目标文件。您可以使用 的隐式规则编译这些make,无需指定额外的规则。
You could say:
你可以说:
app: main.c inc.o inc.h
cc -o app inc.o main.c
And makewill know on its own how to compile inc.ofrom inc.calthough it will nottake inc.hinto account when determining whether inc.omust be rebuilt. For that you wouldhave to specify your own rules.
并且make会自己知道如何编译inc.o,inc.c虽然它在确定是否必须重建时不会考虑。对于您将要指定自己的规则。inc.hinc.o
回答by Miguel Prz
you didn't compile the inc.c file
你没有编译 inc.c 文件
app: main.c inc.h
cc -o app main.c inc.c
回答by Arild
You need to compile inc.cas well. A suitable approach (better scalable to larger applications) would be to split the Makefile up into different targets. The idea is: one target for every object file, then one target for the final binary. For compiling the object files, use the -cargument.
你也需要编译inc.c。一个合适的方法(更好地扩展到更大的应用程序)是将 Makefile 分成不同的目标。这个想法是:每个目标文件都有一个目标,然后是最终二进制文件的一个目标。要编译目标文件,请使用-c参数。
app: main.o inc.o
cc -o app main.o inc.o
main.o: main.c inc.h
cc -c main.c
inc.o: inc.c inc.h
cc -c inc.c

