How to use GCC from console?

I'd like to know how to compile with gcc in a terminal just in case if I need to...so any advice?
Last edited on
For just one file,
g++ -W -Wall -Wextra -pedantic -std=c++11 -O3 -march=native -g -o filename sourcefile.cc
(that -std=c++11 may be replaced by -std=c++0x for pre-4.7 versions, or by -ansi if you need to limit the compiler to the 2003 C++)

For more than one file, write a makefile.
Last edited on
That's another question I have. How do makefiles work exactly? And how would I create one? Sorry for the beginner question...I'm relatively new to the language.
Makefiles aren't part of the (C++) language, they are part of Unix development environment. A makefile is a text file that contains instructions to the program called "make", essentially lists of commands to run in order to build (or install, or do something else to) the project.

The language of makefiles is fairly complicated in general case, which is why reading makefiles from other programs is not going to be helpful, but hunt around for tutorials and documentation: simple makefiles are, well, simple. Also check out alternatives, I liked the python-based program called scons.
Could I not just use g++ directly without a makefile to compile multi-file programs? I just did and it works...
Sure. Add a few more files and you may want to at least write a a shell script that runs the command(s) you just typed... Or at least a text file to paste from. I suppose until you need something that tracks dependencies, you may be fine with that.
aha I won't be needing to track dependencies for a while. And once I do need to do so, I'll be more than comfortable with makefiles. ;-)
Last edited on
The reason to use makefiles is to avoid recompilation.
As long as the source or some dependency (include) does not get modified, you don't need to compile that file, but just link it.

$ g++ -c *.cpp #compiling
$ g++ *.o -o program.bin #linking


In order to generate the dependencies, you could
$ g++ -MM *.cpp
Last edited on
Topic archived. No new replies allowed.