Search code examples
c++compilationheadermakefile

Using G++ to compile multiple .cpp and .h files


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?


Solution

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