【问题标题】:How to break a C file into multiple files如何将一个C文件分成多个文件
【发布时间】:2014-11-23 20:37:36
【问题描述】:

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

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 文件并且它们运行良好。有人可以概括地说,如何将程序分成单独的文件?我想我显然做错了。编译器给我的一个错误是一个函数没有定义,当它在我包含的头文件中明确定义时。

【问题讨论】:

  • 请格式化您的问题:)(在突出显示代码元素后使用页面顶部的 { } 按钮)以帮助提高可读性,因为目前很难阅读。
  • 如果您阅读编译错误,您会看到存在冲突的函数减速。 resizedeallocate 函数在 vector.hvector1.c 中定义不同。 initialize 函数有一个警告,这意味着在定义它之前使用调用了该函数。这是一个警告,而不是编译错误。

标签: c file header makefile modular


【解决方案1】:
    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 两次并且原型不同(它们具有不同数量的参数)。函数deallocate 也是如此。

【讨论】:

  • 嗨,John,我在包含的头文件中添加了初始化声明。
  • 你能告诉我你声明函数initialize的那一行吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-05-13
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多