用gcc 来链接编译三个程序 求指教

YI一一一YI 2017-10-23 10:47:09
这是第一个程序 文件名为main.c
#include <stdio.h>
#include <increment.c>
#include <negate.c>

int main( void )
{
printf( "%d %d\n", increment( 10 ), negate( 10 ) );
printf( "%d %d\n", increment( 0 ), negate( 0 ) );
printf( "%d %d\n", increment( –10 ), negate( –10 ) );
}
这是第二个程序 文件名为increment.c
int
increment( int value )
{
return value + 1;
}
这是第三个程序 文件名为 negate.c
int
negate( int value )
{
return –value;
}

当我用 gcc main.c increment.c negate.c
zhangfeifei@zhangfeifei-ThinkPad-Edge-E531:~/fei$ gcc main.c increment.c negate.c
In file included from main.c:2:0:
increment.c:2:23: fatal error: increment.c: 没有那个文件或目录
compilation terminated.
increment.c:2:23: fatal error: increment.c: 没有那个文件或目录
compilation terminated.
negate.c:2:23: fatal error: increment.c: 没有那个文件或目录
compilation terminated.

我就是在这个目录下编译的啊 求指教
...全文
287 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
YI一一一YI 2017-10-24
  • 打赏
  • 举报
回复
这是main,c //#include <iostream.h> #include <stdio.h> //#include "increment.c" //#include "negate.c" extern int increment( int value ); extern int negate( int value ); int main( void ) { printf( "%d %d\n", increment( 10 ), negate( 10 ) ); printf( "%d %d\n", increment( 0 ), negate( 0 ) ); } 这是increment.c int increment( int value ) { return value + 1; } 这是negate.c int negate( int value ) { return -value; } 按你的操作: zhangfeifei@zhangfeifei-ThinkPad-Edge-E531:~$ gcc -Wall -g increment.c netgate.c main.c -o app gcc: error: increment.c: 没有那个文件或目录 gcc: error: netgate.c: 没有那个文件或目录 gcc: error: main.c: 没有那个文件或目录 gcc: fatal error: no input files compilation terminated. 我把#include <iostream.h>的注释拿掉 #include <iostream.h> #include <stdio.h> //#include "increment.c" //#include "negate.c" extern int increment( int value ); extern int negate( int value ); int main( void ) { printf( "%d %d\n", increment( 10 ), negate( 10 ) ); printf( "%d %d\n", increment( 0 ), negate( 0 ) ); } 在执行 zhangfeifei@zhangfeifei-ThinkPad-Edge-E531:~$ gcc -Wall -g increment.c netgate.c main.c -o app gcc: error: increment.c: 没有那个文件或目录 gcc: error: netgate.c: 没有那个文件或目录 gcc: error: main.c: 没有那个文件或目录 gcc: fatal error: no input files compilation terminated. 求指教
自信男孩 2017-10-24
  • 打赏
  • 举报
回复
在main.c文件里先声明两个函数;
main.c
#include <stdio.h>
//声明函数
extern int increment( int value );
extern int negate( int value );
int main( void )
{
printf( "%d %d\n", increment( 10 ), negate( 10 ) );
printf( "%d %d\n", increment( 0 ), negate( 0 ) );
printf( "%d %d\n", increment( –10 ), negate( –10 ) );
}
文件名为increment.c
int
increment( int value )
{
return value + 1;
}
文件名为 negate.c
int
negate( int value )
{
return –value;
}
编译时试一下如下的编译命令:
gcc -Wall -g increment.c netgate.c main.c -o app 
bigPillow 2017-10-24
  • 打赏
  • 举报
回复
#include <increment.c> ---》 #include “increment.c”
YI一一一YI 2017-10-24
  • 打赏
  • 举报
回复
我想从简单的先体验一波 求指教
YI一一一YI 2017-10-24
  • 打赏
  • 举报
回复
失败原因: 1,#include <increment.c> #include <negate.c> include一般是用来引入头文件的,很少或者基本上没见过引入源文件的。 对于自定义的头文件也不能使用<>引入,因为加上<>是告诉编译器我引入的是系统头文件或者C库的头文件; 自定义的头文件应该用""引号,编译器会明白是用户自定义的头文件,如果没找到,则编译器会从系统和库里寻找; 2,#include <iostream.h> 加了一句这个头文件,标准的输入输出流头文件。用来声明一些用来在标准输入输出设备上进行输入输出操作的对象 与这个没关系 3,main.c #include <stdio.h> //#include "increment.c" //#include "negate.c" extern int increment(); extern int negate(); int main( void ) { printf( "%d %d\n", increment( 10 ), negate( 10 ) ); printf( "%d %d\n", increment( 0 ), negate( 0 ) ); } 为什么这样能成功 原因: 1,首先想法是 想法1:直接在main函数中写上另外的两个函数; #include <stdio.h> int main( void ) { printf( "%d %d\n", increment( 10 ), negate( 10 ) ); printf( "%d %d\n", increment( 0 ), negate( 0 ) ); } 失败:这个做法肯定不行,因为main函数根本不知道另两个函数的存在,怎么调用呢?这个在标准C编译器里面会报错的,但是在Xcode中只是个警告。 想法2:在main.c中包含两个函数的文件 大家都知道#include的作用纯粹就是内容拷贝,所以又相当于 直接把这两个函数复制到main函数前面了: #include <stdio.h> int increment( int value ) { return value + 1; } int main( void ) { printf( "%d %d\n", increment( 10 ), negate( 10 ) ); printf( "%d %d\n", increment( 0 ), negate( 0 ) ); } 重点来啦:这就是重点原因:哎,这么一看好像是对的哦,在main函数前面定义了increment和negate函数,然后在main函数中调用了这两个函数。从语法上看是对的,所以编译是没问题的。但是这个程序不可能运行成功,因为在链接的时候会报错。我们已经在increment.c和negate.c中定义了increment和negate函数,现在又在main.c中定义这两个函数,C语言规定不允许有同名的外部函数,链接的时候链接器会发现increment.obj和main.obj中定义了同一个函数,会直接报错 想法3:上面的2种想法都是不可行的,其实思路是一致的:让main函数知道increment函数的存在。正确的做法应该是在main函数前面对increment函数进行提前声明(看清楚,是声明,不是定义,定义和声明是两码事)。 #include <stdio.h> extern int increment(); extern int negate(); int main( void ) { printf( "%d %d\n", increment( 10 ), negate( 10 ) ); printf( "%d %d\n", increment( 0 ), negate( 0 ) ); } 成功 谢谢 大家指正 交流 感谢 http://www.cnblogs.com/mjios/archive/2013/03/21/2974181.html
YI一一一YI 2017-10-24
  • 打赏
  • 举报
回复
成功了 main.c //#include <iostream.h> #include <stdio.h> //#include "increment.c" //#include "negate.c" extern int increment(); extern int negate(); int main( void ) { printf( "%d %d\n", increment( 10 ), negate( 10 ) ); printf( "%d %d\n", increment( 0 ), negate( 0 ) ); } 其他的两个函数不变,文件不变,编译成功 答案在这里 谢谢大家的指教 http://www.cnblogs.com/mjios/archive/2013/03/21/2974181.html
bigfoolee 2017-10-23
  • 打赏
  • 举报
回复
去学习一下makefile

69,382

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧