Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

strangedaysuk

macrumors newbie
Original poster
Jun 5, 2010
9
0
Hi..

Please can you point me out a bit here. Sorry if its obvious to those in the know but I am lost even with searching loads of forums.

I have started to learn c++ and at the moment I am using OSX. Now it might be a mistake as I think I have found life much easier in Windows.

I have downloaded the Xcode, and through recommendations have also used Eclipse.

There are many Hello World programs around the net but for some reason anything compiling with <iostream> fails

ie:

#include <iostream>
using namespace std;
void main()
{
cout << "Hello World!" << endl; cout << "Welcome to C++ Programming" << endl; }


This is the standard C++ hello world program but wont compile on a mac??? Using any of the ways I have tried. Either in Eclipse, using Xcodes interface or through command line using the "C++" command.


However this works:



#include <stdio.h>

int main(void) { printf(\"Hello world\"); return 0; }




Now I am new at this but I cannot find sensible answers around when others ask about this problem other than people suggesting stuff is either missing or
odd. But this would be the same situation for any noobie learning to program on a mac currently as my install of this stuff is all fresh.

What I am concerned with is I want to get a book soon to dive into this topic and spend time learning the code properly but if the basic building blocks of C++ are somehow missing in Xcode I need to consider learning on a Windows based PC instead, which I was hoping would not be the case.

Can anyone clear this up for me.
 
What's not working is you're in C, not C++ on the Mac. stdio.h & printf are C, iostream & cout are C++.

When you create a project in Xcode, make sure you change the type from C to C++.
 
I compiled it here without problems in the terminal, so you have probably chosen the wrong language in Xcode. You will get an error if you don't use 'int main' and 'return 0' BTW, so your example can't be that standard.
 
Does your C++ source file have a ".cpp" extension? If not, then that is your problem.
 
Added the int and the return, but it worked here.

Kitchen:~ $ cat hw.C
#include <iostream>
using namespace std;
int main()
{
cout << "Hello World!" << endl; cout << "Welcome to C++ Programming" << endl;

return 0;

}
Kitchen:~ $ g++ hw.C -o hw
Kitchen:~ $ ./hw
Hello World!
Welcome to C++ Programming
Kitchen:~ $
 
This is the standard C++ hello world program but wont compile on a mac??? Using any of the ways I have tried. Either in Eclipse, using Xcodes interface or through command line using the "C++" command.

Two points:
1. When you get errors, post the actual error text.
2. When you use the command line, post your actual commands.

In general, you should also post the exact name of the file (which should also be in any command line), because development tools often use the extension to infer what language to compile it with.

For example, when I put your C++ code into a "hello.cpp" file and compile it with this command:
Code:
gcc hello.cpp
it produces this error message:
Code:
hello.cpp:3: error: '::main' must return 'int'
This message is telling you that the main function must return an int type.


I also pasted your C code, which you said worked, into a "hello.c" file and compiled it as:
Code:
gcc hello.c
which produced this error message:
Code:
hello.c:3: error: stray '\' in program
hello.c:3: error: missing terminating " character
hello.c:3: error: parse error at end of input
I don't know what to conclude from this, except that what you posted isn't actually C. (I could tell without compiling it that it wouldn't work, but it's always best to check what the compiler thinks by actually compiling.)

Maybe you're doing something different when you compile your code that somehow makes it compile. Or maybe the code you posted isn't the actual source code in the file. Never retype source code into posts; always copy and paste it exactly as it is.
 
I compiled it here without problems in the terminal, so you have probably chosen the wrong language in Xcode. You will get an error if you don't use 'int main' and 'return 0' BTW, so your example can't be that standard.

It's a "Microsoft standard", meaning it's what Visual Studio does. It apparently also appears in a lot of books.

http://homepage.ntlworld.com/jonathan.deboynepollard/FGA/legality-of-void-main.html

http://users.aber.ac.uk/auj/voidmain.shtml

Find more by googling: c++ void main
 
Sorry, couldn't resist ;)

pointers.png
 
Thanks all for your help.

I went into Xcode and realised that to build a command line utility you can choose between C, C++ etc......

So you were all spot on. Compiling from the command line didnt work but maybe I am using the wrong command line instructions.

Can you do a plain .txt file save with the correct extention and make this work?

again thanks I know where help is!
 
Yeah, just end the filename with .cpp and it should work. To compile a file named test.cpp from the command line try this:

Code:
g++ test.cpp -o test

This will create an executable named test. You can omit the -o flag and output name, you will then get a file named a.out that will be your executable.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.