Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 314 Vote(s) - 3.53 Average
  • 1
  • 2
  • 3
  • 4
  • 5
#include in .h or .c / .cpp?

#1
When coding in either C or C++, where should I have the `#include`'s?

callback.h:

#ifndef _CALLBACK_H_
#define _CALLBACK_H_

#include <sndfile.h>
#include "main.h"

void on_button_apply_clicked(GtkButton* button, struct user_data_s* data);
void on_button_cancel_clicked(GtkButton* button, struct user_data_s* data);

#endif


callback.c:

#include <stdlib.h>
#include <math.h>

#include "config.h"

#include "callback.h"
#include "play.h"

void on_button_apply_clicked(GtkButton* button, struct user_data_s* data) {
gint page;
page = gtk_notebook_get_current_page(GTK_NOTEBOOK(data->notebook));

...


Should all includes be in either the .h or .c / .cpp, or both like I have done here?

Reply

#2
Put as much as you can in the `.c` and as little as possible in the `.h`. The includes in the `.c` are only included when that one file is compiled, but the includes for the `.h` have to be included by every file that uses it.
Reply

#3
If I `#include <callback.h>`, I don't want to have to `#include` lots of other header files to get my code to compile. In `callback.h` you should include everything needed to compile against it. But nothing more.

Consider whether using forward declarations in your header file (such as `class GtkButton;`) will suffice, allowing you to reduce the number of `#include` directives in the header (and, in turn, my compilation time and complexity).
Reply

#4
Put as many includes in your cpp as possible and only the ones that are needed by the hpp file in the hpp. I believe this will help to speed up compilation, as hpp files will be cross-referenced less.

Also consider using [forward declarations][1] in your hpp file to further reduce the include dependency chain.


[1]:

[To see links please register here]

Reply

#5
The only time you should include a header within another .h file is if you need to access a type definition in that header; for example:

#ifndef MY_HEADER_H
#define MY_HEADER_H

#include <stdio.h>

void doStuffWith(FILE *f); // need the definition of FILE from stdio.h

#endif

If header A depends on header B such as the example above, then header A should include header B directly. Do **NOT** try to order your includes in the .c file to satisfy dependencies (that is, including header B before header A); that is a big ol' pile of heartburn waiting to happen. I mean it. I've been in that movie several times, and it always ended with Tokyo in flames.

Yes, this can result in files being included multiple times, but if they have proper include guards set up to protect against multiple declaration/definition errors, then a few extra seconds of build time isn't worth worrying about. Trying to manage dependencies manually is a pain in the ass.

Of course, you shouldn't be including files where you don't *need* to.
Reply

#6
I propose to simply include an All.h in the project that includes all the headers needed, and every other .h file calls All.h and every .c/.cpp file only includes its own header.
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through