包括c文件包含头文件

时间:2014-05-19 20:18:16

标签: c include

我的工作区中有3个文件:

1.H

#ifndef SOME_HEADER_FILE_H
#define SOME_HEADER_FILE_H
//code
#endif

1.C

#include <stdio.h>
#include "1.h"
//code

的main.c

#include "1.h"
int main(){
    printf("hello");
}

但是在main.c中,printf是未识别的,有没有办法让它在1.c中调用相关的标题时工作? 如何链接1.c和1.h文件?

编辑:这是一项学术任务,我不允许在主要和标题中进行更改。

2 个答案:

答案 0 :(得分:1)

您仅在#include <stdio.h>中包含1.c,在1.hmain.c中包含而非。 明显的解决方案是将其包含在main.c

答案 1 :(得分:0)

由于#include宏的工作方式(它扩展了您在调用宏的行中包含的整个头文件),实际上您不需要在stdio.h中包含main.c {1}}只要stdio.h包含在main.c包含的头文件中,就会{1}}。

希望这说清楚:

的main.c

#include "test.h"

int main()
{
    printf("I can call this function thanks to test.h!\n");
    return 0;
}

test.h

#include <stdio.h>

这样可以正常使用。

但这是与能够使用.c文件基于其自己的#include定义可以访问的函数相同,因为您是交叉编译的他们。在这种情况下,调用other.c的{​​{1}}文件将获得#include <stdio.h>,但printf()则不会。{/ p>

在此设置中,

的main.c

main.c

test.c的

int main()
{
    printf("I don't have any way to call this...\n");
    return 0;
}

即使你对两者进行交叉编译,你也无法让main知道printf()是什么。 test.c知道什么是printf()但不是main。

您想要的是#include <stdio.h> 中的#include <stdio.h>other.h中的#include "other.h"

但是为了将来参考,这可能是不好的做法,因为它应该立即显示每个文件的要求是什么,以便你很好地了解它的工作是什么。

所以这就是我可能建议的最佳解决方案:

的main.c

main.c
相关问题