找不到main函数

软件和网站开发以及相关技术探讨
回复
头像
caca
帖子: 22
注册时间: 2007-04-04 20:05
来自: dalian

找不到main函数

#1

帖子 caca » 2007-06-16 18:12

/usr/lib/gcc/i486-linux-gnu/4.0.3/../../../../lib/crt1.o: In function `_start':../sysdeps/i386/elf/start.S:115: undefined reference to `main'。
编译helloworld.c没有这个问题,
我是编译err.c这个文件时出现这个问题的,这个程序是从unix环境高级编程里抄下来的,不知道编译怎么会出错。难道是因为版本问题吗?
头像
bones7456
帖子: 8495
注册时间: 2006-04-12 20:05
来自: 杭州
联系:

#2

帖子 bones7456 » 2007-06-16 19:19

要把你的编译的命令写上来。。。
关注我的blog: ε==3
头像
caca
帖子: 22
注册时间: 2007-04-04 20:05
来自: dalian

#3

帖子 caca » 2007-06-18 21:57

我知道是什么错了,因为这个.c文件里没有main函数, 我直接执行gcc err.c当然不能生成可执行文件。不过我今天有出现了另一个问题。我编了一个hello.c
#include <stdio.h>
int main()
{
printf("hello\n");
return 0;
}
然后在同一目录下:vi makefile
hello: hello.o
gcc -o hello hello.o
hello.o:hello.c
gcc -c hello.c
clean:
rm hello.o
命令我用的是tap键
保存退出
执行make,产生 bash: make: command not found
我已经写了makefile了呀,怎么还会这样
头像
xport
帖子: 1277
注册时间: 2006-05-19 21:23
联系:

#4

帖子 xport » 2007-06-19 0:26

有安装 make 么,一般来说安装了 build-essential 软件包比较方便,含了gcc和make等依赖的软件包。
查看 make 软件包安装状态 写了: $ aptitude show "make"
Package: make
State: installed
Automatically installed: no
Version: 3.81-3build1
Priority: optional
Section: devel
Maintainer: Ubuntu Core Developers <ubuntu-devel-discuss@lists.ubuntu.com>
Uncompressed Size: 1237k
Depends: libc6 (>= 2.5-0ubuntu1)
Suggests: make-doc
Description: The GNU version of the "make" utility.
GNU Make is a program that determines which pieces of a large program need to be recompiled and issues the
commands to recompile them, when necessary. More information about GNU Make can be found in the `make' Info
page. The upstream sources for this package are available at the location ftp://ftp.gnu.org/gnu/make/. The
documentation for this package does not meet the Debian Free Software Guidelines, and has been removed from
this package.
其实我是一个演员!-http://blog.csdn.net/xport/
antonym55
帖子: 353
注册时间: 2007-04-03 9:52
联系:

#5

帖子 antonym55 » 2007-06-19 9:04

是Makefile, M 要大写, 也可以用 make -f makefile 试试
头像
bones7456
帖子: 8495
注册时间: 2006-04-12 20:05
来自: 杭州
联系:

#6

帖子 bones7456 » 2007-06-19 9:15

恩,没有安装make。
上次由 bones7456 在 2007-06-19 12:52,总共编辑 1 次。
关注我的blog: ε==3
头像
zhan
帖子: 1880
注册时间: 2005-08-15 0:04
来自: 南7技校

#7

帖子 zhan » 2007-06-19 12:39

make 查找文件的顺序是 GNUmakefile Makefile 和 makefile.

大写只是一个风格的问题,同时,楼主的问题明显是编译环境的问题。没有 make . xport 说了办法了..
飞得高,飞得低,学习再学习,多少大秘密!
http://zhan.blog.ubuntu.org.cn
头像
caca
帖子: 22
注册时间: 2007-04-04 20:05
来自: dalian

问题已经解决,确实是没安装make。谢谢!

#8

帖子 caca » 2007-06-19 15:58

首先谢谢前面为我解决问题的人,真心感谢,没想到这里回复的这么快。呵呵,以前的帖子好几天才看到有人回,可能是太弱智了吧。

不过,笨笨的我又碰到一个新问题,在写makefile时,包含头文件,执行make不错,找不到调用函数。
我的程序如下:
hello.c
#include<stdio.h>
#include "mysub.h"
int main()
{
printf("you are now in main process!\n");
func();
return 0;
}

mysub.h
void func();

sub.c
#include<stdio.h>
void func()
{
printf("you are now in sub process!\n");
}

makefile:
hello:hello.o
gcc -o hello hello.o
hello.o:hello.c mysub.h
gcc -c hello.c
hello.c. mysub.h.sub.c放在同一目录下
执行make,在main()函数里找不到func()的定义。
在终端执行gcc hello.c sub.c可以成功,还有,我把函数定义放在mysub.h中执行make可以通过
在包含自己定义的头文件时,makefile 应该怎么写呢?我这是照着老师的课件写的,昨天作实验也没有通过。
希望还有人看这个帖子,谢谢啊。
上次由 caca 在 2007-06-19 17:13,总共编辑 1 次。
antonym55
帖子: 353
注册时间: 2007-04-03 9:52
联系:

#9

帖子 antonym55 » 2007-06-19 16:54

代码: 全选

all:hello
	
hello:hello.o sub.o
	gcc -o hello hello.o sub.o

hello.o:hello.c
	gcc -c hello.c

sub.o:sub.c
	gcc -c sub.c

clean:
	rm *.o hello
或者

代码: 全选

#定议编译器和编译选项, 使用时用${ } 括起来
CC=gcc
CFLAGS=-g

all:hello

# $@    --目标文件
# $^	 --所依赖的文件
hello:hello.o sub.o
	${CC} ${CFLAGS} -o $@ $^
	
# .c .o --建立.c 到.o规则
# $<	 --第一个依赖文件
.c .o:
	${CC} ${CFLAGS} $<

clean:
	rm *.o hello

头像
caca
帖子: 22
注册时间: 2007-04-04 20:05
来自: dalian

就好了

#10

帖子 caca » 2007-06-19 17:09

这样就是把所有的.c源文件都告诉makefile.这样make是可以成功的。
但是,如果这样的话,头文件有什么作用呢?
回复