c++ xcode error

Joined
Aug 20, 2009
Messages
1
Reaction score
0
Points
1
when i type in the following code taken from a c++ book:
Code:
#include <iostream>

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

It returns the error:

duplicate symbol _main in /users/alexdavey/documents/test/build/test.build
command /developer/usr/bin/g++-4.0 failed with exit code 1

why?
 
Joined
May 2, 2009
Messages
480
Reaction score
13
Points
18
Your Mac's Specs
MBP 2.33 4GB: MacPro 8 Core 2.8, 16GB: MacMini 2.26 4GB: MacMin 2.53 4GB: iPhone3GS 32GB
You failed to list that you are using the std namespace in C++. Add the following line to your code after the #include

Code:
using namespace std;

Also, make sure that you have selected a C++ type project from XCode. C and C++ link against different libraries so you will need to be sure to have the correct project type.

A good way to verify any compilation issues is to do it outside of XCode:

g++ whateverfile.cc
 

Raz0rEdge

Well-known member
Staff member
Moderator
Joined
Jul 17, 2009
Messages
15,745
Reaction score
2,071
Points
113
Location
MA
Your Mac's Specs
2022 Mac Studio M1 Max, 2023 M2 MBA
Under Xcode 3.1.3, I went New Project->Command line tool->C++ tool and this created a skeleton project for me that prints "Hello World" like you are trying. This builds and runs without any problems.

Can you try this.

@wmprice1240, if you add that line to the file, then you can use "cout" without prefacing it with "std::", it doesn't do anything beyond that..

Regards
 

Shop Amazon


Shop for your Apple, Mac, iPhone and other computer products on Amazon.
We are a participant in the Amazon Services LLC Associates Program, an affiliate program designed to provide a means for us to earn fees by linking to Amazon and affiliated sites.
Top