PDA

View Full Version : how to execute a c++ file


IsKall
June 16th, 2006, 05:37 PM
How do I execute a hello.cc file from the terminal?

It works perfectly from Anjuta but I want to learn to execute from the terminal too.

I tried all these:


gcc hello.cc
g++ hello.cc

gcc hello.o
g++ hello.o


but it doesn't show Hello World! (just a easy cout thing to see if the execution works in the terminal).

Jessehk
June 16th, 2006, 06:01 PM
First, create a file (I'll call mine hello.cpp)


#include <iostream>

int main() {
std::cout << "Hello, world!" << std::endl;
}


Then compile with


g++ hello.cpp -o hello


Run the executable with


./hello


:)

IsKall
June 16th, 2006, 07:53 PM
I know how to program ;) I am just new to program in linux enviroment. :)

Thank you for the help Jessehk!

IsKall
July 29th, 2006, 04:17 PM
how do I compile multiple .cc and a .h files together?

Lord Illidan
July 29th, 2006, 04:23 PM
You need makefiles for that. Google up some tutorials..I never made one myself.

IsKall
July 29th, 2006, 04:44 PM
okey, will look for that :)

nagilum
July 30th, 2006, 05:29 AM
A Makefile is the right choice for a project with multiple source files, you might even want to use autoconf and automake which will create the Makefile for you.
Although for quick testing this is overkill, just compile them like in the example above:

g++ -o hello hello.cpp world.cpp

Alternatively you can also compile the files separately and link them together as a last step:

g++ -c hello.cpp # produces hello.o
g++ -c world.cpp
g++ -o hello hello.o world.o

X.Cyclop
August 1st, 2006, 06:46 PM
It's easier using the best IDE Code::Blocks (http://forums.codeblocks.org/index.php?board=20.0). ;)