Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it a good practice to define C++ functions inside header files? [duplicate]

I'm wondering if it's a good practice to store C++ regular functions, not methods(the ones in classes) inside header files.

Example:

#ifndef FUNCTIONS_H_INCLUDED #define FUNCTIONS_H_INCLUDED  int add(int a, int b) {    return a + b; }  #endif 

And Use it like this:

#include <iostream> #include "Functions.h"  int main(int argc, char* args[]) {     std::cout << add(5, 8) << std::endl;     return 1; } 

Is this a good a good practice? Thanks in advance!

like image 972
Nobody Avatar asked Aug 12 '14 21:08

Nobody


People also ask

Can you define a function inside a header file?

The answer to the above is yes. header files are simply files in which you can declare your own functions that you can use in your main program or these can be used while writing large C programs. NOTE:Header files generally contain definitions of data types, function prototypes and C preprocessor commands.

What happens if we include a header file twice in C?

If a header file happens to be included twice, the compiler will process its contents twice. This is very likely to cause an error, e.g. when the compiler sees the same structure definition twice. Even if it does not, it will certainly waste time. This construct is commonly known as a wrapper #ifndef.

Can header file contains function definition in C?

A header file is a file with extension . h which contains C function declarations and macro definitions to be shared between several source files.

What should be included in header file in C?

The header file contains only declarations, and is included by the . c file for the module. Put only structure type declarations, function prototypes, and global variable extern declarations, in the . h file; put the function definitions and global variable definitions and initializations in the .


1 Answers

If you want to use a function in multiple source files (or rather, translation units), then you place a function declaration (i.e. a function prototype) in the header file, and the definition in one source file.

Then when you build, you first compile the source files to object files, and then you link the object files into the final executable.


Example code:

  • Header file

      #ifndef FUNCTIONS_H_INCLUDED   #define FUNCTIONS_H_INCLUDED    int add(int a, int b);  // Function prototype, its declaration    #endif 
  • First source file

      #include "functions.h"    // Function definition   int add(int a, int b)   {       return a + b;   } 
  • Second source file

      #include <iostream>   #include "functions.h"    int main()   {       std::cout << "add(1, 2) = " << add(1, 2) << '\n';   } 

How you build it depends very much on your environment. If you are using an IDE (like Visual Studio, Eclipse, Xcode etc.) then you put all files into the project in the correct places.

If you are building from the command line in, for example, Linux or OSX, then you do:

$ g++ -c file1.cpp $ g++ -c file2.cpp $ g++ file1.o file2.o -o my_program 

The flag -c tells the compiler to generate an object file, and name it the same as the source file but with a .o suffix. The last command links the two object files together to form the final executable, and names it my_program (that's what the -o option does, tells the name of the output file).

like image 72
Some programmer dude Avatar answered Sep 19 '22 02:09

Some programmer dude