LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
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 09-02-2010, 02:21 PM   #1
jmc1987
Member
 
Registered: Sep 2009
Location: Oklahoma
Distribution: Debian, CentOS, windows 7/10
Posts: 893

Rep: Reputation: 119Reputation: 119
Learning C++ First Script won't compile


I have decided to try out a little bit of c++ programing so I went out and bought me a book "Sams Teach your self C++ in 21 Days"

Now I am at the part of the book where you write the hello world script. when I run

Code:
gcc hello.cpp
I get this
Quote:
james@james-desktop:~/Desktop$ gcc hello.cpp
/tmp/ccYHXH5l.o: In function `main':
hello.cpp:(.text+0xa): undefined reference to `std::cout'
hello.cpp:(.text+0xf): 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/ccYHXH5l.o: In function `__static_initialization_and_destruction_0(int, int)':
hello.cpp:(.text+0x3d): undefined reference to `std::ios_base::Init::Init()'
hello.cpp:(.text+0x42): undefined reference to `std::ios_base::Init::~Init()'
/tmp/ccYHXH5l.o:(.eh_frame+0x12): undefined reference to `__gxx_personality_v0'
collect2: ld returned 1 exit status
Far as I can see the correct is correct by the book.

hello.cpp file
Quote:
#include <iostream>
int main()
{
std::cout << "Hello World!\n";
return 0;
}
Here is my gcc -v output
Quote:
james@james-desktop:~/Desktop$ gcc -v
Using built-in specs.
Target: x86_64-linux-gnu
Configured with: ../src/configure -v --with-pkgversion='Ubuntu 4.3.3-5ubuntu4' --with-bugurl=file:///usr/share/doc/gcc-4.3/README.Bugs --enable-languages=c,c++,fortran,objc,obj-c++ --prefix=/usr --enable-shared --with-system-zlib --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --enable-nls --with-gxx-include-dir=/usr/include/c++/4.3 --program-suffix=-4.3 --enable-clocale=gnu --enable-libstdcxx-debug --enable-objc-gc --enable-mpfr --with-tune=generic --enable-checking=release --build=x86_64-linux-gnu --host=x86_64-linux-gnu --target=x86_64-linux-gnu
Thread model: posix
gcc version 4.3.3 (Ubuntu 4.3.3-5ubuntu4)
Thanks for the help. I am really lost right now.

Last edited by jmc1987; 09-02-2010 at 02:39 PM.
 
Old 09-02-2010, 02:23 PM   #2
14moose
Member
 
Registered: May 2010
Posts: 83

Rep: Reputation: Disabled
Hi -

Don't use "gcc". Use "g++" instead. That should fix the problem

Here's a slight variation on your text book:
Code:
#include <iostream>

using namespace std;

int
main()
{
  cout << "Hello World!" << endl;
  return 0;
}
Quote:
g++ -o hello -Wall -pedantic hello.cpp

./hello
Hello World!

Last edited by 14moose; 09-02-2010 at 02:26 PM.
 
Old 09-02-2010, 02:44 PM   #3
jmc1987
Member
 
Registered: Sep 2009
Location: Oklahoma
Distribution: Debian, CentOS, windows 7/10
Posts: 893

Original Poster
Rep: Reputation: 119Reputation: 119
Thanks so much. I guess gcc for C and g++ for c++?

thanks again

P.S.
I gave you a rep point
 
Old 09-02-2010, 03:46 PM   #4
jmc1987
Member
 
Registered: Sep 2009
Location: Oklahoma
Distribution: Debian, CentOS, windows 7/10
Posts: 893

Original Poster
Rep: Reputation: 119Reputation: 119
Okay I am moving on and and ran into another issue. I tried to compile this code with

g++ exer.cpp

Quote:
#include <iostream>
int main()
{
int x = 5;
int y = 7;
std::cout << endl;
std::cout << x + y << " " << x * y;
std::cout << end;
return 0;
}
and I got the error
Quote:
james@james-desktop:~/Desktop$ g++ exer.cpp
exer.cpp: In function ‘int main()’:
exer.cpp:6: error: ‘endl’ was not declared in this scope
exer.cpp:10: error: ‘end’ was not declared in this scope
according to
http://gcc.gnu.org/gcc-4.3/porting_to.html to resove the error
Quote:
exer.cpp:6: error: ‘endl’ was not declared in this scope
add
using namespace std;

after
#include <iostream>

so now I can't figure out how to resolve these issues.
Quote:
exer.cpp: In function ‘int main()’:
exer.cpp:10: error: ‘end’ was not declared in this scope
Sorry for the trouble.

Thanks again
 
Old 09-02-2010, 04:15 PM   #5
johnsfine
LQ Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,286

Rep: Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197
What do you think end is supposed to be in your code?

I assume you just need that to be endl

Did you make a typo in copying code from the book? Or did the author of the book make the typo?

Either way, the idea of using a book or tutorial is to try to understand the code you are copying, not just copy it. If you try to understand the code you are copying, you should have no trouble recognizing end was a typo for endl.

Last edited by johnsfine; 09-02-2010 at 04:17 PM.
 
Old 09-02-2010, 04:46 PM   #6
Samael
Member
 
Registered: Oct 2008
Location: England
Distribution: Ubuntu and Fedora (64-bit)
Posts: 81

Rep: Reputation: 16
as 14moose put, if you type:

Code:
using namespace std;
under your include section it saves you from typing std:: before every cout and cin.
 
Old 09-02-2010, 04:52 PM   #7
alan99
Member
 
Registered: Mar 2010
Distribution: Debian
Posts: 180

Rep: Reputation: 31
You forgot to reference the namespace, it should be std::endl
 
Old 09-02-2010, 05:30 PM   #8
Kenny_Strawn
Senior Member
 
Registered: Feb 2010
Location: /usa/ca/orange_county/lake_forest
Distribution: ArchBang, Google Android 2.1 + Motoblur (on Motortola Flipside), Google Chrome OS (on Cr-48)
Posts: 1,791
Blog Entries: 62

Rep: Reputation: 56
Code:
#include <iostream>

using namespace std;

int main()
{
    int x = 5;
    int y = 7;
    cout << endl
         << x + y 
         << " " 
         << x * y;
    return 0;
}
^^
Fixed your code for you.
 
Old 09-02-2010, 05:34 PM   #9
jmc1987
Member
 
Registered: Sep 2009
Location: Oklahoma
Distribution: Debian, CentOS, windows 7/10
Posts: 893

Original Poster
Rep: Reputation: 119Reputation: 119
No I am not just copying. The exercise is to determine what the code does then compiles it and see if I am right.

From the book word for word.

Quote:
1. Look at the following program and try to guess what it does without running it.
Quote:
#include <iostream>
int main()
{
int x = 5;
int y = 7;
std::cout << endl;
std::cout << x + y << " " << x * y;
std::cout << end;
return 0;
}

To get it compile using g++ I had to edit like this
Quote:
#include <iostream>
using namespace std;

int main()
{
int x = 5;
int y = 7;
cout << endl;
cout << x + y << " " << x * y;
cout << endl;
return 0;
}
So I suppose it a possible typo in the book?

Like I said the book "Sams Teach Yourself C++ in 21 Days" is publish by Sams which is a well known for Education References.

How ever I notice now I seem to be editing ever tutorial to get them to work.

And just to be sure gcc is a ansi compliant compiler right? I think the bookk may be written for a microsoft ansi compliant compiler. Not sure if that makes a big difference as I am a newbie here in the area.



Thanks again. It starting to work out. I'll let you guys know how things work out.
 
Old 09-02-2010, 05:46 PM   #10
14moose
Member
 
Registered: May 2010
Posts: 83

Rep: Reputation: Disabled
Hi -

1. Despite the best of intentions, many (most) books have typos.

But at least books have technical editors, and some kind of formal review process. My experience is that other resources (like net postings) are often worse.

2. Yes, you want "endl", not "end".
And yes, it's fully qualified name is "std::endl".

3. Yes, gcc is a C compiler, and g++ is a C++ compiler.

4. Yes, the snippet above builds and executes without error:
Code:
#include <iostream>

using namespace std;

int main()
{
  int x = 5;
  int y = 7;
  cout << endl;
  cout << x + y << " " << x * y;
  cout << endl;
  return 0;
}

Last edited by 14moose; 09-02-2010 at 05:49 PM.
 
Old 09-02-2010, 06:08 PM   #11
Kenny_Strawn
Senior Member
 
Registered: Feb 2010
Location: /usa/ca/orange_county/lake_forest
Distribution: ArchBang, Google Android 2.1 + Motoblur (on Motortola Flipside), Google Chrome OS (on Cr-48)
Posts: 1,791
Blog Entries: 62

Rep: Reputation: 56
Here's a build of that program that I was able to create:

Code:
#include <iostream>

using namespace std;

int main()
{
  int x;
  cout << "Enter a number: ";
  cin >> x;
  
  int y;
  cout << "Enter another number: ";
  cin >> y;
  
  cout << endl;
  cout << x + y << ", " << x * y;
  cout << endl;
  return 0;
}
Instead of declaring the variables and simply printing them, how about asking for them when the program runs?
 
Old 09-02-2010, 11:56 PM   #12
Kenny_Strawn
Senior Member
 
Registered: Feb 2010
Location: /usa/ca/orange_county/lake_forest
Distribution: ArchBang, Google Android 2.1 + Motoblur (on Motortola Flipside), Google Chrome OS (on Cr-48)
Posts: 1,791
Blog Entries: 62

Rep: Reputation: 56
Code:
echo $BUMP
 
Old 09-03-2010, 01:21 AM   #13
andrei_b
LQ Newbie
 
Registered: Jul 2009
Posts: 11

Rep: Reputation: 0
or, as alan99 said:

Code:
std::cout << "Hi there!" << std::endl;
cout and endl both reside in std namespace.
 
Old 09-03-2010, 01:39 AM   #14
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
Quote:
Originally Posted by jmc1987 View Post
I have decided to try out a little bit of c++ programing so I went out and bought me a book "Sams Teach your self C++ in 21 Days"
if this is not for homework/work and its just for learning how to program, i would suggest not using C++. Languages like Python/Perl etc doesn't need to be compiled, and you don't have to work with things like pointers, memory management etc. You will make good use of your time learning how to write algorithms and stuff, instead of figuring out how to compile properly, why your pointers doesn't work etc....

Perl:

Code:
printf "Enter a number: "
chomp($number=<>);
print "Number entered: $number\n";
Python:
Code:
number=raw_input("Enter a number:")
print "Number is ", str(number)

Last edited by ghostdog74; 09-03-2010 at 01:40 AM.
 
Old 09-03-2010, 02:17 AM   #15
jmc1987
Member
 
Registered: Sep 2009
Location: Oklahoma
Distribution: Debian, CentOS, windows 7/10
Posts: 893

Original Poster
Rep: Reputation: 119Reputation: 119
I can do a little bit of perl like making calculations but I got a book on C++ so not I am going to learn it. But after I get the hang of C++ from what I understand I should practically be able to write alot of perl easier. So I have plans to jump into perl. The real reason of me wanting to learn some strong programming languages is so I can write some opensource programs.

Thanks for the help everyone.

Just a update on how things are going and for the people who have helped me and/or would like to know what I've done.

I am learning more about integers and stuff. So far I understands how functions work to. Also Learned how to figure out how much integers user in memory with
Quote:
#include <iostream>

int main()
{
using std::cout;

cout << "The size of an int is:\t\t"
<< sizeof(int) << " bytes.\n";
cout << "The size of a short int is:\t"
<< sizeof(short) << " bytes.\n";
cout << "The size of a short int is:\t"
<< sizeof(long) << " bytes.\n";
cout << "The size of char is:\t\t"
<< sizeof(char) << " bytes.\n";
cout << "The size of float is:\t\t"
<< sizeof(float) << " bytes.\n";
cout << "The size of a double is:\t"
<< sizeof(double) << " bytes.\n";
cout <<"The size of a bool is:\t"
<< sizeof(bool) << " bytes.\n";

return 0;
}
Hopefully in another week I might be able to start coding my own programs at the rate Im going.

Wish me Luck
 
  


Reply


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
[SOLVED] Learning Shell script new.2.linux Linux - Newbie 4 07-02-2010 09:40 AM
E-learning learning, VLS want to help other learn? scheidel21 General 0 04-06-2010 11:19 AM
Help needed learning to write a shell script for logons nistur Linux - Networking 8 10-02-2004 02:34 AM
learning Shell script ???? Atrocity Programming 3 02-02-2003 01:49 PM
Learning to script robbage Programming 6 12-26-2002 01:13 PM

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

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

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