AJINKYA
AJINKYA

Reputation: 811

How to Create a Customised Header File in C and Use It in a Program

I want to create a header file in C and add it to a library. How do I create the header file and add/access it to/from the library.

Upvotes: 0

Views: 812

Answers (2)

eldarerathis
eldarerathis

Reputation: 36243

Some header:

//header.h
#ifndef HEADER_H
#define HEADER_H

int ThisReturnsOne() {
    return 1;
}

#endif //HEADER_H

Some c file:

//file.c
#include "header.h"

int main() {
    int x;
    x = ThisReturnsOne(); //x == 1
}

So the contents of "header.h" are available to "file.c". This assumes they are in the same directory.

Edit: Added include guards. This prevents the header file from being included in the same translation unit twice.

Upvotes: 2

Matt Joiner
Matt Joiner

Reputation: 118720

Create a file with the extension .h, for example mystuff.h. Put the desired header contents in there, and include it in your sources via #include "mystuff.h".

Upvotes: 2

Related Questions