Compiling Multiple C Files with G
When inheriting poorly written C code, you may encounter multiple .cpp and .h files. This presents the question of whether a makefile is necessary or if the g main.cpp command still suffices.
Can You Still Use g main.cpp?
If the classes have been properly separated into .h and .cpp files, you can still use the g main.cpp command. However, you must specify each additional .cpp file after main.cpp.
Compilation Command:
g main.cpp other.cpp etc.cpp
Alternatively, Incremental Compilation and Linking
Another option is to compile each .cpp file individually, resulting in multiple ".o" files. These ".o" files must then be linked together to create the executable.
Compilation Step:
g -c main.cpp (compile main.cpp only)
g -c other.cpp (compile other.cpp only)
g -c etc.cpp (compile etc.cpp only)
Linking Step:
g main.o other.o etc.o -o executable_name (link all ".o" files into the executable)
The above is the detailed content of Can I Still Use `g main.cpp` When Compiling Multiple C Files?. For more information, please follow other related articles on the PHP Chinese website!