Many short programs in Xcode

Discussion in 'Mac Programming' started by Nettle picker, May 11, 2014.

  1. Nettle picker macrumors newbie

    Joined:
    May 11, 2014
    #1
    Hi all,

    I study C++ and use Xcode. Can't boast much of a progress so far but that's irrelevant :). Naturally I have to write plenty of short programs and this is where my question stems from: how to organize this rapidly growing library of short programs? To create a separate project for every dozen-row program? To add a new file? I tried adding "C++ class" file, didn't work for me - the program compiled OK but there was no output.

    Anyone had the same problem?
     
  2. ArtOfWarfare macrumors 604

    ArtOfWarfare

    Joined:
    Nov 26, 2007
    #2
    Xcode is overkill for such short programs - don't bother with it. Write your programs in a text editor and compile them through the command line.
     
  3. briloronmacrumo macrumors 6502

    briloronmacrumo

    Joined:
    Jan 25, 2008
    Location:
    USA
    #3
    Another possible solution ( particularly if all the "short" programs share some common code ), is to put several ( or all depending how you want to manage ) of the short programs in one Xcode project with multiple targets. This works well if the short programs are all command line ( i.e. based on Foundation only with no AppKit/UIKit ) without a nib. YMMV.
     
  4. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #4
    1. Is each program a single self-contained file? Or does a single program ever encompass multiple files?

    2. Are there any common or utility classes or functions that will be used in multiple programs? That is, in addition to the "multiple file" criterion, are some of those multiple files reused?

    If the answers to either of the above are "Yes", then you can make an Xcode Target for each program.

    What is a Target:
    https://developer.apple.com/library/mac/featuredarticles/XcodeConcepts/Concept-Targets.html

    Making Targets in a Project:
    https://developer.apple.com/library...ect_editor/articles/EditingBasicSettings.html

    You should also consult Xcode's builtin Help on understanding, making, and managing Targets. For example, I'm pretty sure you can still duplicate an existing target and modify it, rather than always making a new one from scratch.
     
  5. Nettle picker thread starter macrumors newbie

    Joined:
    May 11, 2014
    #5
    Thank you very much for your comments. Looks like I'm gonna have to use text editor and run gcc(g++) in command line.

    chown33, so far every program is a single file. Haven't gotten to classes yet. But thank you for the URLs.
     
  6. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #6
    Check to see if you have the 'make' cmd. If so, then it has some builtin rules for making programs from single source files. For example, if you have a file /foo.cpp' in the current directory, and you enter this command line:
    Code:
    make foo
    
    then make will know how to make 'foo' by compiling with the correct compiler.
     
  7. Nettle picker thread starter macrumors newbie

    Joined:
    May 11, 2014
    #7
    chown33, it didn't work:

    Code:
    $ make test1.cpp
    make: Nothing to be done for `test1.cpp'.
    So I always run:
    Code:
    $ g++ ./test1.cpp -o ./test1
    $
    $ ls ./ | grep test | grep -v .cpp
    test1
    
     
  8. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #8
    If the file was foo.cpp, the command would be:
    Code:
    make foo
    
    That's pretty much what I wrote before.

    So if you have test1.cpp, use this command:
    Code:
    make test1
    
    The make command doesn't know how to "make" a cpp file. It DOES know how to make an executable (test1) FROM a cpp file.

    The cpp file is a source file, not a target. Make can apply actions TO sources that PRODUCE targets. It can't PRODUCE sources.
     
  9. Nettle picker thread starter macrumors newbie

    Joined:
    May 11, 2014
    #9
    chown33, my bad, totally :). It worked with "make" :).

    Thank you for clearing this up.
     
  10. subsonix macrumors 68040

    Joined:
    Feb 2, 2008
    #10
    Make makes the most sense if you have multiple files. Although you'll write less in this case, it'll also give you less options than just typing out what to do with g++ imo.
     
  11. Senor Cuete macrumors regular

    Joined:
    Nov 9, 2011
    #11
    I disagree with Art. I love Xcode because of the integration with the debugger, etc. I have a special project set up just to test small code examples. I keep them in the project and call them from main() to test them. The finder is a great way to organize files in folders.
     

Share This Page