#Problem with including

12 messages ยท Page 1 of 1 (latest)

fallow swiftBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question run !howto ask.

broken moat
#

Do not define functions in the header guards, unless it's inline functions

#

And use header guards to prevent repeated includes

#

If you have a main.h it already suggests something might be not quite right

quasi iris
#

If providing an inline definition, also be aware you must define it in a .c file too

#

static functions are also ok in headers

fallow swiftBOT
#
What Are the File Types in C/C++
Source Files: `.c|.cpp|.cxx|...`
  • function definitions
  • global variable definitions
  • anything used only in that file
    Each file is compiled independently, and in parallel. Don't #include them, but compile each.
Header Files: `.h|.hpp|.tpp|...`
  • forward-declarations/prototypes
  • type declarations
  • templates
    Headers are #included into other source/header files. You will need include guards to prevent redefinition errors.
Translation Units and Lesser Known File Extensions

Each source file becomes a translation unit during translation (expanding macros, remapping unicode characters, etc.)
Sometimes .ipp or .tpp for C++ are used for internal headers with function definitions.

warm quiver
#

it has a pretty easy fix,

#

animal.h:
#ifndef ANIMAL_H
#define ANIMAL_H

void some_fn(){

}

#endif

lion.h:
#include "animal.h"
#ifndef LION_H
#define LION_H

void some_lion_fn(){
some_fn();
}

#endif

main.c:
#include "animal.h"
#include "lion.h"

int main(){

return 0;

}

#

#ifndef LION_H
#define LION_H

// code

#endif

#

you should always do this in a header file, this will not redefine fucntions if files are included multiple times

#

ifndef expands to if not defined