Do all C functions need to be declared in a header file

人盡茶涼 提交于 2019-12-24 01:38:36

问题


Do I need to declare all functions I use in a .c file in a header file, or can I just declare and define right there in the .c file? If so, does a definition in the .c file in this case count as the declaration also?


回答1:


For the compiler, it does not matter if a declaration occurs in a .h or a .c file, because the compiler sees the preprocessed form.

For the human developer reading and contributing to your code, it is much better (to avoid copy&pasting the same declaration twice) to put the declaration of any function used in more than one translation unit (i.e. .c file) in some #include-d header.

And you can define a function before using it.

BTW, you might even avoid declaring a function that you are calling (it defaults to returning int for legacy purposes), but this is poor taste and obsolete way of coding (and most compilers can emit a warning in that case).




回答2:


Do I need to declare all functions I use in a .c file in a header file, or can I just declare and define right there in the .c file?

You used "use" in the first question and "define" in the next question. There is a difference.

void foo()
{
    bar(10);
}

Here, foo is defined and bar is used. You should declare bar. If you don't declare bar, the compiler makes assumptions about its return type.

You can declare bar in the .c file or add the declaration in a .h file and #include the .h file in the .c file. Whether you use the first method or the second method is up to you. If you use the declaration in more than one .c file, it is better to put that in a .h file.

You can define foo without a declaration.

If so, does a definition in the .c file in this case count as the declaration also?

Every function definition counts as a declaration too.




回答3:


The answer to both questions is yes. You can declare c-functions in both header and .c file. Same with definition. However, if you are defining it in header file, you may have slight problems during compilation.




回答4:


By default functions have external linkage. It means that it is supposed that functions potentially will be used in several compilation units.

However sometimes some auxiliary functions that form implementations of other functions are not designed to be used in numerous compilation units. Such functions declared with keyword static have internal linkage.

Usually they are declared and defined inside some .c module and are not visible in other compilation units.




回答5:


One occasion that requires functions to be declared in a separate header is when one is creating a library for other developers to use. Some libraries are distributed as closed source and they are provided to you as a library file (*.dll / *.so ...) and a header.

The header file would contain declarations of all publicly accessible functions and definitions of all publicly required structures, enums and datatypes etc.

Without this header file the 3rd party library user would not know how to interface with the library file and thus would not be able to link against it.

But for small, trivial C programs that are not intended for use by other people, no you can just dump everything into a C file and build it. Although you might curse yourself years later when you need to maintain that code :)




回答6:


No, it is not necessary.

The reason of the header files is to separate the interface from the implementation. The header declares "what" a class (or whatever is being implemented) will do, while the .c file defines "how" it will perform those features.

This reduces dependencies so that code that uses the header doesn't necessarily need to know all the details of the implementation and any other classes/headers needed only for that. This will reduce compilation times and also the amount of recompilation needed when something in the implementation changes.



来源:https://stackoverflow.com/questions/35355727/do-all-c-functions-need-to-be-declared-in-a-header-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!