Question!!!!!!!!!!!!!!!!!!!!!!!!!!!

bheric87 2009-01-21 08:10:03
为什么对函数的声明要在最上面

extern void p(char *);
int a;
int main()
{
...
}
如果把
extern void p(char *);
int a
位置互换编译时找不到p
...全文
186 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
linzsoft 2009-01-22
  • 打赏
  • 举报
回复
[Quote=引用 13 楼 bheric87 的回复:]
晕查了查手册发现少加了一个参数 gcc -o tt tt1.o tt.o
[/Quote]

加上参数就好了吧? 否则这个bug太惊人了。
waizqfor 2009-01-21
  • 打赏
  • 举报
回复
[Quote=引用 11 楼 bheric87 的回复:]
waizqfor 大侠什么意思?我不是声明了extern 了吗?请帮忙分析下
[/Quote]
抱歉 理解错了 前后定义就差别那么大吗 费解
我在VC6下运行没有报错
bheric87 2009-01-21
  • 打赏
  • 举报
回复
晕查了查手册发现少加了一个参数 gcc -o tt tt1.o tt.o
bheric87 2009-01-21
  • 打赏
  • 举报
回复
gcc version 4.3.2 c98 c99对这个的顺序有规定吗?谁能帮我查查
bheric87 2009-01-21
  • 打赏
  • 举报
回复
waizqfor 大侠什么意思?我不是声明了extern 了吗?请帮忙分析下
waizqfor 2009-01-21
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 bheric87 的回复:]
为了查看编译器在编译成目标文件时的处理我写了两个.c文件最后再一起连接
test1.c
char *string="hello world";
extern void pp(char *p);
int main()
{
pp(string);
return 0;
}

test2.c
#include <stdio.h>
void pp(char *p)
{
printf("%s",p);
}
gcc -c tt.c
gcc -c tt1.c
生成tt.o 和tt1.o后进行连接
gcc -o tt1.o tt.o
提示
tt.o: In function `main':
tt.c:(.text+0x…
[/Quote]
定义全局变量 跟定义外部变量是有区别的
全局变量只在一个文件下的作用域 而外部变量可以在多个文件下使用
xiaoyisnail 2009-01-21
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 bheric87 的回复:]
为了查看编译器在编译成目标文件时的处理我写了两个.c文件最后再一起连接
test1.c
char *string="hello world";
extern void pp(char *p);
int main()
{
pp(string);
return 0;
}

test2.c
#include <stdio.h>
void pp(char *p)
{
printf("%s",p);
}
gcc -c tt.c
gcc -c tt1.c
生成tt.o 和tt1.o后进行连接
gcc -o tt1.o tt.o
提示
tt.o: In function `main':
tt.c:(.text+0x1a): undefined reference to …
[/Quote]

你的gcc什么版本的?代码肯定没有问题
bheric87 2009-01-21
  • 打赏
  • 举报
回复
为了查看编译器在编译成目标文件时的处理我写了两个.c文件最后再一起连接
test1.c
char *string="hello world";
extern void pp(char *p);

int main()
{
pp(string);
return 0;
}

test2.c
#include <stdio.h>
void pp(char *p)
{
printf("%s",p);
}
gcc -c tt.c
gcc -c tt1.c
生成tt.o 和tt1.o后进行连接
gcc -o tt1.o tt.o
提示
tt.o: In function `main':
tt.c:(.text+0x1a): undefined reference to `pp'
collect2: ld returned 1 exit status
当我把char *string="hello world";放到extern void pp(char *p);下面则问题解决




bheric87 2009-01-21
  • 打赏
  • 举报
回复
为了查看编译器在编译成目标文件时的处理我写了两个.c文件最后再一起连接
test1.c
char *string="hello world";
extern void pp(char *p);

int main()
{
pp(string);
return 0;
}

test2.c
#include <stdio.h>
void pp(char *p)
{
printf("%s",p);
}
gcc -c tt.c
gcc -c tt1.c
生成tt.o 和tt1.o后进行连接
gcc -o tt1.o tt.o
提示
tt.o: In function `main':
tt.c:(.text+0x1a): undefined reference to `pp'
collect2: ld returned 1 exit status
当我把char *string="hello world";放到extern void pp(char *p);下面则问题解决




cihren 2009-01-21
  • 打赏
  • 举报
回复
如果把
extern void p(char *);
int a
位置互换编译时找不到p


如果找不到的话,那一定是你或编译器至少有一个处于醉酒状态~哈哈~
xiaoyisnail 2009-01-21
  • 打赏
  • 举报
回复
不会有问题的

#include <stdio.h>

int a;
extern void p(char *);

int main()
{
char str[] = "abc";
p(str);
return 0;
}

void p(char *str)
{
printf("%s\n",str);
}
HelloDan 2009-01-21
  • 打赏
  • 举报
回复

#ifndef __fun_h
#define __fun_h
#include<stdio.h>

void p(char* str)
{
printf("%s\n",str);
}

#endif



#include<stdio.h>
#include"fun.h"
int a=90;
extern void p(char*);



int main()
{
char *pt="Hello";
p(pt);
return 0;
}


waizqfor 2009-01-21
  • 打赏
  • 举报
回复
[Quote=引用楼主 bheric87 的帖子:]
为什么对函数的声明要在最上面

extern void p(char *);
int a;
int main()
{
...
}
如果把
extern void p(char *);
int a
位置互换编译时找不到p
[/Quote]
LZ 在那写都一样 把你全码贴出来看看吧
HelloDan 2009-01-21
  • 打赏
  • 举报
回复
我试过的没有任何问题,不知道你的是怎样的。
萬空 2009-01-21
  • 打赏
  • 举报
回复
函数放下面 要在mian函数里定义一下

69,371

社区成员

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

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