![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Expert Programmer
Join Date: Aug 2005
Location: UK
Posts: 862
Rep Power: 0
![]() |
-=[G++ Tutorial]=-
What is this tutorial going to teach me? This short tutorial is designed to show you how to use GNUs g++ to compile your C++ programs effectively. General FAQ What is GNU? GNU stands for GNU is Not Unix. To cut a long story short, it, combined with the Linux kernelmake the operating GNU/Linux AKA Linux. GNU make a lot of free software, such as GIMP (GNU Image Manipulation Program), GCC (GNU Compiler Collection) etc. I won't go into detail to much about the great GNU guys it would at least deserve a "tutorial" of its own.What is GCC? GCC is the GNU Compiler Collection, it was originally called the GNU C Compiler because thats what it was, a C compiler. But over time other languages were added, such as C++ (G++, What we will focus on in this tutorial), Fortran (GFortran) , Java (GCJ) and others. With these additions it was renamed GNU Compiler Collection, because it is now a collection of different compilers. So in this tutorial we will learn to use G++, the C++ compiler in the GCC. How do i get this "GCC"? If you are on Linux (wooo!) you should already have GCC installed. If you are on Linux and you don't have GCC, it is down to one of two things. Either you didn't select it as a package to install when you installed linux or you have a crap Distro, such as Ubuntu. LINUX To check you have GCC on Linux, run this: which gcc jack@slackbox:~/files/cpp$ which gcc /usr/bin/gcc If it isn't installed you will see something along the lines of this: jack@slackbox:~/files/cpp$ which gcc which: no gcc in (/usr/local/bin:/usr/bin:/bin:/usr/X11R6/bin:/usr/games:/opt/www/htdig/bin:/usr/lib/java/bin:/usr/lib/java/jre/bin:/opt/kde/bin:/usr/lib/qt/bin:/usr/share/texmf/bin:.) If you don't have GCC installed on Linux (shame on you) you can download it from the GNU website, or (recommended) get it using your package management software. (Fedora : yum, Debian: apt-get, etc.). WINDOWS You don't have GCC installed unless you have already installed it yourself. I haven't used GCC on Windows, because quite frankly, i prefer Linux for most things. Therefore i cannot help you too much in this department. <If someone knows how to get it working on Windows, feel free to post a reply stating how>. I believe you'd need Cygwin (A Linux-liek environment for Windows). ================= If you coninue past here, i assume you have GCC installed correctly. ================= Main Tutorial (I'm going to be using Linux, and explaining everything from a Linux P.O.V. But all the GCC should be the same in MS-DOS.) Preparing the file to compile Firstly just use you favourite text editor (vi) to create a C++ file called "hello.cpp". This is the file we will be working on. NOTE: This is not a C++ tutorial so i will assume you can understand the program below. jack@slackbox:~/files/cpp$ vi hello.cpp Then type in (or Copy/Paste) this code: #include <iostream>
using namespace std;
int main(int argc, char* argv[])
{
cout<<"C++ Program Output"<<endl;
return 0;
}Then save the program and exit the editor. Using GCC Now that we have our file, cd to the directory where you saved it. Then run the following command. jack@slackbox:~$ cd files/cpp/ jack@slackbox:~/files/cpp$ ls hello.cpp jack@slackbox:~/files/cpp$ g++ hello.cpp It will return to another prompt without saying a thing. This means it compiled without any errors. To run the program we use "./". Example: jack@slackbox:~/files/cpp$ ls a.out* hello.cpp jack@slackbox:~/files/cpp$ ./a.out C++ Program Output Cool! We compiled a C++ program ^_^ but many question still lie unanswered such as why it is called a.out and how to change that name. I'm going to make a simple C++ program with an error in it, then try to compile it. #include <iostream>
using namespace std;
int main()
{
cout<<i<<endl;
return 0;
}I'll call it error.cpp and try to compile it. jack@slackbox:~/files/cpp$ ls a.out* error.cpp hello.cpp jack@slackbox:~/files/cpp$ g++ error.cpp error.cpp: In function `int main()': error.cpp:5: error: `i' undeclared (first use this function) error.cpp:5: error: (Each undeclared identifier is reported only once for each function it appears in.) You can see, it tells us that there is an error, where it is and what is wrong. If we saw this error we could go back and correct it. Now lets focus on the output files. Right now it is a.out, which is ok, but we need to rename it to something more descriptive and unique. We use the "-o" flag, it means output. jack@slackbox:~/files/cpp$ g++ hello.cpp -o hello jack@slackbox:~/files/cpp$ ls a.out* error.cpp hello* hello.cpp jack@slackbox:~/files/cpp$ ./hello C++ Program Output Great! It works ^_^. Now we're going to look at some other flags, i'll do i sort of "fact file". Flag: -Wall Description: Shows all warning messages Usage: g++ -Wall file.cpp -o file Flag: -time Description: Times how long it takes to compile. Usage: g++ -time file.cpp -o file Flag: --help Description: Displays a brief help file, its best to look at "man g++" Usage: g++ --help Flag: -v Description: Shows all programs that the compielr invokes. Usage: g++ -v file.cpp -o file Flag: -E Description: PreProcess only. Usage: g++ -E file.cpp -o file Flag: -S Description: Compile only. Usage: g++ -S file.cpp -o file Flag: -c Description: Compile and assemble only. Usage: g++ -c file.cpp -o file Flag: -g Description: Allow the executable to be debugged using GDB. Usage: g++ -g file.cpp To compile programs with multiple files we using the following syntax. g++ file1.cpp file2.cpp file3.cpp To optimise programs we use the -O flag. We can also use the -march flag. g++ -O -march=pentium2 file.cpp To compile a program using external libraries we use the -l flag. For example if i was compiling a OpenGL GLUT program i would use: g++ file.cpp -o file -lglut -lGLU -l GL -lX11 -lm To use math functions we add "-lm". g++ mathyfile.cpp -o mathyfile -lm =============== Some of these descriptions are a bit thin, and for further reading i suggest "man g++" but it has a lot of stuff in it. Have fun, -ColdDeath |
|
|
|
|
#2 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 8,368
Rep Power: 18
![]() ![]() |
GCC/G++ are the compilers that come with Dev-C++, for Windows.
|
|
|
|
|
#3 |
|
Expert Programmer
Join Date: Aug 2005
Location: UK
Posts: 862
Rep Power: 0
![]() |
Thanks DaWei. I wasn't sure if it used Ming or GCC.
|
|
|
|
|
#4 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 8,368
Rep Power: 18
![]() ![]() |
Mingw is, I believe, the library/header collection. Possibly, that's incorrect.
|
|
|
|
|
#5 |
|
Expert Programmer
Join Date: Aug 2005
Location: Rotterdam, the Netherlands
Posts: 942
Rep Power: 8
![]() |
Good tut
![]() One point: you should have included in your list of parameters -pipe and -O2, it are two nice optimizations. (-pipe uses pipes instead of temporary files, I don't know what -O2 does ). |
|
|
|
|
#6 |
|
Programming Guru
![]() Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,329
Rep Power: 10
![]() |
The tradeoff of -pipe is that it uses more memory, but requires less disk space. I'm not sure offhand if it works with windows ports of gcc.
-O2 concerns optimisation of the generated code. Generally, it will result in longer compilation times (and, often, bigger executable files) but the resultant executable will run faster. You might want to include a link to the gcc online documentation which is available for various versions of gcc here. |
|
|
|
|
#7 |
|
I eat cake for breakfast.
![]() ![]() ![]() ![]() Join Date: Jul 2004
Location: In my box.
Posts: 4,428
Rep Power: 13
![]() |
According to the guys on ##c++ on Freenode, Code::Blocks is better than Dev-C++. It should be - it's made by the ex-lead developer of Dev.
![]() |
|
|
|
|
#8 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 8,368
Rep Power: 18
![]() ![]() |
Depends on WHY he's ex-...
![]() |
|
|
|
|
#9 |
|
Newbie
Join Date: Jan 2011
Posts: 1
Rep Power: 0
![]() |
Re: [tutorial] Simple G++ compiler tutorial
"you have a crap Distro, such as Ubuntu". Whys that? Im running it, pretty much, because its cute. I hate windows, but Im an engineer. I cant run altium designer, PC ocilliscopes, and a number of other tools I need to in ubuntu, let alone Linux. Wine comes close, but doesnt do it. Code::blocks runs nice in ubuntu, but programming is the only aspect of my career leaving windows supports. What would be the advatages of switching to Linux from ubuntu when ubuntu doesnt bridge the windows gap? Maybe if I accept a $35,000/year paycut and stick to programming, I can justify leaving windows for open source, but untill that happens or Linux catches up to windows, Im stuck.
|
|
|
|
|
#10 |
|
using std::sarcasm;
|
Re: [tutorial] Simple G++ compiler tutorial
Congratulations on registering for the sole purpose of bumping a topic that's been dead for 5 years and 2 months
__________________
How do you play Religious Roulette? Stand around in a circle and blaspheme till someone gets struck by lightning. |
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|