Makefile包含头文件

par*_*ara 16 c makefile

我是Linux编程新手我试图编译一个简单的测试结构.但是我在编译时遇到错误.添加inc.c(在app:行中)也不起作用.我该如何包含正确的文件?

Makefile文件:

app: main.c inc.h
    cc -o app main.c
Run Code Online (Sandbox Code Playgroud)

终奌站:

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
Run Code Online (Sandbox Code Playgroud)

main.c中:

#include <stdio.h>
#include "inc.h"

int main()
{
    printf("Kijken of deze **** werkt:\n");
    test();

    getchar();
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

英寸

#ifndef INCLUDE_H

#define INCLUDE_H

void test();

#endif
Run Code Online (Sandbox Code Playgroud)

inc.c

#include <stdio.h>

void test()
{
    printf("Blijkbaar wel!");
}
Run Code Online (Sandbox Code Playgroud)

bit*_*ask 12

您必须链接到inc.o通过编译获得的编译单元inc.c.

通常,这意味着您必须提供包含main.c(传递)中使用的函数的所有目标文件.您可以使用隐式规则编译这些规则make,无需指定额外规则.

你可以说:

app: main.c inc.o inc.h
    cc -o app inc.o main.c
Run Code Online (Sandbox Code Playgroud)

并且make将自己知道如何编译inc.o,inc.c尽管在确定是否必须重建时不会考虑.对于您要指定自己的规则.inc.hinc.o