需要在头文件中的函数原型? [英] Are function prototypes needed in header files?

查看:310
本文介绍了需要在头文件中的函数原型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在robotc这仅仅是c。与外接程序编程编程(如下所有的C规则)。为了组织我的code我已经把我的子程序头文件,并从我的主要.c文件所引用。我还能引用从C文件的头文件的方法,没有把函数原型中
头?

I am programming in robotc which is just c programming with add-ins (follows all c rules). In order to organize my code I have put my subroutines in header files and are referencing from my main c document. Can I still reference the methods in the header files from the c document without putting function prototypes in the headers?

例如:

code在main1.c

Code in main1.c

    #include header1.h
    task main()
    {
      header_method();
    }

code在那么header1.h(无函数原型)

Code in header1.h (no function prototypes)

   header_method()
   {
   //do stuffs
   }


还是我必须这样做:


Or do I have to do this:

   void header_method();

   header_method()
   {
   //do stuffs
   }

的原因是,我只能宣布​​了一定的全局变量为我robotc机器人。

The reason is that I can only declare a certain amount of global variables for my robot in robotc.

推荐答案

您应该(几乎)从来没有把函数的定义的头文件,如你在做你的那么header1.h

You should (almost) never put function definitions in header files, as you've done in your header1.h.

头文件应该包含函数的声明的(原型)。

Header files should contain function declarations (prototypes).

(A原型是一个函数声明,指定类型的参数。有没有指定参数类型非原型函数声明,但他们是过时的,而且也没有理由去使用它们。)

(A "prototype" is a function declaration that specifies the types of the arguments. There are non-prototype function declarations that don't specify argument types, but they're obsolescent and there's no reason to use them.)

函数的定义的(用 { ... } code实现该函数)应该在 .C 文件。

Function definitions (with the { ... } code that implements the function) should be in .c files.

每个 .C 文件应具有任何功能的的#include 指令,它调用的或定义

Each .c file should have a #include directive for any functions that it calls or defines.

和每个头文件应该由包括警卫免受多次包含。

And each header file should be protected from multiple inclusion by include guards.

的想法是,每个功能的声明的出现在每个恰好一次的翻译单元的(即编译每个源文件),每个功能的定义在你的整个程序出现一次。

The idea is that each function declaration appears exactly once in each translation unit (each source file that you compile), and each function definition appears exactly once in your entire program.

如果您有在只有一个 .C 文件中的函数,你可以把它的声明和定义在同一 .C 文件(你应该把它定义为静态)。事实上,如果定义的任何调用之前出现,你可以省略单独的声明;定义本身作为一个声明。

If you have a function that's used in only one .c file, you can put its declaration and definition in the same .c file (and you should probably define it as static). In fact, if the definition appears before any calls, you can omit the separate declaration; the definition itself acts as a declaration.

(定义为在线函数可以有点复杂化这种模式。我建议不要担心,就目前而言)

(Functions defined as inline can complicate this model a bit; I suggest not worrying about that for now.)

这篇关于需要在头文件中的函数原型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆