0

As i said in the title i want to know if is an error to include .cpp files in their respectively .h

I'm compiling using cygwin on windows. The g++ compiler was installed in cygwin using apt-cyg install g++.
Then i added the cygwin path to env and now the g++ command is reachable from a windows cmd.

Here's a basic example to reproduce the problem.

main.cpp

#include <iostream>
#include "tezt.h"

int main(int argc, char const *argv[])
{
    tezt();
    return 0;
}

tezt.h

#ifndef TEZT_H
#define TEZT_H

void tezt();

#endif

tezt.cpp

#include <iostream>
#include "tezt.h"

void tezt() 
{
    std::cout<<"tezt"<<std::endl;
}

If i compile using simply g++ main.cpp i get errors.
So in order to compile correctly i should type g++ main.cpp tezt.cpp or define libraries.
It can be a trivial task when managing with multiple .cpp files stored in different folders.


So i'm proposing this approach:
if i modify the content of tezt.h, like follow, compilation is successfully! It can be a good "workaround" or it's not recommended? It's a colossal stupidity?

#ifndef TEZT_H
#define TEZT_H
#include "tezt.cpp"

void tezt();

#endif

Thanks for the attention.

Roberto Manfreda
  • 2,345
  • 3
  • 25
  • 39
  • not really colossal and I wouldnt call it stupidity, but yeah its the latter. Dont include source files. Looking for a dupe.... – 463035818_is_not_an_ai Jun 14 '19 at 13:45
  • It wont work if you include the same `.h` file in more than one `.cpp` file. So, no. Don't do that. Use a build system instead. [Meson](https://mesonbuild.com/) is becoming very popular and is quite easy to learn too. I highly recommend spending some time with it. – Nikos C. Jun 14 '19 at 13:45
  • How to manage larger multi-sourcefile projects depends on your platform and your own preferences. If you like integrated environments then an IDE is a good choice. If you want command-line only then on Linux systems the `make` program and `Makefile` is something you need to learn. There are many other build-systems available. – Some programmer dude Jun 14 '19 at 13:45
  • I not really sure what you want, but if you want the implementation in a single file, then problably you want to put everything in the header. – hetepeperfan Jun 14 '19 at 13:46
  • related: https://stackoverflow.com/questions/6923961/source-file-and-header-in-c – 463035818_is_not_an_ai Jun 14 '19 at 13:47

2 Answers2

2

Generally it is a bad idea to have an implementation in the header file. if you include tezt.h file in different files in the same project it may have compilation error, with multiple definitions for the same symbol.

zapredelom
  • 1,009
  • 1
  • 11
  • 28
-1

Including *.cpp works.

But the real solution is to use the tool make.

Create a makefile, which defines the dependencies, then call make, which then calls the compiler appropriatly.

A.Franzen
  • 725
  • 4
  • 10
  • 2
    `make` just automates build steps that you can run by hand. In and of itself, a Makefile doesn't buy you anything. (Also, there are other build tools.) – melpomene Jun 14 '19 at 13:55