LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 1055|回复: 10

关于两个文件的编译!

[复制链接]
发表于 2004-3-13 10:36:10 | 显示全部楼层 |阅读模式
  1. #include <unistd.h>
  2. static int test(int);
  3. int
  4. main(void)
  5. {
  6.   int b=6;
  7.   printf("the fun num is %d\n",test(b));
  8.   return(0);
  9. }
复制代码

#gcc -g -c file_a.c
file_a.c:2: warning: `test' used but never defined


  1. static int
  2. test(int a) {
  3.   printf("%d\n",a);
  4.   return(a);
  5. }
复制代码

#gcc -g -c fille_b.c
#gcc -o file_a.o file_b.o

这属于基础不过关,请大家帮忙!!:help :help
发表于 2004-3-13 12:42:06 | 显示全部楼层
gcc file_a.c file_b.c -o myfile

一般应该加一个test.h文件,

  1. #ifndef TEST_H
  2. #define TEST_H

  3. int test(int);

  4. #endif /* TEST_H */
复制代码

在file_a.c和file_b.c里面加入
#include "test.h"
把file_a.c里的
static int test(int);
删掉。
用 gcc file_a.c file_b.c -o myfile 编译。

另外,你的static是不对的,函数加static的意思就是把函数的作用域限制在本文件内,你要用两个文件就不能加static
发表于 2004-3-13 18:01:42 | 显示全部楼层

extern关键字也行吧。

呵呵。
 楼主| 发表于 2004-3-14 13:04:45 | 显示全部楼层
谢谢两位!一定要用头文件吗?要是有很多C文件,就要写个Makefile文件了?
还是都用头文件写.

还有其他的方法吗?
发表于 2004-3-14 13:26:19 | 显示全部楼层
用头文件,这是大家认为比较好的方法
头文件和Makefile没什么特别的关系吧,.c文件多了,当然是写个Makefile好一点啦
发表于 2004-3-14 20:04:19 | 显示全部楼层
有些不懂了。
#ifnef TEST_H
#define TEST_H
....
#endif
这个逻辑关系好象没用哦?不要上面的三行不行吗?就一行:
int test(int);
这个逻辑解构有什么用呢?
发表于 2004-3-14 21:06:24 | 显示全部楼层
这个是为了防止头文件被包含两次(或以上),一般的头文件都有这三行
发表于 2004-3-14 21:09:33 | 显示全部楼层
:thank 在这里TEST_H没有用上,是个摆设?
发表于 2004-3-14 21:18:12 | 显示全部楼层
当test.h第一次被包含的时候,TEST_H还没有定义,所以就
#define TEST_H /* 定义TEST_H */
同时也引入了头文件,当第二次被包含的时候因为TEST_H已经定义了,所以头文件就不会被引入。
发表于 2004-3-14 21:41:06 | 显示全部楼层
应该把static都去掉
用static声明后全局变量,在别的文件里就不能用了吧?
您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表