Questions tagged [header-files]

Header files are used in some programming languages to hold source code as a single copy that may be reused in multiple source files. This tag should be used for questions about using header files. A tag for the programming language such as C, C++, PHP, or Ruby should be used along with this tag since header file usage can vary between programming languages. This tag is not normally used with Java or Go programming and the import directive.

Header files are used in some programming languages to hold source code in a single container, the header file, that may be needed in multiple source files during the processing of the source files. The goal of a header file is to reduce duplication of the same source lines needed in multiple files. Instead of cloning the source lines into each of the files where they are needed, the source lines are put into a header file. The header file is then pulled into a source file by the compiler or script engine during processing of a source file.

The result is that only a single copy of the source lines needs to be maintained and the single copy of source can be reused in multiple files.

The primary purposes of header files is to make it easier to reuse functionality and to reduce errors caused by duplicated source code which becomes slightly different due to maintenance changes.

Typically a special directive such as include or require is used to insert a copy of the contents of the header file at a particular line of a source file during source processing by compiler or script engine. The concept is the include or require directive is replaced by the contents of the header file. Header files are used in a number of programming languages such as C and C++ as well as PHP and Ruby and Lua. The semantics of these directives will vary from programming language to programming language.

The import directive used in languages such as Java and Go is different from the include directive of C and C++ being more of a way to establish linkages and references between classes and packages as a part of combining software components (Java and Go) rather than the including of lines of source code (C and C++ and PHP).

Source lines typically put into header files are definitions and declarations that are needed in multiple files. Information may include class definitions or declarations, function prototypes or structure definitions/declarations or constants of various kinds.

Considerations for header files

When determining what source to put into a header file the primary rule of thumb is they should be lines of source that can be included in multiple files without causing a problem. For instance with the C programming language, a definition of a struct would be appropriate in a header file.

However putting the definition of a standard (not in-line) function into a C header file would not be appropriate. The problem with putting a standard C function definition into a header file is the function will be duplicated at each point where the header file is pulled into a source file. The result would be multiple definitions of the same function causing a linker error.

Some programming languages require care be taken that a header file is not included more than once. This can happen when several header files that are included into a source file themselves include still another header file. In the C and C++ programming languages macros or pragmas are used to enforce a once only rule. In other cases the source lines in the header file may not require a once only rule due to the nature of the source in the header file.

Care must also be taken that a dependency cycle is not created causing an infinite loop during the processing of header files. A header file may include other header files which end up including the original header file again causing a cycle or infinite loop until the compiler runs out of resources during header file processing.

PHP has the require_once directive which will perform a check if the header file has already been included or not.

In C and C++ an external utility, the Preprocessor, handles macro processing and other directives such as the include directive for header files generating a temporary file that is then compiled. In other languages the include directive processing is built into the compiler or script engine.

Typically a file extension of .h is used for header files. However for many programming languages this naming convention is not a requirement for correct processing.

3503 questions
1
vote
2 answers

In compilation time how to find the macro is defined in which header file

Lets take the below code , If the macro HAVE_SYS_SELECT_H is defined then they included the header file. I need to know where the macro is ? That is in which which header file the macro is defined. Is there any option for that while compiling the…
pretty
  • 67
  • 2
  • 10
1
vote
1 answer

xcode pods : unknown type name 'link' : link to non existing header file?

As a C# Developer who inherited an iOS app, I'm struggling to build/run this app. Xcode throws errors for a header file, which (I think ) it cannot find. When i open the file (Pods/Headers/Build/CocoaSecurity/CocoaSecurity.h) , it shows as its…
kris
  • 11
  • 5
1
vote
1 answer

Are there any issues if a header-only library is used both in another library and in the application that links to this library?

I am developing an Open Source library and would have a headers-only library as dependency for it. This dependency will only be used in a single cpp file and thus not exposed via our libraries' headers. Considering the following setup of an…
Ident
  • 1,184
  • 11
  • 25
1
vote
1 answer

C++ Method Definition & Variable Declaration

I am trying to split up my arduino code base from a single 'ino' file into a proper-ish c++ program by creating classes which have both .h & .cpp files. I am running into a few errors that I just haven't been able to solve. I hope I am missing…
Wesley
  • 2,921
  • 6
  • 27
  • 30
1
vote
2 answers

How to break C++ program into modules

I'm writing a c++ program that has two functions that can run completely independent one from another. So i want to break them into two programs. The problem is that both of them rely on the same set of functions (that can grow or change). What i…
Arheisel
  • 186
  • 11
1
vote
1 answer

It said there's something wrong with the header file, mmsystem.h, i can't use PlaySound()

#include #include #include #include #include #pragma comment (lib, "winmm.lib") using namespace std; int main() { PlaySound(TEXT("Happy Birthday To You.wav"), NULL, SND_SYNC); …
Gavin.G
  • 13
  • 9
1
vote
1 answer

How do I use configure.ac to include the contents of AC_INCLUDES_DEFAULT?

AC_INCLUDES_DEFAULT is a macro that expands to a bunch of standard header files, plus related autoconf-generated checks. In fact, the docs says it expands to this: #include #ifdef HAVE_SYS_TYPES_H # include #endif #ifdef…
JesseTG
  • 2,025
  • 1
  • 24
  • 48
1
vote
3 answers

C++ pointer or header issues

I have an example program whose main cpp file has Rectangle rect(5, 5); Triangle tri(5, 5); Shape *rectangle1 = ▭ Shape *triangle1 = &tri; rectangle1->setValues(3, 3); triangle1->setValues(3, 3); int area =…
Arthur Lee
  • 121
  • 1
  • 2
  • 14
1
vote
1 answer

Go/Golang Cross-Compile from Mac to Windows: fatal error: 'windows.h' file not found

Summary: when I try cross-compiling a .go source file that includes a C file somewhere in the file chain, targeting Windows AMD64 from a Mac host, I get: /usr/local/go/src/runtime/cgo/gcc_windows_amd64.c:8:10: fatal error: 'windows.h' file not…
Bart Silverstrim
  • 3,445
  • 6
  • 34
  • 44
1
vote
2 answers

QT Creator Linker Error: Link2019 File Not Found

I'm not quite sure where my problem is. I'm using Qt Creator for an OpenGL project, and for the first time, I have tried adding another few classes to my project. I've added both header and implementation files : terrain.h, terrain.cpp,…
whenitrains
  • 468
  • 5
  • 21
1
vote
1 answer

Where should self-created headers reside in Eclipse-cdt?

Below is a picture I grabbed off google, and it is actually linked back to a previous Stackoverflow question. So, on the left (project explorer view) we see an Includes and src directory. I believe the Includes really is just paths to where header…
user_ABCD
  • 347
  • 2
  • 15
1
vote
2 answers

Using Vim to find definition when navigation around /usr/include/files

I found it was casual to navigate between header files when you want do something under linux. And when I want find a definition of something, for example, struct sockaddr_storage. What I am doing right now is :!find /usr/include/ -type f | xargs…
Frank Wang
  • 788
  • 7
  • 23
1
vote
3 answers

.cpp file will not link with .h file

I am trying to connect two cpp files - MIPSConversion.cpp and TestMIPSConversion.cpp - to the same header file - MIPSConversion.h. However, when I try to call the method readInTheFile() (located in MIPSConversion.cpp) from main() (located in…
user6122388
1
vote
1 answer

built tcc for armv7 and ran it on android device, where would I put header files?

Hello so I compiled tcc (Tiny C Compiler) with the armv7 cross compiler on Ubuntu, arm-linux-gnueabi-gcc. I pushed it to the android bin (/system/bin) and was able to execute it, however I'm missing all of the C header files! Where would I put the…
Bob Smith
  • 58
  • 7
1
vote
1 answer

Multiple structures defined inside a header file - Should I move them out in separate h and cpp files

One of my previous colleagues wrote a huge header file which has around 100 odd structures with inline member function definitions. This structure file is included in most class implementations(cpp files) and header files(don't know why my colleague…
nurabha
  • 1,152
  • 3
  • 18
  • 42
1 2 3
99
100