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.

5
  • not really colossal and I wouldnt call it stupidity, but yeah its the latter. Dont include source files. Looking for a dupe.... Commented Jun 14, 2019 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 is becoming very popular and is quite easy to learn too. I highly recommend spending some time with it. Commented Jun 14, 2019 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. Commented Jun 14, 2019 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. Commented Jun 14, 2019 at 13:46
  • related: stackoverflow.com/questions/6923961/source-file-and-header-in-c Commented Jun 14, 2019 at 13:47

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

Comments

-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.

1 Comment

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.)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.