Can I centralize all my project Header’s into one? C/C++ Language

Asked

Viewed 28 times

0

Good afternoon.

I have a problem with my C project. I have made some libraries and would like to centralize their imports in just one header so that when calling all, give a #include only in the centering header and they are imported.

For example - I have three header’s: header1.h,header2.h and header3.h and would like to centralize them into just one, central_header.h. When to create the main.c, instead of giving #includein the three header’s I just run the #include central_header.hthat it already matters everyone to the main.c.

1 answer

0

You can do all includes in your central header file, so when main. c call it, the file calls the other header files. Example:

#ifndef CENTRAL_H_INCLUIDA
#define CENTRAL_H_INCLUIDA

#include "teste.h"
#include "arquivo.h"

#endif

When you include the above code in the main. c file the test headers. h and file. h files will also be included. To avoid including the same file several times do not forget the #ifndef in all files . h

  • Yes, these settings helped me a lot but I came to another difficulty. I made the includes in a centralized Header with #ifndef and another Header called this centralized Header and then the main one called it, but it didn’t work. Shouldn’t work?

  • It should work, but if it’s not working it’s because there’s an error in at least 1 of the files. Check if the includes are really correct and if possible could you just send the includes part of all the header files so I can analyze the situation better? Note that the way I said only files . h work, files . c do not. To compile files . c you can include them in main. c or write it when compiling. Example in gcc: gcc -o main. c arquivo1.c arquivo2.c arquivon. c.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.