C语言为什么无法include另一个源代码文件
本文介绍了C语言为什么无法include另一个源代码文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
问 题
第一个程序try1
#include<stdio.h>
#include"try2.c"
int main()
{
int i;
scanf("%d",&i);
printf("%d\n",jc(i));
}
第二个程序 try2
#include<stdio.h>
int jc(int a)
{
int b=a;
while(a-->1)
{
b*=a;
}
return b;
}
报错:
||=== Build file: "no target" in "no project" (compiler: unknown) ===|
F:Ctry1.o:try1.c|| undefined reference to `jc'|
||error: ld returned 1 exit status|
||=== Build failed: 2 error(s), 0 warning(s) (0 minute(s), 0 second(s)) ===|
想试一下一起编译两个源文件,为什么不成功?
解决方案
这我以前也想过,首先你看下这个:http://blog.csdn.net/laojiu_/...
然后再回到你的问题,为何不可以在一个a.c
里再包含一个b.c
文件呢?我们知道include的作用简单来说就是把该文件内容在include位置全数展开,此时若可以进入.obj链接过程,链接器就会发现有两份b.c
文件,一份是b.c
自己的,另一份是已包含在a.c
里的,这不就是重定义了么。
所以,在一个a.c
里再包含一个b.c
文件这种做法是错误的,应该包含.h文件。
这篇关于C语言为什么无法include另一个源代码文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文