CX-Post: hello world

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Tim Judd

    #1

    CX-Post: hello world

    I must not be grasping anything here.

    Just a simple application, as follows (as any first-program is...)

    ----
    #include <iostream.h>

    int main() {
    cout <<"hello world";
    return 0;
    }
    ----

    never compiles! Tried it on FreeBSD, OpenBSD, Windows (under Visual C++ for
    Console App), OS X developer tools. THEY ALL complain!
    pretty much the same reason too!

    /tmp/ccJ31810.o: In function `main':
    /tmp/ccJ31810.o(.tex t+0xf): undefined reference to `cout'
    /tmp/ccJ31810.o(.tex t+0x14): undefined reference to
    `ostream::opera tor<<(char const *)'
    collect2: ld returned 1 exit status


    But... I thought the #include was supposed to bring in the definitions from
    that file before checking the code for execution problems.

    I don't understand why it won't work, no matter the tutorials I see, books I
    read, articles I read! Can someone point me in the right direction, a
    utterly clueless newbie! (yes, I called myself that, so you can call me
    that ONCE, too!)

    Appreciate it in advance, email is spam-blocked, remove the single period
    PRIOR to the @ sign to reply by mail.

    --Tim
  • JaSeong Ju

    #2
    Re: CX-Post: hello world

    Do this instead:

    #include <iostream>


    using namespace std;


    int main() {
    cout << "hello world" << endl;
    return 0;
    }



    iostream.h is deprecated, the standard C++ is to use
    iostream without .h

    the undefined reference to cout appeared because
    the "using namespace std" was not there.

    see http://www.research.att.com/~bs/bs_faq2.html

    Comment

    • Kelvin@!!!

      #3
      Re: CX-Post: hello world

      well, you forgot something in ur code..
      first C++ standard encourages you to use
      #include <iostream>
      and cout should really be written as std::cout in your case

      if you dont want to write std::cout through out your whole program
      you should put
      using namespace std;
      before your main() function
      so that you can just write cout in the following lines

      and i also have a question here

      can i put the "using namespace bla" in the middle of my code?
      or can i make it effective in only a certain block of code??
      thank you very much
      --
      { Kelvin@!!! }
      "Tim Judd" <tjudd.01@hotma il.com> wrote in message
      news:irOdnTCt0J ziReHdRVn-hw@comcast.com. ..[color=blue]
      > I must not be grasping anything here.
      >
      > Just a simple application, as follows (as any first-program is...)
      >
      > ----
      > #include <iostream.h>
      >
      > int main() {
      > cout <<"hello world";
      > return 0;
      > }
      > ----
      >
      > never compiles! Tried it on FreeBSD, OpenBSD, Windows (under Visual C++[/color]
      for[color=blue]
      > Console App), OS X developer tools. THEY ALL complain!
      > pretty much the same reason too!
      >
      > /tmp/ccJ31810.o: In function `main':
      > /tmp/ccJ31810.o(.tex t+0xf): undefined reference to `cout'
      > /tmp/ccJ31810.o(.tex t+0x14): undefined reference to
      > `ostream::opera tor<<(char const *)'
      > collect2: ld returned 1 exit status
      >
      >
      > But... I thought the #include was supposed to bring in the definitions[/color]
      from[color=blue]
      > that file before checking the code for execution problems.
      >
      > I don't understand why it won't work, no matter the tutorials I see, books[/color]
      I[color=blue]
      > read, articles I read! Can someone point me in the right direction, a
      > utterly clueless newbie! (yes, I called myself that, so you can call me
      > that ONCE, too!)
      >
      > Appreciate it in advance, email is spam-blocked, remove the single period
      > PRIOR to the @ sign to reply by mail.
      >
      > --Tim[/color]


      Comment

      • JaSeong Ju

        #4
        Re: CX-Post: hello world

        Do this instead:

        #include <iostream>


        using namespace std;


        int main() {
        cout << "hello world" << endl;
        return 0;
        }



        iostream.h is deprecated, the standard C++ is to use
        iostream without .h

        the undefined reference to cout appeared because
        you had iostream.h instead of iostream.

        see http://www.research.att.com/~bs/bs_faq2.html

        Comment

        • Buster

          #5
          Re: CX-Post: hello world [OT in comp.lang.c, followup set]

          Tim Judd wrote:[color=blue]
          > I must not be grasping anything here.
          >
          > Just a simple application, as follows (as any first-program is...)
          >
          > ----
          > #include <iostream.h>
          >
          > int main() {
          > cout <<"hello world";
          > return 0;
          > }[/color]

          There shouldn't be a link error. Did you use
          "gcc" instead of "g++" for linking?

          --
          Regards,
          Buster.

          Comment

          • Kevin Goodsell

            #6
            Sorry about the OT c.l.c (was: CX-Post: hello world)

            Tim Judd wrote:
            [A bunch of C++ stuff.]

            Sorry about this, guys. I've asked the poster and repliers (in c.l.c++)
            to be more careful in the future. Thought I should let you know in the
            interest of saving you folks the trouble.

            -Kevin
            --
            My email address is valid, but changes periodically.
            To contact me please use the address from a recent posting.

            Comment

            • Martin Ambuhl

              #7
              Re: CX-Post: hello world

              Tim Judd wrote:
              [color=blue]
              > I must not be grasping anything here.
              >
              > Just a simple application, as follows (as any first-program is...)
              >
              > ----
              > #include <iostream.h>
              >
              > int main() {
              > cout <<"hello world";
              > return 0;
              > }
              > ----
              >
              > never compiles![/color]

              If you are going to post to comp.lang.c, post C. If you want to use
              C++, post to comp.lang.c++. For one of the two newsgroups you posted
              to, there is no <iostream.h> (or <iostream>) header, there is no
              predefined variable cout (or std::cout), and the symbol "<<" is always,
              other than in data or comments, a left shift operator. If the line
              containing 'cout' is intended to output the text "hello world", then the
              behavior of this program would, as well be undefined, since the last
              line of output does not end with an end-of-line '\n'.

              Here is your code, corrected for comp.lang.c:

              #include <stdio.h>
              int main(void)
              {
              puts("Hello world");
              return 0;
              }

              Follow-ups set to comp.lang.c++, since that where your question belongs.


              Comment

              • Dan Pop

                #8
                Re: CX-Post: hello world

                In <irOdnTCt0JziRe HdRVn-hw@comcast.com> Tim Judd <tjudd.01@hotma il.com> writes:
                [color=blue]
                >I must not be grasping anything here.[/color]

                Most likely, since you appear to be a patent idiot. Otherwise, you'd have
                never crossposted your question to comp.lang.c.

                Dan
                --
                Dan Pop
                DESY Zeuthen, RZ group
                Email: Dan.Pop@ifh.de

                Comment

                Working...