LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-17-2004, 07:22 AM   #1
marlor
Member
 
Registered: May 2004
Distribution: Slackware C
Posts: 274

Rep: Reputation: 30
C++ beginner seeking help with compiling.


hi people,

i have finally purchased a c++ book. to be more specific "the c++ programming language " by the creator of c++

well i use both windows and linux, but i want to start my c++ experience on linux.

i want to write and try the example codes in the book to get an idea of everything.

Q: can anyone let me know how to compile c++ code with linux. and what i should expect as the output created. (like extra files created).
and how i can run that script/program.
(i can't wait to get started)


thank you in advance
 
Old 09-17-2004, 07:47 AM   #2
WhiteChedda
Member
 
Registered: Aug 2003
Location: Florida
Distribution: Mandrake 9.1 for now
Posts: 205

Rep: Reputation: 30
Re: C++ beginner seeking help with compiling.

Quote:
Originally posted by marlor
hi people,

i have finally purchased a c++ book. to be more specific "the c++ programming language " by the creator of c++

well i use both windows and linux, but i want to start my c++ experience on linux.

i want to write and try the example codes in the book to get an idea of everything.

Q: can anyone let me know how to compile c++ code with linux. and what i should expect as the output created. (like extra files created).
and how i can run that script/program.
(i can't wait to get started)


thank you in advance
All of this will depend in how you invoke the compiler. You can tell it to compile and link to an executable program, or just create lib or just output a source file after the preprocessor has run though it etc...

http://annys.eines.info/cgi-bin/man/man2html?gcc+1
The link above lists the man page for gcc, which is not the GNU C Compiler, but rather the GNU Compiler Collection and covers several languages, including C++.

The most common way is to use g++ instead of gcc though, g++ will call gcc with the appropriate options to compile C++ source code for you so you do not have to remember to use -x c++ or whatever it is these days.

Read over the command line options fo rusing gcc and try to locate the man pages for g++ [you should be able to use man gc and man g++ from a terminal in linux and get the same info.
 
Old 09-17-2004, 08:04 AM   #3
marlor
Member
 
Registered: May 2004
Distribution: Slackware C
Posts: 274

Original Poster
Rep: Reputation: 30
thanks for the reply,

yes the man pages i know, i just wanted to get a quick intro to compile with g++ because i just want to get the taste of it first compiling a very very simple program like "hello world". of course afterwards i am planning to read everything about g++ and the others. but as i said i just wanted to the the taste first. that's why i asked the specific questions

thanks again
 
Old 09-17-2004, 09:21 AM   #4
dakensta
Member
 
Registered: Jun 2003
Location: SEUK
Distribution: Debian & OS X
Posts: 194

Rep: Reputation: 35
type into a text editor

Code:
#include <iostream>
using namespace std;

int main()
{
  std::cout << "Hello World\n";
}
and save the file as hello.cpp

Now at the command line, in the same directory, type:
g++ hello.cpp -o hello
There should be no output and an executable file called hello should be generated.

run it by typing ./hello and marvel at your creation

Quote:
to be more specific "the c++ programming language " by the creator of c++
I don't want to discourage you and this is an excellent book that every c++ programmer should either own or have access to BUT is not the best book for learning from.

Personally I recommend "The Complete Idiots Guide to C++" by Paul Snaith for teaching you the basics and ask away here for any issues with actually compiling source code.

The was a thread just recently discussing free on-line books and tutorials too.
http://www.linuxquestions.org/questi...hreadid=230368
http://www.linuxquestions.org/questi...hreadid=230176
Essentially:

g++ sourcefile1.cpp sourcefile2.cpp -o program_name

plus switches for use later (dont use the < and > though!):
-o <program_name> used above, specifies the output file, small letter 'o'
-Wall to turn all warnings on to help you find mistakes
-g to turn debugging info on to help you debug your programs
-O<n> with n = optimization level, capital letter 'O'
-I<some_directory> to specify a directory to find include files, capital 'I' (i)
-L<some_directory> to specify a directory containing libraries
-l<some_library> to specify a library, small letter 'l' (L)

(If you don't fully understand, you will later)
Remember these are gcc specific, other compilers vary.

Good luck!

Last edited by dakensta; 09-17-2004 at 09:27 AM.
 
Old 09-17-2004, 09:31 AM   #5
marlor
Member
 
Registered: May 2004
Distribution: Slackware C
Posts: 274

Original Poster
Rep: Reputation: 30
thanks a lot datensta,

well i am starting c++ this yeear in school. so the basics will be tought there. but i just wanted to have a head start from the lessons.
 
Old 09-17-2004, 09:41 AM   #6
dakensta
Member
 
Registered: Jun 2003
Location: SEUK
Distribution: Debian & OS X
Posts: 194

Rep: Reputation: 35
Actually one other point in terms of the source code I wrote:

Many tutorials will put write something like

#include <iostream.h>

whereas I have put:

#include <iostream>
using namespace std;

Mine is technically correct but many texts pre-date the 1998 'official standard' before which many people used the .h thing.

For now, don't worry about it, do whatever your teacher does.

Secondly, you might see the same code but with "return 0;" written before the last curly bracket. Again don't worry ... it is one of the few things that is optional ... do what your teacher does.

HTH

(I just had a quick look at the tutorial in the first link and thought it worth pointing these things out ... I'm not really trying to trip you up )
 
Old 09-17-2004, 09:46 AM   #7
marlor
Member
 
Registered: May 2004
Distribution: Slackware C
Posts: 274

Original Poster
Rep: Reputation: 30
hehe thanks for the help, i just want to explore a bit before classes start.
just eager (if that's the word i wanted to use) to learn
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Seeking a suitable distro... need help Drakkath Linux - General 2 04-23-2005 04:37 PM
Seeking Fedora Installation Help... haggard Fedora - Installation 21 02-26-2004 01:18 PM
Seeking directions jk2silly Linux - Newbie 3 10-20-2003 10:05 AM
Seeking a Joyboard Dave Skywatcher General 0 03-10-2003 10:56 PM
new to linux - seeking help stuimrozsm Linux - Newbie 4 12-18-2002 08:17 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:36 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration