Thread: New to C++

  1. #1
    Registered User
    Join Date
    Feb 2008
    Posts
    77

    New to C++

    Hello to all,

    I am a newbie to C++ and trying to get started with a tutorial book. My first hiccup is with my first program. I can't get it to compile. Heres the code:

    Code:
    #include <stdafx.h>
    #include <iostream>
    using namespace std ;
    
    int main()
        {
            cout << "Never fear, C++ is here!" ;
            return 0 ;
        }
    The error I receive is that the file "stdafx.h" cannot be found. I am working on a Mac OSX and have had no problems programming in C. Any suggestions would be great.

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Remove the #include <stdafx.h> line.
    It's for when compiling with precompiled headers with Visual Studio, but you aren't using VS.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Registered User
    Join Date
    Feb 2008
    Posts
    77
    I have taken out that particular header file but have received other errors.

    when i compile with the command:

    gcc file.cpp -o file

    I get the following error:

    ld: can't open output file for writing: print1
    collect2: ld returned 1 exit status

    I tried compiling with a different output name but received a lot of other errors:

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

    any suggestions???

  4. #4
    Registered User
    Join Date
    May 2008
    Posts
    81
    lol @ perator

  5. #5
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Yes: you have to use g++. gcc is for compiling C programs. g++ is for C++.

    [edit] That would be the cause of the second error. For the first error, perhaps you already have a program called "file" that you don't have write permission to, or something like that. [/edit]

    Note: you can use [noparse][/noparse] tags to disable the board's parsing of smileys if you want to, thus avoiding the above.
    Last edited by dwks; 06-20-2008 at 10:30 PM.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  6. #6
    Registered User
    Join Date
    Feb 2008
    Posts
    77
    Thank you for the help. I did not know that the terminal command would be:

    c++ file.cpp -o file

    I appreciate the help.

Popular pages Recent additions subscribe to a feed