【问题标题】:How to add another C file in Code::Blocks如何在 Code::Blocks 中添加另一个 C 文件
【发布时间】:2014-04-09 00:10:04
【问题描述】:

所以我的老师希望我的项目的函数原型和 typedef 存储在单独的 .c 文件中。
所以 main.c 有事情要做,another.c 有原型和 typedef,another.h 有声明。

我该怎么做?我正在使用 GNU GCC 编译器,因为这似乎是编译器特定的问题。我正在尝试 gcc another.c 但编译器无法识别 gcc 所以我认为我做错了。

我感觉如此密集....我的整个项目运行良好,但应该在 another.c 中的所有内容都在 another.h....

谢谢

【问题讨论】:

  • 你的意思是#include?听起来你应该在课堂上多听!
  • 她从来没有在课堂上讲过它,实验室的助教拒绝帮助我,因为我不想学习如何在 linux 中编码。我尝试了#include another.c 没有工作,我尝试了 gcc another.c
  • 那么你为什么要用“so”开头的句子呢?那么为什么不将句子的第一个字母和“I”大写呢?所以请至少像对待潜在雇主一样对待我们。

标签: c gcc compiler-construction


【解决方案1】:

main.c 看起来像这样:

// bring in the prototypes and typedefs defined in 'another' 
#include "another.h"

int main(int argc, char *argv[])
{
    int some_value = 1;

    // call a function that is implemented in 'another' 
    some_another_function(some_value);
    some_another_function1(some_value);
    return 1;        
}

another.h 应该包含在 another.c 文件中找到的函数的 typedef 和函数原型:

// add your typdefs here

// all the prototypes of public functions found in another.c file
some_another_function(int some_value);
some_another_function1(int some_value);

another.c 应该包含在 another.h 中找到的所有函数原型的函数实现:

// the implementation of the some_another_function
void some_another_function(int some_value)
{
    //....
}

// the implementation of the some_another_function1
void some_another_function1(int some_value)
{
    //....
}

【讨论】:

  • 非常感谢,我想我只是把所有东西都倒过来了。
  • 作为一般规则,请记住 c 文件应包含运行的代码,而头文件包含该代码的定义。因此,运行的代码(即可以在其上放置断点的代码)应该在 c 文件中找到,而不是在 h 文件中(在大多数情况下)。您还包括 h 文件并编译和链接 c 文件。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-08-23
  • 1970-01-01
  • 1970-01-01
  • 2013-08-13
相关资源
最近更新 更多