Skip to main content

I have a source file that is used by other sources so it has a header file. If in the header, there are types used that needs inclusion of other headers, I'm better to include this header in its source file to avoid duplicating header inclusions.

However, if in the header, there are only primitive types used, it will compile without including it in its source.

I feel like it is always good to include the source's header anyway, although it seems redundant.

What are the best practices here?

EDIT:

Example obj.h:

int add(int a, int b);

Example obj.c:

//#include "obj.h" should be included or not?

int add(int a, int b) {
  return a + b;
}

main.c:

#include "obj.h"

int main() {
  int c = add(1, 2);
}

I have a source file that is used by other sources so it has a header file. If in the header, there are types used that needs inclusion of other headers, I'm better to include this header in its source file to avoid duplicating header inclusions.

However, if in the header, there are only primitive types used, it will compile without including it in its source.

I feel like it is always good to include the source's header anyway, although it seems redundant.

What are the best practices here?

EDIT:

Example obj.h:

int add(int a, int b);

Example obj.c:

//#include "obj.h" should be included or not?

int add(int a, int b) {
  return a + b;
}

main.c:

#include "obj.h"

int main() {
  int c = add(1, 2);
}

I have a source file that is used by other sources so it has a header file. If in the header, there are types used that needs inclusion of other headers, I'm better to include this header in its source file to avoid duplicating header inclusions.

However, if in the header, there are only primitive types used, it will compile without including it in its source.

I feel like it is always good to include the source's header anyway, although it seems redundant.

What are the best practices here?

Example obj.h:

int add(int a, int b);

Example obj.c:

//#include "obj.h" should be included or not?

int add(int a, int b) {
  return a + b;
}

main.c:

#include "obj.h"

int main() {
  int c = add(1, 2);
}
Next time, please compile your examples first before posting them.
Source Link
Doc Brown
  • 220.3k
  • 35
  • 410
  • 623

I have a source file that is used by other sources so it has a header file. If in the header, there are types used that needs inclusion of other headers, I'm better to include this header in its source file to avoid duplicating header inclusions.

However, if in the header, there are only primitive types used, it will compile without including it in its source.

I feel like it is always good to include the source's header anyway, although it seems redundant.

What are the best practices here?

EDIT:

Example obj.h:

int add(int a, int b);

Example obj.c:

//#include "obj.h" should be included or not?

voidint add(int a, int b) {
  return a + b;
}

main.c:

#include "obj.h"

int main() {
  int c = add(1, 2);
}

I have a source file that is used by other sources so it has a header file. If in the header, there are types used that needs inclusion of other headers, I'm better to include this header in its source file to avoid duplicating header inclusions.

However, if in the header, there are only primitive types used, it will compile without including it in its source.

I feel like it is always good to include the source's header anyway, although it seems redundant.

What are the best practices here?

EDIT:

Example obj.h:

int add(int a, int b);

Example obj.c:

//#include "obj.h" should be included or not?

void add(int a, int b) {
  return a + b;
}

main.c:

#include "obj.h"

int main() {
  int c = add(1, 2);
}

I have a source file that is used by other sources so it has a header file. If in the header, there are types used that needs inclusion of other headers, I'm better to include this header in its source file to avoid duplicating header inclusions.

However, if in the header, there are only primitive types used, it will compile without including it in its source.

I feel like it is always good to include the source's header anyway, although it seems redundant.

What are the best practices here?

EDIT:

Example obj.h:

int add(int a, int b);

Example obj.c:

//#include "obj.h" should be included or not?

int add(int a, int b) {
  return a + b;
}

main.c:

#include "obj.h"

int main() {
  int c = add(1, 2);
}
added 271 characters in body
Source Link
mltm
  • 121
  • 4

I have a source file that is used by other sources so it has a header file. If in the header, there are types used that needs inclusion of other headers, I'm better to include this header in its source file to avoid duplicating header inclusions.

However, if in the header, there are only primitive types used, it will compile without including it in its source.

I feel like it is always good to include the source's header anyway, although it seems redundant.

What are the best practices here?

EDIT:

Example obj.h:

int add(int a, int b);

Example obj.c:

//#include "obj.h" should be included or not?

void add(int a, int b) {
  return a + b;
}

main.c:

#include "obj.h"

int main() {
  int c = add(1, 2);
}

I have a source file that is used by other sources so it has a header file. If in the header, there are types used that needs inclusion of other headers, I'm better to include this header in its source file to avoid duplicating header inclusions.

However, if in the header, there are only primitive types used, it will compile without including it in its source.

I feel like it is always good to include the source's header anyway, although it seems redundant.

What are the best practices here?

I have a source file that is used by other sources so it has a header file. If in the header, there are types used that needs inclusion of other headers, I'm better to include this header in its source file to avoid duplicating header inclusions.

However, if in the header, there are only primitive types used, it will compile without including it in its source.

I feel like it is always good to include the source's header anyway, although it seems redundant.

What are the best practices here?

EDIT:

Example obj.h:

int add(int a, int b);

Example obj.c:

//#include "obj.h" should be included or not?

void add(int a, int b) {
  return a + b;
}

main.c:

#include "obj.h"

int main() {
  int c = add(1, 2);
}
Source Link
mltm
  • 121
  • 4
Loading