195

I've just inherited some C++ code that was written poorly with one cpp file which contained the main and a bunch of other functions. There are also .h files that contain classes and their function definitions.

Until now the program was compiled using the command g++ main.cpp. Now that I've separated the classes to .h and .cpp files do I need to use a makefile or can I still use the g++ main.cpp command?

Meir
  • 10,735
  • 19
  • 56
  • 70
  • 1
    `g++ *.cpp` would be enough to compile all the cpp files since the order doesn't matter as long as we're not linking yet. – aderchox Mar 15 '21 at 10:49

11 Answers11

255

list all the other cpp files after main.cpp.

ie

g++ main.cpp other.cpp etc.cpp

and so on.

Or you can compile them all individually. You then link all the resulting ".o" files together.

Goz
  • 58,120
  • 22
  • 114
  • 192
  • 181
    You can even do `g++ *.cpp -o output` – rubenvb Jul 08 '10 at 10:52
  • 8
    Is this anyhow a bad practice? I'd like to use this in my makefile. – gabriel_vincent Sep 26 '13 at 16:01
  • 11
    @gabriel_vincent, it's not necessarily bad practice, but it will mean that editing one file in a project of hundreds will cause the compiler to redo all the work for all the files. Compiling separately allows for incremental compilation. – Paul Draper Sep 17 '16 at 21:36
  • 1
    @gabriel_vincent You shouldn't be writing makefiles by hand to begin with. Use a proper build system like CMake instead. – Baum mit Augen Nov 22 '17 at 01:16
  • "Or you can compile them all individually. You then link all the resulting ".o" files together." For those here wondering "Umm how would I do *that*?" See the answer by @FredAKA. – eric Mar 10 '18 at 15:44
  • 5
    @BaummitAugen Yeah, except I just want to compile this tiny 2-file program I've written in 14 minutes, and I'd really prefer to spend 1 minute on writing a makefile instead of an hour setting up cmake. – Przemek D Mar 27 '19 at 07:33
64

To compile separately without linking you need to add -c option:

g++ -c myclass.cpp
g++ -c main.cpp
g++ myclass.o main.o
./a.out
Pritam Banerjee
  • 15,297
  • 10
  • 71
  • 92
FredAKA
  • 968
  • 7
  • 8
  • 3
    Note if you want to control the name of the executable in the linking step just do what you usually do: `g++ -o my_executable myclass.o main.o` – eric Mar 10 '18 at 16:18
23

Now that I've separated the classes to .h and .cpp files do I need to use a makefile or can I still use the "g++ main.cpp" command?

Compiling several files at once is a poor choice if you are going to put that into the Makefile.

Normally in a Makefile (for GNU/Make), it should suffice to write that:

# "all" is the name of the default target, running "make" without params would use it
all: executable1

# for C++, replace CC (c compiler) with CXX (c++ compiler) which is used as default linker
CC=$(CXX)

# tell which files should be used, .cpp -> .o make would do automatically
executable1: file1.o file2.o

That way make would be properly recompiling only what needs to be recompiled. One can also add few tweaks to generate the header file dependencies - so that make would also properly rebuild what's need to be rebuilt due to the header file changes.

Dummy00001
  • 14,958
  • 5
  • 32
  • 56
16

I know this question has been asked years ago but still wanted to share how I usually compile multiple c++ files.

  1. Let's say you have 5 cpp files, all you have to do is use the * instead of typing each cpp files name E.g g++ -c *.cpp -o myprogram.
  2. This will generate "myprogram"
  3. run the program ./myprogram

that's all!!

The reason I'm using * is that what if you have 30 cpp files would you type all of them? or just use the * sign and save time :)

p.s Use this method only if you don't care about makefile.

Pritam Banerjee
  • 15,297
  • 10
  • 71
  • 92
zobair
  • 177
  • 1
  • 3
13

You can still use g++ directly if you want:

g++ f1.cpp f2.cpp main.cpp

where f1.cpp and f2.cpp are the files with the functions in them. For details of how to use make to do the build, see the excellent GNU make documentation.

12

.h files will nothing to do with compiling ... you only care about cpp files... so type g++ filename1.cpp filename2.cpp main.cpp -o myprogram

means you are compiling each cpp files and then linked them together into myprgram.

then run your program ./myprogram

Pritam Banerjee
  • 15,297
  • 10
  • 71
  • 92
Matthew
  • 121
  • 1
  • 2
7

As rebenvp said I used:

g++ *.cpp -o output

And then do this for output:

./output

But a better solution is to use make file. Read here to know more about make files.

Also make sure that you have added the required .h files in the .cpp files.

Pritam Banerjee
  • 15,297
  • 10
  • 71
  • 92
4

You can use several g++ commands and then link, but the easiest is to use a traditional Makefile or some other build system: like Scons (which are often easier to set up than Makefiles).

gauteh
  • 14,017
  • 3
  • 25
  • 32
2

If you want to use #include <myheader.hpp> inside your cpp files you can use:

g++ *.cpp -I. -o out
Matias Haeussler
  • 793
  • 2
  • 9
  • 20
1

I used to use a custom Makefile that compiled all the files in current directory, but I had to copy it in every directory I needed it, everytime.

So I created my own tool - Universal Compiler which made the process much easier when compile many files.

Shubham Chaudhary
  • 36,933
  • 9
  • 67
  • 78
-3

~/In_ProjectDirectory $ g++ coordin_main.cpp coordin_func.cpp coordin.h

~/In_ProjectDirectory $ ./a.out

... Worked!!

Using Linux Mint with Geany IDE

When I saved each file to the same directory, one file was not saved correctly within the directory; the coordin.h file. So, rechecked and it was saved there as coordin.h, and not incorrectly as -> coordin.h.gch. The little stuff. Arg!!