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 03-21-2004, 05:52 PM   #1
marz
LQ Newbie
 
Registered: Mar 2004
Posts: 6

Rep: Reputation: 0
Question Getting my existing c++ programs to work with the g++ compiler


Hi, I'm a newbie

I'd like to know how to modify my existing c++ files to work with the g++/gcc compiler. I jus got linux red hat 9.0 (shrike) and every time i try to run my programs nothing happens. example

i just learned that how to use the cin and cout statements with g++ (i used Turbo C++ on my windows machine), so i have this file "helloworld.cpp" that i made to test out g++ and it has:
Code:
#include <iostream> //iostream.h deprecated
using namespace std; //must have to use cin and cout instead ot std.cin

int main(void)
{
int number[5];

cout<<"Hello World.Enter a number (5):";
cin>>number;
cout<<number;

return 0;
}
and when i type "g++ helloworld.cpp" nothing happens. then i go into the folder and there is another file called "a.out" and the "helloworld.cpp"

i'd really like to why this is and/or what i'm doing wrong

Last edited by marz; 03-21-2004 at 05:59 PM.
 
Old 03-21-2004, 08:53 PM   #2
tvn
Member
 
Registered: Mar 2004
Posts: 96

Rep: Reputation: 15
a.out is the default output file , you can run it in the follow ways:
1) cd to the dir where it is at, then type ./a.out
2) use its whole path, e.g /home/you/where_the_file_is/a.out
 
Old 03-21-2004, 11:19 PM   #3
aluser
Member
 
Registered: Mar 2004
Location: Massachusetts
Distribution: Debian
Posts: 557

Rep: Reputation: 43
You can give a name for the executable like this:
Code:
g++ -o helloworld helloworld.cpp
It's also a Good Idea to compile with -Wall, as this gives warnings about lots of questionable constructs:

Code:
g++ -Wall -o helloworld helloworld.cpp
I always cringe when somebody retypes the gcc/g++ command line over and over again: use a Makefile. Just write something like this into a file called Makefile in the same directory as your code:

Code:
CXX = g++
all: helloworld
helloworld: helloworld.cpp
	$(CXX) -Wall -o helloworld helloworld.cpp
*NOTE: That must be a TAB character before $(CXX), not spaces!

Once you do this, you can just type

Code:
make
to build the executable, and g++ will only be run if helloworld.cpp was modified after helloworld. Your project can get much larger and compile into separate object files with special flags and so on, but if you keep your Makefile good, all you ever have to do to compile it is type "make". Makefiles get much cooler and much more complicated: type "info make" to read about it. You might want to "info info" first if you haven't used the info doc system before.
 
Old 03-22-2004, 05:53 AM   #4
marz
LQ Newbie
 
Registered: Mar 2004
Posts: 6

Original Poster
Rep: Reputation: 0
Thumbs up

Thanx Alot!
 
  


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
C compiler cannot run C compiled programs petero Programming 8 05-27-2005 11:35 AM
why gun compiler doesn't work michaelwu Linux - Software 1 04-22-2005 02:50 AM
how do I compile programs from source with a secondary compiler? jarobman Programming 3 02-16-2005 05:57 PM
How can I set my Suse 9.0 system to work as a router on an existing network? ww6y Linux - Networking 1 02-04-2004 09:08 AM
Getting g++ to work, or any compiler Kompressor Linux - Newbie 3 06-10-2002 07:47 PM

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

All times are GMT -5. The time now is 12:51 AM.

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
Open Source Consulting | Domain Registration