LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
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 12-30-2004, 05:09 PM   #1
g2axiom
LQ Newbie
 
Registered: Dec 2004
Posts: 8

Rep: Reputation: 0
learning c++


Ok so bare with me please as im quiet nex to c++ and linux. I borwed a book to help me learn C++ the book starts out with an example program to get you started it is:

Code:
#include <iostream>

int main()
{
	//write to the screen
	std:cout << "My first c++ program";
	return 0;
}
so i saved the file as frst.c and ran it in gcc. I recieved the following error:
Code:
gcc first.c
first.c:1:20: iostream: No such file or directory
first.c: In function `main':
first.c:6: error: `cout' undeclared (first use in this function)
first.c:6: error: (Each undeclared identifier is reported only once
first.c:6: error: for each function it appears in.)
My question inst so much that I got an error, but rather is it possbile to have c++ examples that only work in windows? As i thought c++ was not platform dependent.

So I then found the example c++ program called hellow
Code:
#include <stdio.h>
int main()
 {
  (void)printf("Hello, World!\n");
  return 0; /* Just to be nice */

}
Now this one worked with no other issue than
Code:
hello.c:7:2: warning: no newline at end of file
this one produced the output file a.out and I was able to run it and receive the desired output message from the program in console.

So maybe someone could let me know what is wrong with the above program? i just want to nkow before I start getting into this book and start having more problems running there example programs.
Sorry if this doesnt make alot of sence, i tried to state it the best way i could.
Thank you again to anyone who may help or guide me in this issue.
 
Old 12-30-2004, 05:11 PM   #2
dizzutch
Member
 
Registered: Sep 2004
Location: Worcester, MA USA
Distribution: Debian, Gentoo, Fedora FC2
Posts: 66

Rep: Reputation: 15
ok, you'reusing C++ so try compiling with g++
that should solve your problem, also make the file .cpp in steadof .c that way g++ and gcc will know it's a C++ file.

Jule
 
Old 12-30-2004, 05:30 PM   #3
Dodgeram01
Member
 
Registered: Jun 2003
Distribution: Gentoo and Ubuntu
Posts: 95

Rep: Reputation: 15
The first code sample you posted was C++, while the second code sample you posted was C. Try readjusting your compiler commands appropriately and report back with the results.
 
Old 12-30-2004, 07:26 PM   #4
g2axiom
LQ Newbie
 
Registered: Dec 2004
Posts: 8

Original Poster
Rep: Reputation: 0
Hey thanks for the response dizzutch and Dodgeram01.
Ok so I did somemore reading and yes the top one is C++ the other is C, so since im trying to learn C++ ill stick with the first one mentioned above. So I renamed the file first.cpp from first.c
I then ran at console g++ first.cpp ( I ran --help first but wasnt really sure what to use since im a new at this so just ran g++ first.cpp)
And I recieved the same above errors
:
Code:
g++ first.cpp
first.cpp: In function `int main()':
first.cpp:6: error: use of namespace `std' as expression
first.cpp:6: error: syntax error before `:' token
So im not sure if this is somethign I am doing wrong or am I missing some sort of library?
But thanks for your help Im willing to try anything to learn what it is im doiing wrong.

Thank you once again for your time!!

Axiom
 
Old 12-30-2004, 07:34 PM   #5
deiussum
Member
 
Registered: Aug 2003
Location: Santa Clara, CA
Distribution: Slackware
Posts: 895

Rep: Reputation: 32
std:cout

Should be:

std::cout
 
Old 12-30-2004, 07:43 PM   #6
g2axiom
LQ Newbie
 
Registered: Dec 2004
Posts: 8

Original Poster
Rep: Reputation: 0
Ok so i found my first mistake:
in the code i typed
std:cout
should have been std::cout

So it worked in g++ it created the a.out file. But a friend of mine ran his with
gcc first.cpp
and his worked?

Mine when ran through gcc gives me this:
Code:
gcc first.cpp
/tmp/ccXsPy2h.o(.text+0x25): In function `main':
: undefined reference to `std::cout'
/tmp/ccXsPy2h.o(.text+0x2a): In function `main':
: undefined reference to `std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*)'
/tmp/ccXsPy2h.o(.text+0x56): In function `__static_initialization_and_destruction_0(int, int)':
: undefined reference to `std::ios_base::Init::Init[in-charge]()'
/tmp/ccXsPy2h.o(.text+0x85): In function `__tcf_0':
: undefined reference to `std::ios_base::Init::~Init [in-charge]()'
/tmp/ccXsPy2h.o(.eh_frame+0x11): undefined reference to `__gxx_personality_v0'
collect2: ld returned 1 exit status
So i guess I am happy that it worked and created the ou

tput in g++ but still a bit perplexed as why i cant do it in gcc and a friend can? we both are running suse9.2 with all the current updates.
so anymore help is grealtly welcomed.

Thankyou
Axiom

thanxs deiussum i just saw your post

Last edited by g2axiom; 12-30-2004 at 07:45 PM.
 
Old 12-30-2004, 07:57 PM   #7
btmiller
Senior Member
 
Registered: May 2004
Location: In the DC 'burbs
Distribution: Arch, Scientific Linux, Debian, Ubuntu
Posts: 4,290

Rep: Reputation: 378Reputation: 378Reputation: 378Reputation: 378
Both gcc and g++ are front-ends for the GNU compiler collection. AFAIK g++ just sets some options in terms of lnking in the C++ standard libraries. I think if you name your program file (i.e. with a C++ file extension such as .C or .cxx, gcc will pick up on the fact that it's C++ and linkthose in too. This is probably what your friend did. If you try to compile and link C++ without linking in the C++ standard libs, as you saw, bad things can happen.

What version of gcc do you have? I think more recent ones willrecognize cpp as a valid C++ extension.

[Edit: I just tried this (I don't do C++ very much) and I couldn't make it work without using g++ -- are you sure your friend was able to? maybe he explicitly linked inthe C++ libs.]

Last edited by btmiller; 12-30-2004 at 08:01 PM.
 
Old 12-30-2004, 08:16 PM   #8
reddazz
LQ Guru
 
Registered: Nov 2003
Location: N. E. England
Distribution: Fedora, CentOS, Debian
Posts: 16,298

Rep: Reputation: 77
I'm also learning c++, trying to come to grips with arrays and pointers at the moment (stressing). I believe the correct way of writing the first program using the new C++ standards is,

#include <iostream>
using namespace std;
int main (void)
{
//write to the screen
cout << "My first c++ program";
return 0;
}

Then using g++ do,

$g++ -o programname programname.cpp
 
Old 12-30-2004, 11:26 PM   #9
fr0zen
Member
 
Registered: Nov 2003
Location: 127.0.0.1
Distribution: xubuntu
Posts: 217

Rep: Reputation: 30
Keep in mind that you don't want to use using namespace std; in production code. Instead, prepend the 'std' scope qualifier to functions.

#include <iostream>

int main (void)
{
// you could actually do this:
// using std::cout;
// cout << "Moo\n";

// or call the function directly (typically a better idea)
std::cout << "My first c++ program" << std::endl;

return 0;
}

Last edited by fr0zen; 12-30-2004 at 11:27 PM.
 
Old 12-31-2004, 02:00 AM   #10
reddazz
LQ Guru
 
Registered: Nov 2003
Location: N. E. England
Distribution: Fedora, CentOS, Debian
Posts: 16,298

Rep: Reputation: 77
Why isn't using namespace std; not used in production code? Still a newbie to programming, so I'm very curious.
 
Old 12-31-2004, 05:22 AM   #11
fr0zen
Member
 
Registered: Nov 2003
Location: 127.0.0.1
Distribution: xubuntu
Posts: 217

Rep: Reputation: 30
I said it wasn't a good idea to use it in production code, that isn't to say programmers (professional or not) do it anyway.

The reason is, you're effectively importing the entire namespace. It defeats the purpose of having the namespace for organization. You may run into an error down the line with a function name clashing with something in std. The best thing to do is to either specify the full name (scope included) of the function.

Aside from function name clashing, there's the possibility of unnecessary overhead. This isn't always the case, but, it's something to consider. In my opinion, leaving the namespace std alone often makes code look better too. "Where is that function? Oh, it's in std."
 
Old 12-31-2004, 05:46 AM   #12
reddazz
LQ Guru
 
Registered: Nov 2003
Location: N. E. England
Distribution: Fedora, CentOS, Debian
Posts: 16,298

Rep: Reputation: 77
I get what you mean now, thanks for the extra detail
 
Old 12-31-2004, 12:05 PM   #13
g2axiom
LQ Newbie
 
Registered: Dec 2004
Posts: 8

Original Poster
Rep: Reputation: 0
Thanks again for all the input into this question!!

Well if you noticed in my first post above I did originally use the .C for my file extension. ANd still could not get it to work in gcc (gcc version: 3.3.4)
And I know the code may not be written "perfectly" but that source was taken line from line from a book that I am trying to use to teach myself with. So I was a bit foggy as to why it wouldnt work with gcc and would with g++.

So as you can see I really dont know much about c++, basically I was just reading through my book "Wiley's Tech yourself C++ 7th Edition" (got this book used for dirt cheap so I thought it would be a good score) and saw that first snippet of code and thought I would try it just to see if it would work.

I have seen some books that are specifically titled "c++ for Linux" and such, but I thought since c++ wasnt platform dependent that I could use whatever book to help teach me the foundations of programming in C++. Am I wrong in this statement? Or better yet is there a book or books someone can recommend for learning c++?

Well thanks again for all your help here, I still have lots more to learn so I sure I will be back to pick the minds of the linuxquestions community!!


Thanks again, help is very much appreciated over here


Axiom
 
Old 12-31-2004, 01:07 PM   #14
deiussum
Member
 
Registered: Aug 2003
Location: Santa Clara, CA
Distribution: Slackware
Posts: 895

Rep: Reputation: 32
The C/C++ language itself isn't platform dependent, but there are platform specific APIs... For instance, in Windows you have the Win32 API, which is specific to Windows. For Linux/UNIX you have a number of other APIs that are specific to UNIX based platforms.

However, even though those APIs are fairly platform specific, you can sometimes get a port of them. For instance, I believe that Winelib is basically a port of the Win32 API functions to Linux, and for Windows, you can get most of the Linux/UNIX APIs using a tool like Cygwin...

If you stick to those functions of C/C++ that are defined by the ANSI/ISO standard, your code should stay pretty portable. Once you are ready to move on to other APIs, you should better understand C/C++ and be able to make choices of the APIs you want to use.
 
Old 12-31-2004, 07:57 PM   #15
microsoft/linux
Senior Member
 
Registered: May 2004
Location: Sebec, ME, USA
Distribution: Debian Etch, Windows XP Home, FreeBSD
Posts: 1,445
Blog Entries: 9

Rep: Reputation: 48
I've found the O'Reilly book Practical C++ Programming a good book, I'm also trying to learn C++. I don't have time to study it but, I like this book.
 
  


Reply



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
Learning C J_K9 Programming 18 09-17-2005 01:17 PM
Learning as I go. Curious LinuxQuestions.org Member Intro 1 09-25-2003 12:07 AM
Learning? slack105 Slackware 3 07-31-2003 12:46 PM
Learning C++ pilot1 Programming 8 07-02-2003 01:58 PM
learning how much mb? nakkaya Linux - General 4 02-27-2003 10:04 AM

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

All times are GMT -5. The time now is 03:36 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