-2

I have these files: classA.cpp, classA.h, classB.cpp, classB.h, and main.cpp.
All needed libraries are included in both .h files.

In main.cpp I include classA.cpp and classB.cpp.
In classB.cpp I include classB.h and in classA.cpp it is classA.h

I compile it by

g++ main.cpp

(+some unimportant stuff) and it is working perfectly.

But I am almost certainly sure, that on our lectures we were told to do that differently, sadly I can't find it now.
Is this the best way of including and compiling? If not, what is?

Smeagol
  • 593
  • 9
  • 23
  • 1
    Why are you including cpp files from other cpp files? You've created one gigantic translation unit. Which C++ book are you using? This is all explained inside it. – Lightness Races in Orbit May 08 '17 at 13:45
  • @BoundaryImposition I have no book, I am studying at university, sadly we only touched this topic yet. – Smeagol May 08 '17 at 13:46
  • 2
    @TGar You can always learn things outside of class. The Internet has many many resources. –  May 08 '17 at 13:47
  • So I should include only .h files? It will now where to find the rest? Sorry until now I always have all my programs in single file. – Smeagol May 08 '17 at 13:47
  • 3
    _"I have no book"_ There's your problem then. No book, no lecture notes... how do you expect to learn without a learning resource! – Lightness Races in Orbit May 08 '17 at 13:47
  • @BoundaryImposition I didn't have problem until now, I use cppreference.com. – Smeagol May 08 '17 at 13:48
  • 3
    You can't learn the language from a reference. Here are some books: http://stackoverflow.com/q/388242/560648 "How to include" is too broad for an SO Q&A. Good luck! – Lightness Races in Orbit May 08 '17 at 13:48
  • http://stackoverflow.com/q/1686204/560648 actually may be useful – Lightness Races in Orbit May 08 '17 at 13:51
  • Include the header files instead of cpp files, then do: `g++ main.cpp classA.cpp classA.h classB.cpp classB.h` [see this doc](http://stackoverflow.com/documentation/c%2b%2b/4708/compiling-and-building#t=201705081359124744663) for more details – Jahid May 08 '17 at 13:56
  • http://stackoverflow.com/questions/1686204/why-should-i-not-include-cpp-files-and-instead-use-a-header – AresCaelum May 08 '17 at 14:05
  • 1
    Questions asking for "the best way" tend to be viewed as being "primarily opinion-based". What's best in one situation is not necessarily best anywhere else! – Toby Speight May 08 '17 at 14:39

2 Answers2

2

the simply way:g++ main.cpp ClassA.cpp ClassB.cpp etc.cpp more advanced way you should use a makefile. enter link description here

kfc
  • 567
  • 1
  • 5
  • 10
  • You should probably also mention to include the headers in main.cpp not the cpp files.. – Jahid May 08 '17 at 14:12
  • I would say it really isn't the answer as it doesnt "answer" your question which was `Is this the best way of including and compiling? If not, what is?` This doesn't explain at all anything about including. – AresCaelum May 08 '17 at 14:12
-1

Normally you don't include source files into other source files. In fact, the reason we have header files is so that we can use the same declarations in all files, whilst compiling definitions only once.

So your main.cpp should include classA.h and classB.h instead of classA.cpp and classB.cpp:

#include "classA.h"
#include "classB.h"

#include <algorithm> // and whatever else you need

int main()
{
    // your code....
}

Then write a simple Makefile:

CXXFLAGS += -Wall -Wextra

main: main.o classA.o classB.o
    $(LINK.cpp) $(OUTPUT_OPTION) $^

main.o: main.cpp
main.o: classA.h
main.o: classB.h

classA.o: classA.cpp classA.h

classB.o: classB.cpp classB.h

For bigger projects, you can write a rule to create the dependencies from the source files automatically, but we can start as above by hand-generating them.

Now, the correct commands will be run when you execute

make
Toby Speight
  • 27,591
  • 48
  • 66
  • 103