c - 如何将一个C文件分成多个文件

标签 c file header makefile modular

我正在为一个类(class)做一个项目,需要帮助将我的程序分成不同的部分。我的老师给了我们一个提示,说明哪些文件会做什么,但他没有告诉我们如何编写头文件。根据我在网上学到的知识,我将每个 .c 文件中的函数原型(prototype)放入其自己的 .h 文件中,并将它们包含在 .c 文件中的“header.h”中。但是,我收到编译错误,例如

course1.c:20:3: warning: implicit declaration of function ‘initialize’ [-Wimplicit-function-declaration]
   initialize(courses, subjects, CRN);
   ^
vector1.c:14:6: error: conflicting types for ‘resize’
 void resize(char ***courses, char***subjects, int **CRN) {
      ^
In file included from vector1.c:2:0:
vector.h:11:6: note: previous declaration of ‘resize’ was here
 void resize(char ***subjects, char ***courses, int **CRNs, int *size);
      ^
vector1.c:39:6: error: conflicting types for ‘deallocate’
 void deallocate(char **courses, char**subjects, int *CRN) {
      ^
In file included from vector1.c:2:0:
vector.h:12:6: note: previous declaration of ‘deallocate’ was here
 void deallocate(char **subjects, char **courses, int *CRNs, int size);

我非常确定我的文件具有正确的语法,因为我将它们单独编译成 .o 文件并且它们工作得很好。有人可以概括地说一下如何将程序分成单独的文件吗?我想我显然做错了。编译器给我的一个错误是,当我包含的头文件中明确定义了函数时,该函数未定义。

最佳答案

    course1.c:20:3: warning: implicit declaration of function ‘initialize’ [-Wimplicit-       function-declaration]
   initialize(courses, subjects, CRN);

这意味着您需要将其声明为

 int initialize(courses, subjects, CRN);

  void initialize(courses, subjects, CRN);

在旧的 C 风格中,如果您不指定,编译器会将其视为返回 int

vector1.c:14:6: error: conflicting types for ‘resize’
 void resize(char ***courses, char***subjects, int **CRN) {
  ^
In file included from vector1.c:2:0:
vector.h:11:6: note: previous declaration of ‘resize’ was here
void resize(char ***subjects, char ***courses, int **CRNs, int *size);
  ^

这意味着您在不同位置定义了函数 resize 两次,并且原型(prototype)不同(它们具有不同数量的参数)。这与函数deallocate相同。

关于c - 如何将一个C文件分成多个文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26100127/

相关文章:

c - 2个数字的总和,输入错误

Python文件缓存

java - 使用 RestTemplate 上传 CommonsMultipartFile 的文件失败

php - 如何在 PHP 中读取 tcp/ip header ?

c - 使用数组时后缀/前缀如何工作

c - 编译C代码时出错

Python 字数统计不起作用

apache - 通过 htaccess 禁用 Apache http2 公告

python - Google App Engine - 用于缓存控制的 headers[] 和 headers.add_header()

c - http 服务器 (c) - 发送命令不起作用(来自服务器的空回复)