LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Getting my existing c++ programs to work with the g++ compiler (https://www.linuxquestions.org/questions/programming-9/getting-my-existing-c-programs-to-work-with-the-g-compiler-160724/)

marz 03-21-2004 05:52 PM

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
:confused:

tvn 03-21-2004 08:53 PM

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

aluser 03-21-2004 11:19 PM

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.

marz 03-22-2004 05:53 AM

Thanx Alot! :D


All times are GMT -5. The time now is 12:06 PM.