gcc编译问题

C、C++和Java语言
回复
sysq
帖子: 6
注册时间: 2006-12-18 14:57
送出感谢: 0
接收感谢: 0
联系:

gcc编译问题

#1

帖子 sysq » 2006-12-18 15:13

偶新学linux编程,照书上输入了这个这个显示登录信息的程序

代码: 全选

#include <stdio.h>
#include <utmp.h>
#include <fcntl.h>
#include <unistd.h>


#define SHOWHOST								/* include remotemachine on output*/
int main()
{
	struct utmp current_record;			/* read info into here */
	int			utmpfd;						/* read from this descriptor */
	int			reclen = sizeof(current_record);

	if((utmpfd = open(UTMP_FILE, O_RDONLY)) == -1){
		perror(UTMP_FILE);					/* UTMP_FILE is in utmp.h */
		exit(1);
	}
	while ( read(utmpfd,&current_record,reclen) == reclen )
			show_info(&current_record);
			close(utmpfd);
			return 0;							/* went ok */
}




/*
 *show info()
 *displays contents of the utmp struct in human readable form
 * *note* these sizes should not be hardwired
   */
show_info( struct utmp *utbufp)
{
	printf(%-8.8s¨,utbufp->ut_name);		/* the logname */
	printf(¨ ¨);									/* a space */
	printf(¨%-8.8s¨,utbufp->ut_line);		/* the tty */
	printf(¨ ¨);									/* a space */
	printf(¨%10ld¨,utbufp->ut_time);			/* login time */
	printf(¨ ¨);									/* a space */
#ifdef SHOWHOST
	printf(¨%s¨,utbufp->ut_host);				/* the host */
#endif
	printf(¨\n¨);									/* newline */
}
程序语法应该没问题
但是编译的时候出现以下错误,请问这些问题该怎么解决?谢谢
sysq@ubuntu:~/programmetest$ gcc who1.c -o who1
who1.c: In function ‘main’:
who1.c:16: warning: incompatible implicit declaration of built-in function ‘exit’
who1.c: In function ‘show_info’:
who1.c:34: error: expected expression before ‘%’ token
who1.c:34:11: error: invalid suffix "s" on floating constant
who1.c:34: error: stray ‘\302’ in program
who1.c:34: error: stray ‘\250’ in program
who1.c:35: error: stray ‘\302’ in program
who1.c:35: error: stray ‘\250’ in program
who1.c:35: error: stray ‘\302’ in program
who1.c:35: error: stray ‘\250’ in program
who1.c:35: error: too few arguments to function ‘printf’
who1.c:36: error: stray ‘\302’ in program
who1.c:36: error: stray ‘\250’ in program
who1.c:36: error: expected expression before ‘%’ token
who1.c:36:13: error: invalid suffix "s" on floating constant
who1.c:36: error: stray ‘\302’ in program
who1.c:36: error: stray ‘\250’ in program
who1.c:37: error: stray ‘\302’ in program
who1.c:37: error: stray ‘\250’ in program
who1.c:37: error: stray ‘\302’ in program
who1.c:37: error: stray ‘\250’ in program
who1.c:37: error: too few arguments to function ‘printf’
who1.c:38: error: stray ‘\302’ in program
who1.c:38: error: stray ‘\250’ in program
who1.c:38: error: expected expression before ‘%’ token
who1.c:38:12: error: invalid suffix "ld" on integer constant
who1.c:38: error: stray ‘\302’ in program
who1.c:38: error: stray ‘\250’ in program
who1.c:39: error: stray ‘\302’ in program
who1.c:39: error: stray ‘\250’ in program
who1.c:39: error: stray ‘\302’ in program
who1.c:39: error: stray ‘\250’ in program
who1.c:39: error: too few arguments to function ‘printf’
who1.c:41: error: stray ‘\302’ in program
who1.c:41: error: stray ‘\250’ in program
who1.c:41: error: expected expression before ‘%’ token
who1.c:41: error: stray ‘\302’ in program
who1.c:41: error: stray ‘\250’ in program
who1.c:43: error: stray ‘\302’ in program
who1.c:43: error: stray ‘\250’ in program
who1.c:43: error: stray ‘\’ in program
who1.c:43: error: stray ‘\302’ in program
who1.c:43: error: stray ‘\250’ in program
who1.c:43: error: ‘n’ undeclared (first use in this function)
who1.c:43: error: (Each undeclared identifier is reported only once
who1.c:43: error: for each function it appears in.)
亡灵之猫
帖子: 6
注册时间: 2006-12-18 10:59
送出感谢: 0
接收感谢: 0
联系:

#2

帖子 亡灵之猫 » 2006-12-18 16:54

读一下错误信息

代码: 全选

show_info( struct utmp *utbufp)
{
   printf(%-8.8s¨,utbufp->ut_name);      /* the logname */
   printf(¨ ¨);                           /* a space */
   printf(¨%-8.8s¨,utbufp->ut_line);      /* the tty */
   printf(¨ ¨);                           /* a space */
   printf(¨%10ld¨,utbufp->ut_time);         /* login time */
   printf(¨ ¨);                           /* a space */
#ifdef SHOWHOST
   printf(¨%s¨,utbufp->ut_host);            /* the host */
#endif
   printf(¨\n¨);                           /* newline */
} 
这里的

代码: 全选

   printf(%-8.8s¨,utbufp->ut_name);      /* the logname */
字符串双引号没有打

另外,代码中还是声明一下show_info比较好,容易查错
sysq
帖子: 6
注册时间: 2006-12-18 14:57
送出感谢: 0
接收感谢: 0
联系:

#3

帖子 sysq » 2006-12-18 17:07

我发现除了who1.c:16: warning: incompatible implicit declaration of built-in function ‘exit’ 的错误是由于我的罗技酷影手键盘输入的引号¨ 与标准的引号不同引起的 :oops: 我把键盘布局从“美国英语international”改成Korea,Repubulic of就能在GEDIT中输入正常的引号了,但是切换到命令控制台下输入,符号又变回去了 :(


而who1.c:16: warning: incompatible implicit declaration of built-in function ‘exit’虽然只要把exit(0);删除程序就能成功编译执行;
但是这个exit问题要怎样才能解决?exit函数声明应该在哪里看?我用man -k找不到exit的联机帮助
sysq
帖子: 6
注册时间: 2006-12-18 14:57
送出感谢: 0
接收感谢: 0
联系:

#4

帖子 sysq » 2006-12-18 17:16

谢谢2楼,
那个缺的引号我后来修改的时候发现了,但是主要还是那个引号的问题.....

还有这个who1.c:16: warning: incompatible implicit declaration of built-in function ‘exit’
亡灵之猫
帖子: 6
注册时间: 2006-12-18 10:59
送出感谢: 0
接收感谢: 0
联系:

#5

帖子 亡灵之猫 » 2006-12-18 17:23

exit貌似用在子程序中,用来直接结束程序。
而main函数返回(return)也是结束程序
在main函数中使用return和exit的结果是一样的
建议换成

代码: 全选

return 1;
sysq
帖子: 6
注册时间: 2006-12-18 14:57
送出感谢: 0
接收感谢: 0
联系:

#6

帖子 sysq » 2006-12-18 17:46

哦,但是如果我要在子程序中用exit该怎么办?
它仍然提示这个错误

代码: 全选

#include<stdio.h>
main()
{
        int i;
        if(fork()!=0)
                exit(0);
        for(i=1;i<=10;i++)
        {
                printf("still here..\n");
                sleep(i);
        }
        return 0;
}
sysq@ubuntu:~/programmetest$ gcc fktest.c -o fktest
fktest.c: In function ‘main’:
fktest.c:6: warning: incompatible implicit declaration of built-in function ‘exit’
亡灵之猫
帖子: 6
注册时间: 2006-12-18 10:59
送出感谢: 0
接收感谢: 0
联系:

#7

帖子 亡灵之猫 » 2006-12-18 18:04

奇怪了
我这里把楼上的代码复制后编译是可以正常编译和工作的
(假设int fork(){return 1;})

重新安装一下GCC试试看,会不会是包没有安装全
sysq
帖子: 6
注册时间: 2006-12-18 14:57
送出感谢: 0
接收感谢: 0
联系:

#8

帖子 sysq » 2006-12-18 18:10

我用return也能成功,但是改成exit必然报警........... :cry:
上次由 sysq 在 2006-12-18 18:37,总共编辑 1 次。
sysq
帖子: 6
注册时间: 2006-12-18 14:57
送出感谢: 0
接收感谢: 0
联系:

#9

帖子 sysq » 2006-12-18 18:15

哈哈哈,明白了,要加上<stdlib.h>就没错误了
回复

回到 “C/C++/Java”