Makefile包含标头 [英] Makefile include header

查看:68
本文介绍了Makefile包含标头的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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?

Makefile:

app: main.c inc.h
    cc -o app main.c

端子:

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!");
}

推荐答案

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

You must link against the compilation unit inc.o which you obtain by compiling inc.c.

通常,这意味着您必须提供(透明地)包含在main.c中使用的功能的所有目标文件.您可以使用make的隐式规则进行编译,而无需指定其他规则.

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.

您可以说:

app: main.c inc.o inc.h
    cc -o app inc.o main.c

make会自行知道如何从inc.c编译inc.o,尽管在​​确定是否必须重建inc.o时会inc.h考虑在内.为此,您必须指定自己的规则.

And make will know on its own how to compile inc.o from inc.c although it will not take inc.h into account when determining whether inc.o must be rebuilt. For that you would have to specify your own rules.

这篇关于Makefile包含标头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆