makefile中小C程序

ZhengNanSoft 2010-10-19 03:14:15
1楼

代码:
//first.c
#include<stdio.h>
#include"second.c"
int main(){
printf("Fighting") ;
printWords() ;
}
//second.c
#include<stdio.h>
void printWords(){
printf("The second file") ;
}
//Makefile文件
first:first.o second.o
gcc first.o second.o -o first
first.o:first.c
gcc -c first.c -o first.o
second.o:second.c
gcc -c second.c -o second.o
编译之后出错,但是将second.c中的函数和first.c中的函数调用删掉后就成功
Why??
...全文
132 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
ZhengNanSoft 2010-10-19
  • 打赏
  • 举报
回复
Thanks
jihen 2010-10-19
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 tanxs001 的回复:]

同意memoleak 和punchio,second.c就是一个函数,生成不了second.o!
[/Quote]
可以生成second.o,加-c 命令就可以,
memoleak 说的很对,主要是你的头文件包含了之后已经定义过了(声明加定义),你的second.o文件就多余了,如果你真的要用,就改成下面的形式。只加一个声明就可以了
//first.c
#include<stdio.h>
void printWords();
int main(){
printf("Fighting") ;
printWords() ;
}
//second.c
#include<stdio.h>
void printWords(){
printf("The second file") ;
}
//Makefile文件
first:first.o second.o
gcc first.o second.o -o first
first.o:first.c
gcc -c first.c -o first.o
second.o:second.c
gcc -c second.c -o second.o
ZhengNanSoft 2010-10-19
  • 打赏
  • 举报
回复
我从英文上看出,是重定义错误,4楼是很准确的,如果不写 -- #include"second.c" 而改为 #include<second.c>
时,它好像是告诉我,它找不到,
大家应该知道,#include"second.c" 这样写,它找的范围比用“<文件名>”这样范围广,因此我采用了"文件名"这种方法,
不过还有一个问题,那就是
gcc -c 文件.c 生成的".o"中间文件和makefile中的.o目标文件有什么区别?
谢谢,嘻嘻
s393102639 2010-10-19
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 steptodream 的回复:]
第一次看到 这样写#include"second.c"
[/Quote]
我也是
tanxs001 2010-10-19
  • 打赏
  • 举报
回复
同意memoleak 和punchio,second.c就是一个函数,生成不了second.o!
胖企鹅 2010-10-19
  • 打赏
  • 举报
回复
second.o:second.c
gcc -c second.c -o second.o
编译肯定出错的,生成不了second.o
memoleak 2010-10-19
  • 打赏
  • 举报
回复
一般不推荐使用include "second.c"这样的写法.
include是预编译指令,直接将后面的文件插入到当前文件中.这样first.o中已经包含函数printWords.
second.o里面也有printWords,于是连接生成可执行文件的时候就会出现重定义的错误.
Makefile写成
first:first.o
gcc first.o -o first
first.o:first.c
gcc -c first.c -o first.o
就好了,由于使用了include "second.c", second.c不需要编译,也不需要连接
justkk 2010-10-19
  • 打赏
  • 举报
回复
编译之后什么错误?
steptodream 2010-10-19
  • 打赏
  • 举报
回复
第一次看到 这样写#include"second.c"
ZhengNanSoft 2010-10-19
  • 打赏
  • 举报
回复
然后make之后,理应当输出编译的信息,可是编译出错

23,120

社区成员

发帖
与我相关
我的任务
社区描述
Linux/Unix社区 应用程序开发区
社区管理员
  • 应用程序开发区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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