You are not logged in.

#1 2010-01-10 11:25:38

astacha
Member
Registered: 2009-03-04
Posts: 27

Problem compiling and running C++ file [SOLVED]

Hi all, just starting out in C++ again after a few years and having some issues.

I'm getting errors trying to run a simple hello world app, it compiles ok by the looks of it but I get an error when trying to run it.

my code is as follows (filename hi.cpp):

#include <iostream>
int main()
{
    using namespace std;
    cout << "hello world";
    cout << endl;
    return 0;
}


I compile the file as follows:
cpp hi.cpp -o hi

This seems to compile ok, so I chmod +x hi and then try to run ./hi then get the following:

./hi: line 64: syntax error near unexpexted token `('
./hi line 64: `namespace std __attribute__ ((__visability__ ("default"))) {'

My geese is that I may not have installed some required components but do not now what.

Last edited by astacha (2010-01-10 15:00:37)

Offline

#2 2010-01-10 11:33:18

PeteMo
Member
From: H'Burg, VA
Registered: 2006-01-26
Posts: 191
Website

Re: Problem compiling and running C++ file [SOLVED]

cpp is the c preprocessor.  It processes the source file before compilation, dealing with macros, includes, etc.  You need to compile your program with g++

g++ hi.cpp -o hi

There will then be no need to chmod the resulting file.  Also, I've never seen "using namespace std" placed inside of main, although it appears to be legal.  I'd also recommend the -Wall flag to g++, it will warn you about potential errors in your code.

Offline

#3 2010-01-10 11:33:57

bharani
Member
From: Karaikudi, India
Registered: 2009-07-12
Posts: 202

Re: Problem compiling and running C++ file [SOLVED]

You have it compile it as

g++ h1.cpp -o h1

Oops  late.

Last edited by bharani (2010-01-10 11:34:35)


Tamil is my mother tongue.

Offline

#4 2010-01-10 11:36:40

astacha
Member
Registered: 2009-03-04
Posts: 27

Re: Problem compiling and running C++ file [SOLVED]

Thank you for that. That'll teach me for skipping over the parts I think I remember and reading the book properly.

Offline

Board footer

Powered by FluxBB