1

I'm doing the "Hello World" in the GTKMM tutorial, the "app" uses three files, the main.cc, helloworld.h and helloworld.cc.

At the beginning I thought that compiling the main.cc :

g++ -o HW main.cc $(pkg-config ... )

would be enough, but gives an error (undefined reference to Helloworld::Helloworld), etc.

In other words, it compiles the main and the header, but not the HW class, and this makes sense because the header is included in Main but not the Helloworld.cc. The thing is I'm kinda scared of including it because I read in other question that "including everything was a bad practice".

That being said, when I compile using all the files in the same command:

g++ -o HW main.cc helloworld.cc $(pkg-config ... )

the "app" works without errors.

So, since using the last command works, is compiling in this way a good practice?

What happens if my app uses a big ton of classes?

Must I manually write them all down in the command?

If not, must I use #include?

Is it good practice using #include for all cc used files?

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
GNSYS
  • 11
  • 1

3 Answers3

2

Is normal to list all the cpp/cc files when compiling with g++?

Yes, completely.

How else will it know what source code you want it to compile?

The thing is I'm kinda scared of including it because I read in other question that including everything was a bad practice.

#includeing excess headers is bad practice.

Passing your complete source code to the compiler is not.

Is it good practice using #include for all cc used files?

Absolutely not.

What happens if my app uses a big ton of classes? Must I manually write them all down in the command?

No. You should be using a build system that handles this for you. That could be an IDE which takes all the files in your project and passes them to the compiler in turn, or it could be a CMakeLists.txt/Makefile with a *.cpp wildcard in (although I actually recommend listing source files explicitly, one-by-one; it's not hard).

Invoking g++ manually on the command-line is fine for a quick test, but for real usage you don't want to be clowning around with such machinery.

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
1

is good practice using #include for all cc used files

It's not only bad practice, never do it.

In order to create an executable you actually have to do two things:

  1. Compile all the source code files to object files or libraries.

  2. Link all the object files and needed libraries into an executable.

You seem to be missing the point that the link phase is where symbols defined in separate source files are resolved or linked.

Paul Evans
  • 27,315
  • 3
  • 37
  • 54
0

Must I manually write them all down in the command?

For the compiler to know about the DEFINTION of the symbols DECLARED in your headers, you must include all source files. Exceptions to this rule can be (but are not limited to) headers containing template metaprogramming (TMP) code that usually exist entirely in header files.

What happens if my app uses a big ton of classes?

Most of the large C++ projects utilize build configuration tools such as CMAKE to handle the generation of makefiles for them.