1. Welcome to the new MacRumors forums. See our announcement and read our FAQ

Compiling and running C++ programs

Discussion in 'Mac Programming' started by thrill4rishabh, Jul 24, 2012.

  1. macrumors newbie

    #1
    Hey,

    I learnt C++ on Borland C++. Now I have shifted to mac and am using Eclipse, which is good, but not quite the same.

    I read somewhere that we can write a program in TextEdit and compile and link it via terminal?

    Can anyone tell me how to do that? Please go step by step and very detailed.

    Thanks
     
  2. macrumors demi-god

    wrldwzrd89

    #2
    Here's the basic outline:

    Make SURE you convert to plain text in TextEdit when writing code. Compilers and rich text format don't get along.
    You'll need a C++ compiler, obviously - and it needs to be in your PATH environment variable to be usable in the Terminal. If you use Xcode's command line tools, this part is done for you.
    Compiling and running is really easy:
    1. compiler_name options program_name.ext
    2. ./program_name
    ...where the compiler_name and options depend on your compiler.

    For Xcode, either GCC or Clang will work.
     
  3. macrumors newbie

    #3

    My compiler is gcc, but what do you mean by options?
     
  4. macrumors 68000

    #4
    enter 'man gcc' in a Terminal window - there's lots of options ;)
     
  5. macrumors newbie

    #5
    GCC(1) GNU GCC(1)

    NAME
    gcc - GNU project C and C++ compiler

    SYNOPSIS
    gcc [-c|-S|-E] [-std=standard]
    [-g] [-pg] [-Olevel]
    [-Wwarn...] [-pedantic]
    [-Idir...] [-Ldir...]
    [-Dmacro[=defn]...] [-Umacro]
    [-foption...] [-mmachine-option...]
    [-o outfile] [@file] infile...

    Only the most useful options are listed here; see below for the
    remainder. g++ accepts mostly the same options as gcc.

    In Apple's version of GCC, both cc and gcc are actually symbolic links
    to the llvm-gcc compiler. Similarly, c++ and g++ are links to
    llvm-g++.

    Note that Apple's GCC includes a number of extensions to standard GCC
    (flagged below with "APPLE ONLY"), and that not all generic GCC options
    :


    Again, no idea what to do.
     
  6. macrumors demi-god

    wrldwzrd89

    #6
    The only option that you might want to use if your program consists of multiple source files is -o (output file). Example:
    Code:
    gcc -o myprog myprog.cpp myprog2.cpp myprog_funcs.cpp
    Other than that, you don't need much in the way of options - the defaults are good for someone just learning, like you.
     
  7. macrumors newbie

    #7

    I entered man gcc, but how to enter further commands? any commant to close the man command?
     
  8. macrumors demi-god

    wrldwzrd89

    #8
    That's easy - type q to get out of the manual page viewer and back to a command prompt.
     
  9. macrumors 603

    #9
    Or install Bwana and use your web browser to read man pages:
    http://www.bruji.com/bwana/
     
  10. macrumors demi-god

    wrldwzrd89

    #10
    Thanks for that link - that looks useful to programmers. Bookmarked.
     
  11. macrumors newbie

    #11

    I'm sorry but I cant't seem to get it to work.

    I wrote a simple hello world program in TextEdit and saved as hello.txt on desktop.

    What exactly do I type in terminal.
     
  12. macrumors 603

    #12
    I haven't used the 'man' command in years. Once Bwana is installed, it even recognizes command-line URLs, so this opens a man page in your browser:
    Code:
    open man:gcc
    
     
  13. Pivs, Jul 24, 2012
    Last edited: Jul 24, 2012

    macrumors member

    #13
    Save it as hello.cpp (plain text), compile with g++ and run.

    Code:
    g++ hello.cpp
    ./a.out
     
  14. macrumors newbie

    #14

    hello.cpp: No such file or directory
     
  15. macrumors member

    #15
    Make sure you rename hello.txt to hello.cpp and are in the same directory as hello.cpp when you run the g++ command.
     
  16. macrumors newbie

    #16
    Thanks man!! done finally!! But can anyone recommend an absolutely basic IDE, like Borland, in which i just type all the code and run the program?
     
  17. macrumors 604

    SnowLeopard2008

    #17
    Real programmers use VI. The problem you are having is that you need to change the .txt to .cpp. And next time, learn VI.
    edit: too slow...
    ----------

    Net beans? Btw, sorry for the double post. I was typing a bit slow.
     
  18. macrumors demi-god

    wrldwzrd89

    #18
    The most notable Mac C/C++ IDE options out there are Xcode, Eclipse (requires Java) and NetBeans (requires Java). I'm sure there are more I don't know about.
     
  19. macrumors 604

    SnowLeopard2008

    #19
    OP already tried Eclipse and it's not absolutely basic. Net beans is tho.
     
  20. thrill4rishabh, Jul 24, 2012
    Last edited: Jul 24, 2012

    macrumors newbie

    #20
    Anyone here has some experience with Eclipse?

    I have one problem. I can create and execute a program if:

    1. File>>New>>C++Project>>Executable Files>>Hello World C++ Project.

    But when I choose Empty Project I get lost.

    I'm sorry. I'm totally confused and irritated. I am intermediate level in C++ ( I have done until dynamic memory allocation ), but the moment I started with mac, it was like I was starting all over again.

    At this point I would be ready to try all the IDE's there are to get one which is simple and effective.
     
  21. macrumors demi-god

    wrldwzrd89

    #21
    I use Eclipse, but not for C development. An empty project is just that - a project with no files in it. To add source files to the project you just created, go to New -> C++ Source File, give the file a name, and start writing code.
     
  22. macrumors 68040

    lee1210

    #22
    Are your projects 10s,100s,1000s,10000s,etc. lines of code? I've worked on professional projects with millions of lines of code, and we used vi/vim to edit and make/rake to build. My point is that you're complaining that the tools youve found aren't just perfect for you to ply your trade. I encourage you to stop that sort of thinking. It will serve you well to be comfortable with a wide range of tools, so you can be effective in lots of environments.
    You've stated Eclipse is too complex and a text editor and gcc is too simple. You're basically saying: without Borland I'm not comfortable programming. I would insist you work through that discomfort and come out the other side a more versatile programmer. If you don't want anything different, you can get a Windows VM and install Borland. You'd be robbing yourself of an opportunity to learn and grow if you do, though.

    Code more, search for a Borland-alike less. Good luck.

    -Lee

    P.S. unless you invent a language and code in it for years, don't say you're advanced. It's a death sentence, and makes the offer to others to attack your understanding.
     
  23. macrumors newbie

    #23
    Yeah that part I can do. But I'm not able to run the programs.
     
  24. macrumors demi-god

    wrldwzrd89

    #24
    Oh, that's most likely because Eclipse doesn't know where your C++ compiler is located. This is set in the preferences -> C++.
     
  25. macrumors newbie

    #25

    You are right to some extent. I am willing to work with Eclipse, which I find decent, but I'm kinda stuck and don't know what to do next, and what irritates me is that its not the coding part, its the building and running part.
     

Share This Page