Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I create C header files [closed]

I want to be able to create a collection of functions in a header file that I could #include in one of my C Programs.

like image 551
user340838 Avatar asked May 14 '10 01:05

user340838


People also ask

How can I create my own header file in C?

To make a header file, we have to create one file with a name, and extension should be (*. h). In that function there will be no main() function. In that file, we can put some variables, some functions etc.

Can we write C program without header files?

Yes it is possible to write a simple program without header files, but why would you do that ? Header files are useful to share definitions, constants, functions prototypes, etc between multiple files or modules.

Do all C files need a header file?

No, it is not necessary. The reason of the header files is to separate the interface from the implementation.

What is the file extension for C header files?

A header file is a file with extension . h which contains C function declarations and macro definitions to be shared between several source files.


2 Answers

  1. Open your favorite text editor
  2. Create a new file named whatever.h
  3. Put your function prototypes in it

DONE.

Example whatever.h

#ifndef WHATEVER_H_INCLUDED #define WHATEVER_H_INCLUDED int f(int a); #endif 

Note: include guards (preprocessor commands) added thanks to luke. They avoid including the same header file twice in the same compilation. Another possibility (also mentioned on the comments) is to add #pragma once but it is not guaranteed to be supported on every compiler.

Example whatever.c

#include "whatever.h"  int f(int a) { return a + 1; } 

And then you can include "whatever.h" into any other .c file, and link it with whatever.c's object file.

Like this:

sample.c

#include "whatever.h"  int main(int argc, char **argv) {     printf("%d\n", f(2)); /* prints 3 */     return 0; } 

To compile it (if you use GCC):

$ gcc -c whatever.c -o whatever.o $ gcc -c sample.c -o sample.o 

To link the files to create an executable file:

$ gcc sample.o whatever.o -o sample 

You can test sample:

$ ./sample 3 $ 
like image 198
Pablo Santa Cruz Avatar answered Sep 17 '22 12:09

Pablo Santa Cruz


Header files can contain any valid C code, since they are injected into the compilation unit by the pre-processor prior to compilation.

If a header file contains a function, and is included by multiple .c files, each .c file will get a copy of that function and create a symbol for it. The linker will complain about the duplicate symbols.

It is technically possible to create static functions in a header file for inclusion in multiple .c files. Though this is generally not done because it breaks from the convention that code is found in .c files and declarations are found in .h files.

See the discussions in C/C++: Static function in header file, what does it mean? for more explanation.

like image 30
Bryan Ash Avatar answered Sep 16 '22 12:09

Bryan Ash