Professional Web Applications Themes

how to compile c++ at the cmd line? - Mac Programming

hey all-- I'm a java/obj-c programmer and i picked up sams "teach yourself c++ in 10 min a day". i can compile a program in project builder, but for these simple little things i'd much rather compile and run from the command line, ie Terminal.app. only thing is, I'm not sure how to link my program with the proper libraries. see here: ================================================== ======== [marks:~] mehaase% cat main.cpp #include <iostream.h> int main (int argc, const char * argv[]) { int amt, total; cout << "Welcome to Cash Register!\n"; do { cout << "? "; cin >> amt; total += amt; ...

  1. #1

    Default how to compile c++ at the cmd line?

    hey all--

    I'm a java/obj-c programmer and i picked up sams "teach yourself c++ in
    10 min a day". i can compile a program in project builder, but for these
    simple little things i'd much rather compile and run from the command
    line, ie Terminal.app.

    only thing is, I'm not sure how to link my program with the proper
    libraries. see here:

    ================================================== ========
    [marks:~] mehaase% cat main.cpp
    #include <iostream.h>

    int main (int argc, const char * argv[]) {
    int amt, total;

    cout << "Welcome to Cash Register!\n";
    do {
    cout << "? ";
    cin >> amt;
    total += amt;
    cout << "Total=\t" << total << "\n";

    } while (amt != 0);

    return 0;
    }
    [marks:~] mehaase% gcc main.cpp
    ld: warning prebinding disabled because of undefined symbols
    ld: Undefined symbols:
    std::basic_istream<char, std::char_traints<char> >::operator>>(int&)
    std::basic_ostream<char, std::char_traits<char> >::operator<<(int)
    std::ios_base::Init::Init[in-charge]()
    std::ios_base::Init::~Init [in-charge]()
    std::cin
    std::cout
    std::basic_ostream<char, std::char_traits<char> >& std::operator<<
    <std::char_traits<char> >(std::basic_ostream<char,
    std::char_traits<char> >&, char const*)
    ___gxx_personality_v0


    ================================================== ========

    thanks in advance

    --
    |\/| /| |2 |<
    mehaase(at)sas(dot)upenn(dot)edu
    Mark Guest

  2. #2

    Default Re: how to compile c++ at the cmd line?

    Mark Haase wrote:
     


    gcc main.cpp -lstdc++

    Just to be clear in case the fonts are funny:
    that's a dash lower-case L not a dash numeric one and no space between
    the -l and stdc++
    Anthony Guest

  3. #3

    Default Re: how to compile c++ at the cmd line?

    Or instead of gcc, use g++ for compiling c++ and you don't have to worry
    about linking in the library.
    Anthony Guest

  4. #4

    Default Re: how to compile c++ at the cmd line?

    In article <supernews.com>,
    Anthony Loeppert <mailshell.com> wrote:
     
    >
    >
    > gcc main.cpp -lstdc++[/ref]

    Using

    g++ main.cpp

    will automatically include libstdc++.

    The result, by the way, will be a file called a.out, which you can run
    by typing './a.out'. To specify a different name, use the -o flag to
    gcc/g++.

    -Eric

    --
    Eric Albert stanford.edu
    http://rescomp.stanford.edu/~ejalbert/
    Eric Guest

  5. #5

    Default Re: how to compile c++ at the cmd line?

    In article <ejalbert-F18373.02215125072004localhost>,
    Eric Albert <stanford.edu> wrote:
     

    Thanks. OOC, does gcc call g++ or are they two totally separate programs?

    --
    |\/| /| |2 |<
    mehaase(at)sas(dot)upenn(dot)edu
    Mark Guest

  6. #6

    Default Re: how to compile c++ at the cmd line?

    On Sun, 25 Jul 2004 02:21:51 -0700, Eric Albert
    <stanford.edu> wrote:
     
    > >
    > >
    > > gcc main.cpp -lstdc++[/ref]
    >
    > Using
    >
    > g++ main.cpp
    >
    > will automatically include libstdc++.[/ref]

    I usually find it preferable to use make with its default rules,
    thus:

    make foo

    will by default look for a foo.o to link, failing to find it it
    will look for a foo.c or a foo.cc or a fo.cpp etc and compile
    accordingly, with a final output of foo as an executable. The
    default rules usually gather up all the usual libraries and
    linkage settings.

    YMMV,

    Alan G.

    Author of the Learn to Program website
    http://www.freenetpages.co.uk/hp/alan.gauld
    Alan Guest

  7. #7

    Default Re: how to compile c++ at the cmd line?

    On 2004-07-25, Mark Haase <net> wrote: 

    Is the usage of iostream.h from this book? You should dump this book so you
    don't pick up any bad habits.

    http://accu.org/cgi-bin/accu/rvout.cgi?from=0ti_t&file=t001917a

    Try _Accelerated C++_ instead.

    Dave Cook
    David Guest

  8. #8

    Default Re: how to compile c++ at the cmd line?

    In article <upenn.edu>,
    Mark Haase <net> wrote:
     
    >
    > Thanks. OOC, does gcc call g++ or are they two totally separate programs?[/ref]

    Yes. :)

    gcc and g++ are both names for the compiler. You can basically think of
    gcc as the generic C compiler and g++ as gcc set up automatically for
    C++. Everything you get when invoking g++ you can get by invoking gcc
    with the right flags.

    If you want to see exactly what's getting invoked, by the way, pass the
    -v switch to the compiler.

    -Eric

    --
    Eric Albert stanford.edu
    http://rescomp.stanford.edu/~ejalbert/
    Eric Guest

  9. #9

    Default Re: how to compile c++ at the cmd line?

    In article <local>,
    David Cook <net> wrote:
     

    They do use iostream in the book.

    Is iostream bad because of the potential for buffer overflows? Just
    wondering...

    I liked the "10 minute" book because its small and seems like a handy
    reference. It does dwell on stuff I know from C for way too long
    however. (Nearly half the book!) I'm going to take it back and see if
    there's something more suitable.

    The book you reccommend sounds just right, thanks.

    --
    |\/| /| |2 |<
    mehaase(at)sas(dot)upenn(dot)edu
    Mark Guest

  10. #10

    Default Re: how to compile c++ at the cmd line?

    In article <upenn.edu>,
    net says... 
    >
    > They do use iostream in the book.
    >
    > Is iostream bad because of the potential for buffer overflows? Just
    > wondering...
    >
    >[/ref]

    #include <iostream.h> is bad because it is not standard. The book should
    be using #include <iostream> and talking about the std namespace.
    James Guest

  11. #11

    Default Re: how to compile c++ at the cmd line?

    In article <Individual.NET>,
    James Weatherley <net> wrote:
     
    > >
    > > They do use iostream in the book.
    > >
    > > Is iostream bad because of the potential for buffer overflows? Just
    > > wondering...
    > >
    > >[/ref]
    >
    > #include <iostream.h> is bad because it is not standard. The book should
    > be using #include <iostream> and talking about the std namespace.[/ref]

    But then, many of the source code examples in _Accelerated C++_ aren't
    exception safe. So, take any source code from _Accelerated C++_ with a
    grain of salt.

    _Accelerated C++_ is a fine book, even so.
    David Guest

Similar Threads

  1. Line break for multi-line tab-aligned text?
    By Bob in forum Adobe Indesign Macintosh
    Replies: 13
    Last Post: January 2nd, 08:52 PM
  2. Forced line break changes previous line endings
    By Olav_Kvern@adobeforums.com in forum Adobe Indesign Macintosh
    Replies: 12
    Last Post: September 8th, 01:11 AM
  3. how to compile web service in command line
    By mei xiao in forum ASP.NET Web Services
    Replies: 2
    Last Post: July 21st, 09:00 AM
  4. DRKv5 3D Line Chart line width, rendering problems
    By dbarker webforumsuser@macromedia.com in forum Macromedia Flash Actionscript
    Replies: 0
    Last Post: January 26th, 04:48 PM
  5. Multiple line text entry shows only first line
    By Al in forum Adobe Photoshop 7, CS, CS2 & CS3
    Replies: 5
    Last Post: September 15th, 10:37 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139