Can I write Unix programs with XCode?

Discussion in 'Mac Programming' started by BadWolf13, May 5, 2010.

  1. BadWolf13 macrumors 6502

    Dec 17, 2009
    Forgive me if this is a stupid question, because I am someone unknowledged when it comes to the whole OSX written with a Unix base. In XCode, it gives you an option to create a "Command-line Tool" which is still in Ojb-C, just lacks the cocoa interface. My understanding is that that command line is the Unix that underlies OSX. Does that mean that those "Command-line Tools" are actually Unix programs that I could copy and run on a Unix computer? If not, is there a way to create a Unix program in XCode, and if so, how?
  2. plinden macrumors 68040


    Apr 8, 2004
    To be pedantic about it, every program that runs on OS X is a unix program, but if you want cross platform programs (e.g. run one on Linux or Solaris) you need to compile for that OS. That means not including any OS X specific frameworks and although Objective-C is available via GNUStep, you'd be unlikely to be able to produce much useful unless you use C.

    You can put together source code that will compile on multiple platforms but you're not going be able to compile in XCode and copy the executable over.

    There are plenty of cross-platform applications out there - like Apache. If you want to see how that's down, download the Apache source code and take a look at it. To be honest though, if you're asking this question you're unlikely to understand all the different options.
  3. mfram macrumors 65816

    Jan 23, 2010
    San Diego, CA USA
    Yes, a command-line tool is essentially a Unix program that can be run on the command line. However, the resulting file is a Mac binary executable. You can't just run the executable on a non-Mac computer. You have to move over the source code and recompile for whatever other platform you want the tool to be running on.
  4. Bill McEnaney macrumors 6502

    Apr 29, 2010
    OS X is a version of Unix, I think. A command interpreter, called a "shell," lets you type commands on a command line. Each time you run a program, that program gets its own shell that stops running when your program stops running. Unix is one thing. The utility programs that come with it, e.g., wc, cat, ls tr, lex, yacc, cal, ftp, tron, troff, more, prof , ed, mail, and others are utilities. I would say that strictly, Unix is made up of the kernel's programs, e.g., the programs that schedule other program, manage memory, etc.

    I'm explaining these details, my friend, because I'm not sure what you mean by "Unix program." Is Unix program any program that runs on a Unix platform?

    Everybody, please feel free to correct me if I'm mistaken.

  5. rdcsw macrumors newbie

    May 5, 2010
    This project builds a command-line tool written in C.

    In XCode, choose New Project. Then Mac OS X, Command Line Tool Type C. The prompt will read

    This project builds a command-line tool written in C.

    XCode will create a project with this in the main.c:

    #include <stdio.h>

    int main (int argc, const char * argv[]) {
    // insert code here...
    printf("Hello, World!\n");
    return 0;

    This should not only compile on any unix system, but any system with a c compiler as long as you "include" standard libraries and not something mac specific.
  6. Cromulent macrumors 603


    Oct 2, 2006
    The Land of Hope and Glory
    As long as you use APIs available on both platforms and choose a language that has a compiler on both platforms (yes, Objective C works on non-mac platforms) then you just need to recompile your source code.
  7. flyingturtle macrumors regular

    Apr 7, 2010
    If you want to go old school you can use VIM or Emacs (or any text editor) and write a program and compile it using the terminal.

    I've written some quick scripts using VIM then ran the gcc compiler to compile. I just had to look up the libraries I needed to use and I was good to go.
    !gcc -framework Foundation main.m -o main
    Great way to get a console app in Objective-C done quickly without needing to boot up x-code.

    Having said that, it's probably easier to write a console type program using something like Python as that will run on both Unix and Window computers.
  8. androticus macrumors newbie

    Mar 15, 2010
    try cmake

    the cmake utility lets you describe a project in a few lines (there are many examples available) then generate native compiler build files or project files for any platform, including Mac and Linux -- it would let you generate an XCode project or a make-based command line project -- it is not too hard to write code that runs on most Unix's, just stick to the standard C++ library and standard libraries that are supported on most Unix systems
  9. BadWolf13 thread starter macrumors 6502

    Dec 17, 2009
    Ok, a lot of answers, thank you. Let me just say it back to see if I understand it right. I can create a "command-line tool" using XCode, then transfer those files to a Unix or Linux computer, compile them using an obj-c compiler for Unix/Linux(which exist?), and I'll have a Unix program. Am I right, or am I wrong on any of those points?
  10. Cromulent macrumors 603


    Oct 2, 2006
    The Land of Hope and Glory
    Right. Assuming of course you don't use any Mac only APIs.

    You'll also obviously need to write a makefile to build your project too as there is no Xcode outside of Mac OS X.

Share This Page