C++....Compiling....and Unix

Discussion in 'Mac Help/Tips' started by newmanium, Apr 12, 2002.

  1. newmanium macrumors member

    Joined:
    Jan 18, 2002
    #1
    hey there, being a CS major i tend to do a heck of a lot of programming, particularly in C++. from what i read about Unix (which i really know nothing about), there is a nifty C++ compiler that came with the BSD shell built in the terminal. ive attempted to use developer tools but project builder doesnt seem to like alot of the libraries i use nor the classes i create. anyways, i wanted to give the terminal compiler a try. typing "cc -o [executable name] [file name] " is as far as i got, but with no luck. any help/suggestions, Unix users??
     
  2. Baseline macrumors member

    Joined:
    Apr 9, 2002
    Location:
    Southern Ontario
    #2
    Well, without any compiler error messages, it's hard to tell what exactly the problem is. Your syntax (in a standard *nix environment, at least) is correct.

    I've never tried building anything on OS X before (still waiting to get my first Apple).

    So, what kind of compile time messages are you receiving?
     
  3. Taft macrumors 65816

    Taft

    Joined:
    Jan 31, 2002
    Location:
    Chicago
    #3
    Library problem.

    You are probably running into a problem that the compiler doesn't recognize the file as C++ and doesn't use the the proper libraries and options for the C++ language.

    By default, the compiler favors straight C and Objective-C and sets up compilation for those languages. If you name the file with .C, .cc, or .cpp the compiler will usually set itself to the C++ language and include the appropriate libraries. I haven't had good luck with that on my system, though.

    To force cc to use the C++ compiler use the c++ command (it just calls cc with the right language and library paths set).

    Like: c++ -o hello hello.C

    If you are really bold, you can use the hybrid C++/Objective-C compiler (you still use cc). For that, use the .M extension on your files (you may again have to mess with library stuff). That way, you can build projects in Objective-C and with Apple's application tools and still call C++ libraries.

    All this information (and much, much more) is in the developer documentation from Apple.

    Matthew
     
  4. evildead macrumors 65816

    evildead

    Joined:
    Jun 18, 2001
    Location:
    WestCost, USA
    #4
    I have had the same problems

    you may have to give the full path of the Libs...

    project builder is pretty good. I have not had too many proplems with that. I have not used the terminal cc with OS X.. I have used gcc On Solaris. look up the man pages and see if you can find the syntax for entering the full path for the libs.

    -evildead
     
  5. newmanium thread starter macrumors member

    Joined:
    Jan 18, 2002
    #5
    the libraries i usually attempt to use are the basic "iostream", "math.h", "stdio.h", etc. libraries. most of the errors come from trying to create an output (i.e. cout<< "hello world"; type of thing). i get awkward error msgs like:

    " testing.cpp:1: parse error before `{'
    testing.cpp:1: stray '\' in program
    testing.cpp:1: stray '\' in program
    testing.cpp:1: stray '\' in program
    testing.cpp:1: stray '\' in program
    testing.cpp:2: stray '\' in program
    testing.cpp:2: stray '\' in program
    testing.cpp:2: stray '\' in program
    - (for the next 12 lines, its the same thing)
    "

    i tried the "c++ -o hello hello.c" method but had the same errors as above. and looking up the manual for the cc command is really short and only a description. i tried to methods to make the .cpp file, textedit and changing the extension manually, and using the built in text editor within the terminal.

    my main whine about wanting to use the c++ compiler here is because i would take any other alternative to Visual Studio. and if i can only get it to work natively, i could ditch VPC forever. are there any methods or procedures i might be missing before i compile it through the terminal?
     
  6. Beej macrumors 68020

    Beej

    Joined:
    Jan 6, 2002
    Location:
    Buffy's bedroom
  7. newmanium thread starter macrumors member

    Joined:
    Jan 18, 2002
    #7
    as in the file name extension, or the command done through the terminal?
     
  8. Beej macrumors 68020

    Beej

    Joined:
    Jan 6, 2002
    Location:
    Buffy's bedroom
    #8
    Both. To compile a C++ program called "simple.cpp", use the command:

    cpp simple.cpp

    Then to run it:

    ./a.out

    Then if that works, fiddle with the -o etc commands.

    I can't believe you're majoring in CS and have never had any experience with UNIX! I'm in my final year and, apart from the first few months when we use borland (ick!) in windoze, we've used Linux the whole time. (Well, I use OS X at home and Linux at Uni.)
     
  9. Taft macrumors 65816

    Taft

    Joined:
    Jan 31, 2002
    Location:
    Chicago
    #9
    Ah!

    You have a sly problem here. The clue is that you are using TextEdit. You are probably saving the file in RichText format which puts all sorts of extra characters into the saved document. Go to the format menu in TextEdit and select Make Plain Text. Save the document and it should compile. I tried it both ways and got the same error when I saved the file as Rich Text. You will probably still need to use the c++ command to get the right libraries or else you'll get a bunch of symbol not defined errors.

    For serious coding I suggest an editor outside of TextEdit.

    Also, the man pages for Apple's cc are non-existent. For help on the compiler, go to the Developer->Documentation->DeveloperTools directory and open up devtools.html. There is a link to the compiler help page.

    Matthew
     
  10. newmanium thread starter macrumors member

    Joined:
    Jan 18, 2002
    #10
    borland...::shudder::

    yeah, i've had to use borland too when coding pascal, c, and c++. but since we dont really run unix or linux machines except for the school's own servers, so i've done most of my work from MS visual studio.

    and i've tried making simple code trying out plain text, but it hates the iostream library. what alternatives are there to using iostream
     

Share This Page