So you're willing to write your own C or C++ programs, or simply compile someone else's code? This guide will get you started...
This FAQ assumes you're trying to compile error-free code. To actually learn the C or C++ languages, see the How to start programming thread.
If you encounter compilation problems, you may want to check this thread: Troubleshooting common C and C++ compilation errors.
0. Make sure the build-essential package is installed
This package installs the essential tools needed to build C and C++ programs. Don't try to install individual tools by yourself, experts made that package for a good reason...
Code:
sudo apt-get install build-essential
1.a Compiling your first C program
Copy & paste this code into a new file. Save the file as main.c
Code:
#include <stdio.h>
int main()
{
printf("Hello World!\n");
return 0;
}
Open a terminal, go to the directory where you saved main.c, and type:
If all went fine, nothing is printed and you get back to the shell. Now, run it:
"Hello World!" gets printed in the terminal.
Explanations:- gcc is the C compiler
- obviously, main.c means that the compiler must compile this file. If you want to compile several source files into a single executable, just list them all here.
- -o test means "output the result of the compilation to an executable file named test". If this part is omitted, the executable will be named a.out by default.
- ./test runs the newly compiled executable. The ./ is needed because of the way the shell works (the current directory . is not in the PATH by default).
1.b Compiling your first C++ program
Most of what I just said regarding the compilation of C programs also apply to C++ programs.
The two main differences (besides the language itself) are:
- g++ is the compiler that you must use.
- you should use a .cpp file extension rather than a .c one
Code:
#include <iostream>
int main()
{
std::cout << "Hello World!" << std::endl;
return 0;
}
Code:
g++ main.cpp -o test
./test
2. Paranoid programming is good for your health!
Many beginners want to avoid compiler errors and warnings at any cost, which often leads them to ignore the "cryptic" messages the compiler outputs and to tinker anxiously with the relevant line until the error goes away.
Don't fall in that trap! The compiler is your friend, the messages it outputs are kind advices rather than punishments. You should really take the time to try and understand those messages, this is a very good way to improve your code's quality.
As a matter of fact, most seasoned programmers set the compiler's warning level as high as possible, so that it catches the little errors they make when they aren't paying much attention.
Here are a few command line switches to enable some warnings and language features (check the manual for detailed information):- -std=gnu99 (C only) enforces the latest C standard (1999), plus GNU extensions (must be explicitely specified)
- -std=gnu++98 (C++ only) enforces the 1998 C++ standard (but the STL now supports most of C++0x TR1 anyway), plus GNU extensions (this is the default for C++ code)
- -Wall enables most warnings (very useful)
- -Wextra enables more warnings (very useful)
- -Wwrite-strings warns when you misuse plain old C string constants (aka. deprecated cast from const char* to char*) (very useful) (see this discussion for more in-depth information)
- -pedantic issues warnings when strict ISO compatibility is not met (the name says it all: pedantic, but it can be useful once in a while)
- -Woverloaded-virtual (C++ only) warns when virtual functions are overloaded in derived classes (very useful)
- -Weffc++ (C++ only) warns when some key point of Scott Meyer's Efficient C++ and More Efficient C++ books are not met (good practice)
- -Wold-style-cast (C++ only) warns when you use old-style C casts in a C++ program (good practice, you should be using the new-style C++ casts)
3. Useful tips- the -g flag instructs the compiler to include debugging information, so that you can debug your program with gdb itself or any gdb frontend.
- sudo apt-get install manpages-dev to help you make sense of compiler warnings and errors.
4. Read the manual!
Plain old manpage:
HTML version of the gcc manual:
Code:
sudo apt-get install gcc-doc
Once installed, point your browser to /usr/share/doc/gcc-doc/gcc.html
Thanks to so many people @UF for their very useful help, this FAQ wouldn't be the same without them (you know who you are ).
Bookmarks