CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    May 2010
    Posts
    23

    Unhappy new to c++, getting error

    Hello, here is my simple program which I am trying to compile and run.

    Code:
    # include <iostream>
    
    int main() 
    {
    	cout << "Hello World" << end1;
    	int sum = 0;
    	
    	for(int index = 0; index <= 10; index++)
    	{
    		sum += index;
    	}
    	
    	cout << "The sum is: " << sum << end1;
    	
    	return 0;
    }
    the name of file is firstCPlusPlus.cpp

    When I enter 'g++ firstCPlusPlus.cpp into terminal window, get this error:

    Undefined symbols:
    "_main", referenced from:
    start in crt1.10.6.o
    ld: symbol(s) not found
    collect2: ld returned 1 exit status

    Any help would be great. Thinking my code is alright but my compiler side of things is messed. New to the game of c++ and frustrated with this error

    Thanks to all for any advice!
    Last edited by Marc G; December 18th, 2010 at 10:50 AM. Reason: Added code tags

  2. #2
    Join Date
    Nov 2003
    Location
    Belgium
    Posts
    8,150

    Re: new to c++, getting error

    That should work.
    However, depending on how old your compiler is, you should get other errors because cout and endl should be prefixed with "std::" or you should put "using namespace std;" at the top of your CPP sourcefile.

    Can you tell us the output of:
    Code:
    g++ --version
    Marc Gregoire - NuonSoft (http://www.nuonsoft.com)
    My Blog
    Wallpaper Cycler 3.5.0.97

    Author of Professional C++, 4th Edition by Wiley/Wrox (includes C++17 features)
    ISBN: 978-1-119-42130-6
    [ http://www.facebook.com/professionalcpp ]

  3. #3
    Join Date
    May 2010
    Posts
    23

    Re: new to c++, getting error

    Hey Marc,

    The output I got from 'g++ --version' is:

    i686-apple-darwin10-g++-4.2.1 (GCC) 4.2.1 (Apple Inc. build 5664)
    Copyright (C) 2007 Free Software Foundation, Inc.
    This is free software; see the source for copying conditions. There is NO
    warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

  4. #4
    Join Date
    Apr 1999
    Posts
    27,449

    Re: new to c++, getting error

    Quote Originally Posted by msae View Post
    Hey Marc,

    The output I got from 'g++ --version' is:

    i686-apple-darwin10-g++-4.2.1 (GCC) 4.2.1 (Apple Inc. build 5664)
    Copyright (C) 2007 Free Software Foundation, Inc.
    This is free software; see the source for copying conditions. There is NO
    warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
    That code you posted is not compilable.
    Code:
    # include <iostream>
    
    int main() 
    {
    	cout << "Hello World" << end1;
    1) There is no specification of the "std" namespace anywhere in that code.

    2) What is end1? Don't you mean endl?

    Are you typing your code into the message? If not, don't.

    Take the code directly from your code editor, copy it using the copy command, and paste it into the message. Do not type in code, as you see your code has omissions and typographical errors.

    Regards,

    Paul McKenzie

  5. #5
    Join Date
    Dec 2010
    Posts
    20

    Re: new to c++, getting error

    you miss the line: using namespace std;

  6. #6
    Join Date
    May 2010
    Posts
    23

    Re: new to c++, getting error

    Hey guys,

    I found the problems. Thanks for the tips. It helped a lot. The main issue was that the file was in a different location than the location I was designating when compiling. Had same file names but the one I was compiling had nothing in it. Still do not understand the compile error I got though when nothing there to compile?

    Either way, dumb mistake! But thank you for the tips, I fixed other errors along the way!

    Ciao till next time,
    msae

  7. #7
    Join Date
    Nov 2003
    Location
    Belgium
    Posts
    8,150

    Re: new to c++, getting error

    Every C/C++ program should have a function called "main" where execution of your application starts. An empty C++ file doesn't have a main function so the linker is complainig about it missing.
    Marc Gregoire - NuonSoft (http://www.nuonsoft.com)
    My Blog
    Wallpaper Cycler 3.5.0.97

    Author of Professional C++, 4th Edition by Wiley/Wrox (includes C++17 features)
    ISBN: 978-1-119-42130-6
    [ http://www.facebook.com/professionalcpp ]

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured