C++ Compiler for Macs?

Discussion in 'The Lounge' started by Pandemic21, Dec 12, 2008.

C++ Compiler for Macs?

Discussion in 'The Lounge' started by Pandemic21, Dec 12, 2008.

  1. Pandemic21

    Pandemic21 New Member

    Joined:
    Nov 2, 2008
    Messages:
    25
    Likes received:
    0
    Trophy points:
    0
    So ya, I was bored today and started looking for a C++ compiler for my mac.

    I tried XCode, and it failed for some reason. Idk why, tried reinstalling... failed.

    After that I moved onto Eclipse. Whenever I run a program though... (Hello World ftw!), it gives me the error:

    "Launch failed. Binary not found."

    And... wtf. I know the code is right, "hello world" is to simplistic for me to mess up...

    But I'm about to explode ( :twitcy: <-- me) if I don't figure this out. I had it set to JAVA before this, is that a possible problem?

    Anyway, help?


    -Pandemic
     
  2. 11-Sodium

    11-Sodium New Member

    Joined:
    Dec 17, 2007
    Messages:
    194
    Likes received:
    0
    Trophy points:
    0
    First I have some questions about how XCode failed:

    Did the application crash, or was it just not compiling? If you expected it to display the console with "Hello, World!" or whatever your program was supposed to do, you should know that the default settings for XCode have the console disabled by default. To enable it, go to Preferences->Debugging and where it says "On Start: Do Nothing" select "Show Console" or "Show Console and Debugger" from the drop down.

    Did you try running the unix executable file in the project directory after building? Assuming you don't have the above preference set, this would be the only way to test the program.

    Was there some other kind of error?


    I just downloaded the Eclipse IDE for C/C++ Developers (never used it before) and created a C++ project using the default Hello World code. It built with no errors or warnings and executed correctly. I don't know much about Eclipse, but from what I can see there are totally different IDEs for Java and C/C++. If you want to use Eclipse, make sure you have the correct IDE. The one I have has the subtitle "Ganymede."


    Finally, if all else fails and you just want to code something simple in C++, you could use a text editor such as textedit, vim, or emacs and the command line to build some applications. Here's an example of how to create a simple project in the command line:

    Code:
    mkdir SimpleExample
    cd SimpleExample/
    Create all the source files in this directory either by using an editor from the command line
    Code:
    vim main.cpp
    or by writing the files in TextEdit and saving them in the directory.

    When you are ready to compile and run type in
    Code:
    g++ -g -Wall -o example main.cpp secondaryFile.cpp another.cpp
    ./example
    Replace "example" with whatever you want to call the program.

    Any questions?
     
    Last edited: Dec 13, 2008
  3. Pandemic21

    Pandemic21 New Member

    Joined:
    Nov 2, 2008
    Messages:
    25
    Likes received:
    0
    Trophy points:
    0
    I tried using G++, but do you need to install a plug-in or something?

    And ya, I know I have the correct Eclipse... I guess i'll just try uninstalling/reinstalling.


    And I guess I'll but back to compiling in Terminal. I could just memorize the line of code... but that's boring. Do you mind explaining what all the -o is, and everything else, and why you have 3 x.cpp's? I'm assuming you can compile one if you wish?

    And stupid question time. But after you compile it in Terminal, how do you run it?


    -Pandemic
     
  4. 11-Sodium

    11-Sodium New Member

    Joined:
    Dec 17, 2007
    Messages:
    194
    Likes received:
    0
    Trophy points:
    0
    To see if g++ is installed, first type in "cd /usr/bin", then type (note: LS) "ls g++". If the next line isn't "g++@", then you don't have g++; if you do then ignore the rest of this paragraph. Next try "ls gcc" and see if the next line is "gcc@". If it is, then use gcc in place of g++ and everything should work. If it isn't, then you should reinstall the developer tools, including Xcode, and try again.

    If it still isn't working, check the PATH for
    Code:
    /usr/bin
    by typing "echo $PATH". If it isn't in the PATH, add it to the .bashrc file in your home directory, ~/ , on a new line exactly as follows:
    Code:
    export PATH=$PATH:/usr/bin
    To access the file, the easiest way is to use one of the command line editors, such as vim, nano, or emacs (nano is probably the easiest to use). To edit the file:
    Code:
    /usr/bin/nano ~/.bashrc
    control-o return will allow you to save, control-x will quit.
    If it still doesn't work and you have installed Xcode, try copying the error message you get here and I'll take a look.


    g++ -g -Wall -o example main.cpp

    What the above command does:
    g++ is the name of the compiler
    -g enables debugging
    -Wall (think of it as W all) turns on all warnings
    -o [filename] saves the output, the program you are making, to [filename].

    I chose to include 3 source files just to show that every new or modified source file (but none of the headers) in the project needs to be included. If you only have one file, only list that one file.
    There are other options to set on compilation, and you can find them by looking online or, if you are feeling adventurous, digging through the man page by typing in "man gcc".

    To run the program once the compiler is finished, you type in the name of the file you put right after the -o in the compilation step. If you had "-o hello_world" you would type "hello_world" or "./hello_world". The ./ indicates that the program is in the current directory, and may be necessary depending on the PATH variable. To add it, again in the .bashrc change the line that says
    Code:
    export PATH=$PATH:/usr/bin
    to
    Code:
    export PATH=$PATH:/usr/bin:./
     
  5. Pandemic21

    Pandemic21 New Member

    Joined:
    Nov 2, 2008
    Messages:
    25
    Likes received:
    0
    Trophy points:
    0
    The main problem with XCode was the unix executable file. The mac had no clue how to open it... I tried changing it to a .app, but that didn't work, and I had no ideas after that.


    Path I'm in:
    /bin:/sbin:/usr/bin:/usr/sbin

    And I did the whole:
    ( > = input, < = output )
    > cd /usr/bin
    > ls g++
    < ls: g++: No such file or directory

    (same results for gcc)

    ...So what do I need to install to get g++?

    Or you could just simplify this whole thing, call me an idiot, and tell me what to do with a unix executable file :p.


    -Pandemic
     
  6. 11-Sodium

    11-Sodium New Member

    Joined:
    Dec 17, 2007
    Messages:
    194
    Likes received:
    0
    Trophy points:
    0
    Unix executables should open in the terminal. They can be executed directly from the command line by typing in the directory location and then the executable name, for example an executable called "helloworld" in a folder called "hello" in your home directory could be run by typing "~/hello/helloworld"

    It sounds like you don't have the full set of Xcode tools installed. Did you install Xcode from the installation discs that came with the computer? If not, I would try installing from the discs and install everything related to development.

    Beyond that, I'm not sure what the problem is. I'd recommend asking someone who knows more about this than I do, such as some developers on an Apple forum. Sorry I can't be of more help.
    Here's a forum that could help: http://macosx.com/forums/
     
  7. Pandemic21

    Pandemic21 New Member

    Joined:
    Nov 2, 2008
    Messages:
    25
    Likes received:
    0
    Trophy points:
    0
    ARG!

    Ok, I got XCode installed. Fixed the g++ problem... created another one. Whenever I attempt to compile, I get an error:

    i686-apple-darwin8-g++-4.0.1: no input files

    You got any clue? If not I'll just post this on that forum you suggested...


    -Pandemic
     
  8. 11-Sodium

    11-Sodium New Member

    Joined:
    Dec 17, 2007
    Messages:
    194
    Likes received:
    0
    Trophy points:
    0
    That error means that you haven't specified any source files to compile. Remember, the next thing to go after the -o is the name of the program/executable you are creating. Then you type a space and then all the source files with spaces between them.