problems with c++

Discussion in 'Mac Programming' started by Mann, May 18, 2009.

  1. Mann macrumors newbie

    Joined:
    May 18, 2009
    #1
    I am just learning to program using c++ and am having difficulty with the terminal. I have been searching different forums, but cannot seem to find an answer.

    to compile the program I first configured textedit to allow me to write .cpp files. Basically, I went into the preferences and configured everything how the a page I found told me to. I also used TextWrangler which came up with the same results.

    I saved the file "hello.cpp" on the desktop. I then went to the terminal where I first entered:

    cd desktop

    I then put:

    g++ -Wall -W -pedantic -o hello hello.cpp

    And I received error messages no matter what program I wrote.

    First, I am trying to learn using Bjarne Stroustrup's, "Programming: Principles and Practice Using c++" In the second chapter the code he says to enter is:

    #include "std_lib_facilities.h"

    int main()
    {
    cout << "Hello, World!\n";
    return 0;
    }

    That came back with :

    hello.cpp: In function ‘int main()’:
    hello.cpp:6: error: ‘cout’ was not declared in this scope


    So I tried:

    #include <iostream.h>

    main()
    {
    cout << "Hello World!";
    return 0;
    }

    That came back with :

    hello.cpp:3: error: ‘::main’ must return ‘int’
    warren-woodrich-pettines-macbook-air:desktop wwp$ #include <iostream.h>
    warren-woodrich-pettines-macbook-air:desktop wwp$
    warren-woodrich-pettines-macbook-air:desktop wwp$ main()
    > {
    > cout << "Hello World!";
    -bash: !": event not found
    > return 0;
    > }

    when I entered ./hello it said there was no file

    I have tried a couple other variations on the internet and have found none that work. What am I doing wrong? Is Stroupstrup's book compatible with Mac, and if not why?

    Thanks so much for the help. Hopefully I am just making a newb mistake
     
  2. kainjow Moderator emeritus

    kainjow

    Joined:
    Jun 15, 2000
    #2
    I'm not familiar with that book, but either it's using outdated standards (or non-standards) or it's just really old, or both :)

    Try this:
    Code:
    #include <iostream>
    using namespace std;
    
    int main()
    {
        cout << "Hello World!";
        return 0;
    }
     
  3. Sander macrumors 6502

    Joined:
    Apr 24, 2008
    #3
    Well, if I look here, it seems like it's quite new. It's also by Mr. C++ himself, of course.

    I must say that std_lib_facilities.h was new to me, too.
     
  4. vishvesh macrumors newbie

    Joined:
    Mar 17, 2008
    Location:
    India
    #4
    it looks like it is custom header file(std_lib_facilities.h), does it exist on your desktop

    you could also try

    gcc hello.cpp -o hello.a

    for compiling
     
  5. Mann thread starter macrumors newbie

    Joined:
    May 18, 2009
    #5
    Thanks for the responses, but it looks like none of these things are helping.

    I used the new code you suggested as well as the gcc hello.cpp -o hello.a
    and it came back with:

    i686-apple-darwin9-gcc-4.0.1: hello.cpp: No such file or directory
    i686-apple-darwin9-gcc-4.0.1: no input files

    So I tried prefacing it with cd desktop, and it gave me:

    Undefined symbols:
    "___gxx_personality_v0", referenced from:
    ___gxx_personality_v0$non_lazy_ptr in ccHaqeEx.o
    "std::ios_base::Init::~Init()", referenced from:
    ___tcf_0 in ccHaqeEx.o
    "std::basic_ostream<char, std::char_traits<char> >& std::eek:perator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*)", referenced from:
    _main in ccHaqeEx.o
    "std::ios_base::Init::Init()", referenced from:
    __static_initialization_and_destruction_0(int, int)in ccHaqeEx.o
    "std::cout", referenced from:
    __ZSt4cout$non_lazy_ptr in ccHaqeEx.o
    ld: symbol(s) not found
    collect2: ld returned 1 exit status

    Is there a problem with my compiler? I've tried running these using Xcode and came up with similar problems.

    Thanks again for taking the time to respond
     
  6. vishvesh macrumors newbie

    Joined:
    Mar 17, 2008
    Location:
    India
  7. sammich macrumors 601

    sammich

    Joined:
    Sep 26, 2006
    Location:
    Sarcasmville.
    #7
    hello.cpp
    Code:
    #include <iostream>
    using namespace std;
    
    int main () {
    [INDENT]cout << "Hello world!" < endl;
    return 0;[/INDENT]
    }
    command line
    Code:
    g++ -o hello hello.cpp
    ./hello
    ^ needs to be run where your hello.cpp is, the command line doesn't magically know where to look.

    btw g++ and gcc are the same thing

    basic format of the g++ compiler:

    g++ -o [name of your compiled program] [source code]
     
  8. vishvesh macrumors newbie

    Joined:
    Mar 17, 2008
    Location:
    India
    #8
    I don't understand what is the problem, would have to check it out on some gcc forums.

    I managed to compile this

    #include <stdio.h>
    int main ()
    {
    fprintf(stderr,"Hello world!");
    // cout << "Hello world!";//<< endl;
    return 1;
    }

    for now you could continue using this, may be some of these call have been deprecated by gcc(just a assmption).
     
  9. pilotError macrumors 68020

    pilotError

    Joined:
    Apr 12, 2006
    Location:
    Long Island
    #9
    Didn't we just go through this with someone else?

    Getting started with C++

    Did you install the developer tools or are you trying to use the built-in compiler?

    ** Added

    If you want to use that header, your going to need to add the path where the directory resides on the command line

    If you installed source code for the book, you need to put the location of the headers into the command line.

    ie. The books source code is installed into /Users/MyName/examples and the header file is located in /Users/MyName/examples/include, your command line should look like this:


    g++ -o testFile -I/Users/MyName/examples/include testFile.cpp

    This will look for header files in the default directories and also any headers in the examples/include directory.

    Mike...
     
  10. Mann thread starter macrumors newbie

    Joined:
    May 18, 2009
    #10
    That other forum helped a lot. I guess i didn't find it in my search.

    Thanks.
     
  11. sammich macrumors 601

    sammich

    Joined:
    Sep 26, 2006
    Location:
    Sarcasmville.
    #11
    No wonder I was getting that sense of deja'vu...
     
  12. kainjow Moderator emeritus

    kainjow

    Joined:
    Jun 15, 2000
    #12
    Strange. Maybe it's some file that comes with the CD in the book or something...
     
  13. gnasher729 macrumors P6

    gnasher729

    Joined:
    Nov 25, 2005
    #13
    The name of the file was not one of the standard header names, C++ standard headers don't end in .h anyway, and the include command was

    #include "somefilename.h"

    and not

    #include <somefilename.h>

    which means it is not a standard header, but a file name that the programmer is supposed to supply.
     
  14. pilotError macrumors 68020

    pilotError

    Joined:
    Apr 12, 2006
    Location:
    Long Island
    #14
    There's a few of the books that include those catch-all header files. They do this to avoid having to retype the same 5 headers into the code.

    It saves them page space for the more important lines of code...

    It's really confusing when you jump into the middle of a book and can't remember what the heck was in that generic header to begin with.
     
  15. Mann thread starter macrumors newbie

    Joined:
    May 18, 2009
    #15
    the answer

    I sent some emails back and forth with Stroustrup and it turns out

    std_lib_facilities.h

    is a catch all header that simplifies everything till later in the book. I think someone mentioned this earlier in the thread.

    He emailed me a copy of the file and now everything is working fine. If anyone reads this who is also having trouble, just hit him with an email and he's really responsive.
     
  16. lee1210 macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #16
    Or google that filename and find this link:
    http://www.stroustrup.com/Programming/std_lib_facilities.h
    (EDIT: It looks like Dr. Stroustrup's site is down... if you search for the URL, the cached copy is available from google)

    Even if Dr. Stroustrup isn't teaching a class during the summer, he probably still has other things to be working on rather than responding to a bevy of requests for this file (not knocking the OP, this is for reference for others that may need to seek out the file).

    -Lee
     
  17. Mann thread starter macrumors newbie

    Joined:
    May 18, 2009
    #17
    Good point. His website was down when I posted.
     

Share This Page