Latest LQ Deal: Latest LQ Deals
Go Back > Forums > Linux Forums > Linux - Newbie
User Name
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!


  Search this Thread
Old 10-15-2007, 06:34 PM   #1
LQ Newbie
Registered: Sep 2007
Posts: 19

Rep: Reputation: 0
C compiling problem

Hi, I installed Ubuntu a couple of weeks ago and am still getting used to the whole environment. I was trying to compile this C program, which I'm pretty sure is bug-less:

// basic file operations
#include <iostream>
#include <fstream>
using namespace std;

int main () {
ofstream myfile; ("example.txt");
myfile << "Writing this to a file.\n";
return 0;

but when I try to compile it with
gcc example.c

I get:
example.c:2:20: error: iostream: No such file or directory
example.c:3:19: error: fstream: No such file or directory
example.c:4: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘namespace’
example.c: In function ‘main’:
example.c:7: error: ‘ofstream’ undeclared (first use in this function)
example.c:7: error: (Each undeclared identifier is reported only once
example.c:7: error: for each function it appears in.)
example.c:7: error: expected ‘;’ before ‘myfile’
example.c:8: error: ‘myfile’ undeclared (first use in this function)

I included the two necessary packages so I don't know what it's complaining about... am I missing something in my compile instruction?
Old 10-15-2007, 06:42 PM   #2
LQ Newbie
Registered: Apr 2006
Location: Cambridge, MA
Distribution: SuSE 11.2
Posts: 5

Rep: Reputation: 0
First of all, you named it example.c and not cpp (for C++) and you are using the codestyle for C++ with streams.

Be sure to use g++ instead of gcc to compile this program... it should work.
Old 10-15-2007, 06:43 PM   #3
LQ Newbie
Registered: Mar 2004
Posts: 23

Rep: Reputation: 15

Now, it's been a while since I did any C/C++ programming, but I typed your program in, and got the same message that you did. I then switched the name to "example.cpp" and used
> g++ example.cpp

This got a clean compile.

Best of luck!
Old 10-15-2007, 06:48 PM   #4
LQ Newbie
Registered: Sep 2007
Posts: 19

Original Poster
Rep: Reputation: 0
great problem solved thanks.
Old 10-16-2007, 11:36 AM   #5
LQ Newbie
Registered: Jul 2007
Location: Athens, Greece
Distribution: Ubuntu 7.10
Posts: 28

Rep: Reputation: 15

g++ example.cpp
get rid of your source file?
Old 10-16-2007, 08:47 PM   #6
Registered: Oct 2001
Location: Brockport, NY
Distribution: Kubuntu
Posts: 384

Rep: Reputation: 32
Originally Posted by Jason21 View Post
Wouldn't g++ example.cpp get rid of your source file?
Of course not. It will compile example.cpp, put the resulting executable in a file named a.out, and leave the source file unchanged.
Old 10-16-2007, 09:15 PM   #7
Registered: Mar 2005
Distribution: Debian stable, OpenBSD, FreeBSD
Posts: 30

Rep: Reputation: 15
If you want to have gcc/g++ output the final ELF binary that you can redistribute, use the -o switch.


g++ example.cpp -o example
That will compile your example.cpp into the program "example". You can then test it by typing:

in the same folder. Note: you need the ./ to tell Linux to look in the CURRENT directory for the program. Without it, it will look in your PATH, e.g. /bin, /usr/bin, etc.


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
compiling problem gr0sshirn Linux - Software 3 09-02-2006 11:11 AM
Problem while compiling shinni Programming 1 02-08-2005 01:13 AM
Problem compiling javakid Programming 8 12-16-2004 01:03 AM
Compiling problem.... pyrocrickett Linux - Software 2 09-11-2003 03:19 PM
problem compiling qt qanopus Linux - Software 3 03-08-2003 03:09 PM > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 11:22 PM.

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