PDA

View Full Version : C++ compiling and running on OS X


tharriso
11-18-2004, 12:17 PM
I am transitioning from C++ development on a Unix platform to OS X.
I've downloaded XTools, but having some initial difficulties in getting started.
----------------
PROBLEM: My C++ compiles (using g++2 version 2.95.2 compiler) but I can't run it.
-----------------


Summary:
Since g++ 3.3 doesn't like my 'cout' statements, I'm reverting to use g++2 (version 2.95.2) to compile the code(succcessful), but I can't run it. Here's what I get (any ideas?) Permissions are set correctly.


terry-mac.local:c++code % g++2 hello.cpp -o hello
terry-mac.local:c++code % hello
tcsh: hello: Command not found.

Permissions:
-rwxr-xr-x 1 tharriso staff 122652 18 Nov 13:07 hello*
-rw-r--r-- 1 tharriso staff 63 18 Nov 13:02 hello.cpp


Any ideas?

thanks,
Terry

hayne
11-18-2004, 12:35 PM
I've downloaded XTools
I guess you meant XCode Tools - did you get teh latest version (1.5)?
What version of OS X are you using?

Since g++ 3.3 doesn't like my 'cout' statements
Please show us the relevant parts of your code or (preferably) a small sample program that illustrates the problem. There should be no such problem with g++ 3.3.
Or do these cout statements output some templated variables? (There was a bug relating to templates).

terry-mac.local:c++code % hello
tcsh: hello: Command not found.

I guess you have been working on Unixes where the current directory is in your PATH by default. That is not the case in OS X. You can change your PATH to make . (dot = current directory) be part of your PATH, or just use a full path when running the programs.
E.g.:
./hello

tharriso
11-18-2004, 12:54 PM
1. Just downloaded the Xcode, so pretty sure it was the latest version (how could I check after the fact?)

2. I added '.' to my PATH in my .cshrc file, so now I can run the code by entering simply 'hello' on the command line (Thanks for tipping me off on that).

-rwxr-xr-x 1 tharriso staff 122652 18 Nov 13:07 hello*
-rw-r--r-- 1 tharriso staff 58 18 Nov 13:47 hello.cpp
terry-mac.local:/Users/tharriso/Desktop/c++code % hello
Hello World


3. Compiling w. g++ 3.3
OK, here's some very simple code that won't compile w. version 3.3 but will with 2.95.2:

# hello.cpp
#include <iostream>
void main()
{cout<<"Hello World\n"; }


3.3 chokes on 2 things:
hello.cpp:3: error: `main' must return `int'
hello.cpp: In function `int main(...)':
hello.cpp:3: error: `cout' undeclared (first use this function)


*OK, I can make main return an int, but what can I do to fix the 'cout' issue?
*I'm trying to get familiar on how to make my code g++ 3.3 friendly.


Of course, the real bummer here is that if I'm importing in some c++ (massive length) that uses the above conventions, I'll have to revert to the 2.95.2 compiler.

Any tips for the above (parts #1 and #3) would be most appreciated.

thanks,
Terry

black0
11-18-2004, 02:25 PM
You just have a namespace issue. You need to write
std::cout<<"Hello World\n";
or put
using namespace std;
at the top of your file.

hayne
11-18-2004, 03:21 PM
1. Just downloaded the Xcode, so pretty sure it was the latest version (how could I check after the fact?)

The usual ways:
- the "About" menu when running XCode
- File/Get Info on the XCode icon in Finder

the real bummer here is that if I'm importing in some c++ (massive length) that uses the above conventions, I'll have to revert to the 2.95.2 compiler.

1) The function 'main' must always return an 'int'. This has been true since time immemorial and this is mandated by the C & C++ standards. Unfortunately, many textbooks incorrectly show 'main' with a 'void' return type and many compilers incorrectly allow this error to pass unnoticed.
Nevertheless, it is an error and must be corrected. I.e. any code that you are importing that uses a 'void' return for 'main' is broken code and needs to be fixed.

2) As black0 has already pointed out, your problem with the #include's is that the current version of standard C++ mandates the use of namespaces.
It would be a good idea to update your code to conform to this - perhaps you could do this en batch by using a Perl script or somesuch.
Otherwise, I suspect that there is a compiler setting (flag) to turn off the namespaces feature - you just have to look for it in the docs (or maybe google will help).

tharriso
11-19-2004, 02:48 PM
Great help guys.
I got the "hello world" compiled and running successfully.

Now back to the real challenge, installing Apache mod_auth_shadow!

Terry