DaemonForums  

Go Back   DaemonForums > FreeBSD > FreeBSD General

FreeBSD General Other questions regarding FreeBSD which do not fit in any of the categories below.

Reply
 
Thread Tools Display Modes
  #1   (View Single Post)  
Old 19th May 2008
Johnny2Bad Johnny2Bad is offline
Port Guard
 
Join Date: May 2008
Posts: 21
Thanked 0 Times in 0 Posts
Exclamation Why wont this compile?

Hi,
Well over the past few weeks this newbie has been very impressed over FreeBSD and wonders why he wasted years with M$ products. Your support has been wonderfull and the O/S rocks.

However being a programmer at heart I wanted to check out gcc++. So I made a file Hello.cpp

Code:
#include <iostream>

int main()
{
    cout << "Hello World\n";

    return 0;
}
the simplest program I could think of, which when compiled gives me...

Code:
/var/tmp//ccPEm5DG.o(.text+0x1e): In function `std::__verify_grouping(char const*, unsigned int, std::string const&)':
: undefined reference to `std::string::size() const'
/var/tmp//ccPEm5DG.o(.text+0x69): In function `std::__verify_grouping(char const*, unsigned int, std::string const&)':
: undefined reference to `std::string::operator[](unsigned int) const'
/var/tmp//ccPEm5DG.o(.text+0xa7): In function `std::__verify_grouping(char const*, unsigned int, std::string const&)':
: undefined reference to `std::string::operator[](unsigned int) const'
/var/tmp//ccPEm5DG.o(.text+0xef): In function `std::__verify_grouping(char const*, unsigned int, std::string const&)':
: undefined reference to `std::string::operator[](unsigned int) const'
/var/tmp//ccPEm5DG.o(.text+0x13c): In function `main':
: undefined reference to `std::cout'
/var/tmp//ccPEm5DG.o(.text+0x141): 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*)'
/var/tmp//ccPEm5DG.o(.text+0x17d): In function `__static_initialization_and_destruction_0(int, int)':
: undefined reference to `std::ios_base::Init::Init()'
/var/tmp//ccPEm5DG.o(.text+0x1ce): In function `__tcf_0':
: undefined reference to `std::ios_base::Init::~Init()'
/var/tmp//ccPEm5DG.o(.eh_frame+0x11): undefined reference to `__gxx_personality_v0'
$ /var/tmp//ccPEm5DG.o(.text+0x1e): In function `std::__verify_grouping(char const*, unsigned int, std::string const&)':
: undefined reference to `std::string::size() const'
/var/tmp//ccPEm5DG.o(.text+0x69): In function `std::__verify_grouping(char const*, unsigned int, std::string const&)':
: undefined reference to `std::string::operator[](unsigned int) const'
Syntax error: word unexpected (expecting ")")
/var/tmp//ccPEm5DG.o(.text+0xa7): In function `std::__verify_grouping(char const*, unsigned int, std::string const&)':
$ : undefined reference to `std::string::size() const'
/var/tmp//ccPEm5DG.o(.text+0x69): In function `std::__verify_grouping(char const*, unsigned int, std::string const&)':
: undefined reference to `std::string::operator[](unsigned int) const'
/var/tmp//ccPEm5DG.o(.text+0xa7): In function `std::__verify_grouping(char const*, unsigned int, std::string const&)':
: undefined reference to `std::string::operator[](unsigned int) const'
/var/tmp//ccPEm5DG.o(.text+0xef): In function `std::__verify_grouping(char const*, unsigned int, std::string const&)':
> /var/tmp//ccPEm5DG.o(.text+0x69): In function `std::__verify_grouping(char const*, unsigned int, std::string const&)':
Syntax error: "(" unexpected
$ : undefined reference to `std::string::operator[](unsigned int) const'
> /var/tmp//ccPEm5DG.o(.text+0xa7): In function `std::__verify_grouping(char const*, unsigned int, std::string const&)':
Syntax error: word unexpected (expecting ")")
Syntax error: word unexpected (expecting ")")
$ : undefined reference to `std::string::operator[](unsigned int) const'
> /var/tmp//ccPEm5DG.o(.text+0xef): In function `std::__verify_grouping(char const*, unsigned int, std::string const&)':
Syntax error: word unexpected (expecting ")")
Syntax error: word unexpected (expecting ")")
$ : undefined reference to `std::string::operator[](unsigned int) const'
> /var/tmp//ccPEm5DG.o(.text+0x13c): In function `main':
Syntax error: word unexpected (expecting ")")
> : undefined reference to `std::cout'
main:
: undefined reference to `std::cout: not found
$ /var/tmp//ccPEm5DG.o(.text+0x141): In function `main':
Syntax error: word unexpected (expecting ")")
$ : 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*)'
> /var/tmp//ccPEm5DG.o(.text+0x17d): In function `__static_initialization_and_destruction_0(int, int)':
Syntax error: redirection unexpected
Syntax error: word unexpected (expecting ")")
$ : undefined reference to `std::ios_base::Init::Init()'
> /var/tmp//ccPEm5DG.o(.text+0x1ce): In function `__tcf_0':
> : undefined reference to `std::ios_base::Init::~Init()'
$ /var/tmp//ccPEm5DG.o(.eh_frame+0x11): undefined reference to `__gxx_personality_v0'
Can anyone explain why this is not working? Perhaps I should be posting this to a programming forum, however it is so simple I thought you guys would pickup where I was going wrong in seconds.

Thanking you in advance,
Jonathan.
Reply With Quote
  #2   (View Single Post)  
Old 19th May 2008
windependence's Avatar
windependence windependence is offline
Real Name: Tim
Shell Scout
 
Join Date: May 2008
Location: Phoenix, Arizona
Posts: 116
Thanked 4 Times in 3 Posts
Default

It's been a while but I think you are missing some includes like stdio.h.

And yes, this should be in the programming forum.

-Tim
__________________
www.windependence.org
Get your Windependence today!
Reply With Quote
  #3   (View Single Post)  
Old 19th May 2008
18Googol2's Avatar
18Googol2 18Googol2 is offline
Real Name: whoami
Spam Deminer
 
Join Date: Apr 2008
Location: pwd
Posts: 283
Thanked 20 Times in 18 Posts
Default

Quote:
Originally Posted by Johnny2Bad View Post

Code:
#include <iostream>

int main()
{
    cout << "Hello World\n";

    return 0;
}
".h" was missing. iostream ==> iostream.h
__________________
The power of plain text? It can control an entire OS
Reply With Quote
  #4   (View Single Post)  
Old 19th May 2008
ephemera's Avatar
ephemera ephemera is offline
Knuth's homeboy
 
Join Date: Apr 2008
Posts: 537
Thanked 49 Times in 43 Posts
Default

looks like you were trying to compile it with 'gcc' instead of 'g++'.

change your program to:

Code:
//  test.cc  or test.cpp
#include <iostream>

int main()
{
    std::cout << "Hello World\n";

    return 0;
}
to compile:

$ g++ test.cc
Reply With Quote
  #5   (View Single Post)  
Old 19th May 2008
BSDfan666 BSDfan666 is offline
Real Name: N/A, this is the interweb.
Helpful companion
 
Join Date: Apr 2008
Location: Ontario, Canada
Posts: 2,223
Thanked 193 Times in 184 Posts
Default

Or, instead.. do:
Code:
//  test.cc  or test.cpp
#include <iostream>

using namespace std;

int
main(void) {
    cout << "Hello World\n";
                            
    return 0;               
}
That we you don't need to add the std:: prefix..

Anyway, I don't understand why people use C++ instead of C.. it's so much cleaner.
Reply With Quote
  #6   (View Single Post)  
Old 19th May 2008
Nicolas's Avatar
Nicolas Nicolas is offline
Real Name: Nicolas Belan
New User
 
Join Date: May 2008
Location: Clermont Ferrand
Posts: 2
Thanked 0 Times in 0 Posts
Default

Quote:
Originally Posted by BSDfan666 View Post
Anyway, I don't understand why people use C++ instead of C.. it's so much cleaner.
Yes ... I agree with you ... Things are so simple in C ...

I tried the compilation and it works fine for me (FreeBSD 6.3) using std::cout.

On older versions, you may have to add libstd++ to link the software.
__________________
-- Nicolas
Reply With Quote
  #7   (View Single Post)  
Old 19th May 2008
ephemera's Avatar
ephemera ephemera is offline
Knuth's homeboy
 
Join Date: Apr 2008
Posts: 537
Thanked 49 Times in 43 Posts
Default

people have different requirements IMO its not correct to discourage others from using c++ because of your personal (unsubstantiated) opinions.

freebsd has proper support for c++ and quite a few apps (outside the fbsd base system) use c++.
Reply With Quote
  #8   (View Single Post)  
Old 19th May 2008
Nicolas's Avatar
Nicolas Nicolas is offline
Real Name: Nicolas Belan
New User
 
Join Date: May 2008
Location: Clermont Ferrand
Posts: 2
Thanked 0 Times in 0 Posts
Default

1) I don't discourage others ... only the ones who work with me
2) I do not want to start a troll pros/cons c++

I only said that I agree with BSDfan666.

I have spent 9 months debugging a huge C++ hugly code, because the language is too much complicated for novices.

C is far more simpler (powerless ? I do not think so)

I may speak about malloc/new, references, exceptions and so on...

"How C++ is like teenage sex:

1. It is on everyone's mind all the time.
2. Everyone talks about it all the time.
3. Everyone thinks everyone else is doing it.
4. Almost no one is really doing it.
5. The few who are doing it are: A. Doing it poorly. B. Sure it will be better next time. C. Not practising it safely."
__________________
-- Nicolas
Reply With Quote
  #9   (View Single Post)  
Old 19th May 2008
ephemera's Avatar
ephemera ephemera is offline
Knuth's homeboy
 
Join Date: Apr 2008
Posts: 537
Thanked 49 Times in 43 Posts
Default

> I have spent 9 months debugging a huge C++ hugly code, because the language is too much complicated for novices.

yes i agree C++ is a complicated prog. language.

> "How C++ is like teenage sex: ... Not practising it safely."

i hope you didn't miss the context of that joke.
it was a toilet graffiti in the CS dept. of a college in Israel way back in 1993 (i suppose C++ must have been the over-hyped latest programming fad at the time).

Last edited by ephemera; 19th May 2008 at 10:57 PM.
Reply With Quote
Old 19th May 2008
Johnny2Bad Johnny2Bad is offline
Port Guard
 
Join Date: May 2008
Posts: 21
Thanked 0 Times in 0 Posts
Talking Okay it now works...

ephemera had most of the answer, I was using the wrong compiler. I should use g++ not anything else, but then I ran into a problem.

So g++ -o Hello Hello.cpp worked with the following code.

Code:
#include <iostream>

using namespace std;

int main()
{
        cout<<"Hello World\n";

        return 0;
}

The thing wouldn't execute from the current directory so

Code:
Hello
failed, but when I typed...

Code:
./Hello
it finally said "Hello World"!

Thanks for your help guys, in future I will post programming questions where they belong.

Cheers,
Jonathan.
Reply With Quote
Old 19th May 2008
TerryP's Avatar
TerryP TerryP is offline
Arp Constable
 
Join Date: May 2008
Location: USofA
Posts: 1,547
Thanked 112 Times in 104 Posts
Default

Quote:
Originally Posted by 18Googol2 View Post
".h" was missing. iostream ==> iostream.h
Code:
#include <iostream>
is correct and standard C++, the .h part is the old method and I believe is depreciated in modern C++.


Although you might find a compiler that barfs about missing .h's you probably will never find one that complains by default about them.


I believe one of the changes in the transition from Qt3 to Qt4 is #include <QWhatEver> is now the preferred norm as well over the old #include <QWhatEver.h> or die.


(Disclaimer: I have avoided using C++ for a long time now, I prefer ISO/IEC 9899:1999 aka C99 ).


If one doesn't need access to much of the (entire) std namespace it's probably best to do something else in a larger (more realistic) program, but I think everyone here knows that and I use helloworlds to test new compilers/IDE's as well


The name spaces related syntax and templates are one of the very few things that I like about C++.

Quote:
Originally Posted by Johnny2Bad View Post
The thing wouldn't execute from the current directory so

Code:
Hello
failed, but when I typed...

Code:
./Hello
it finally said "Hello World"!
This is because the shell uses a search path to locate programs. Everything on the command prompt is eventually tokenized and checked.

If $PATH is == /bin:/usr/bin typing 'hello' tells the shell to look for /bin/hello and then /usr/bin/hello.

If you don't like doing the ./hello thing you'll have to add the current working directory to your shells $PATH.
__________________
My Journal

Thou shalt check the array bounds of all strings (indeed, all arrays), for surely where thou typest ``foo'' someone someday shall type ``supercalifragilisticexpialidocious''.
Reply With Quote
Reply

Thread Tools
Display Modes

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 On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
pidgin (py25-gtk) wont upgrade mururoa FreeBSD Installation and Upgrading 3 7th September 2009 07:45 PM
Epiphany and Firefox2 wont update delboy FreeBSD Ports and Packages 4 25th August 2009 01:18 PM
Firefox 3 port wont compile : libsoftokn3.so problems padmanabh FreeBSD Ports and Packages 3 8th October 2008 07:13 AM
PF wont open port despite rules... Dain_L OpenBSD Security 3 12th September 2008 01:14 AM
KDE wont start up dctr FreeBSD General 9 11th June 2008 05:59 AM


All times are GMT. The time now is 06:13 AM.


Powered by vBulletin® Version 3.8.4
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
Content copyright © 2007-2010, the authors
Daemon image copyright ©1988, Marshall Kirk McKusick