Search code examples
cmacosbuildterminaloutput-directory

Change build output directory when building via terminal


Recently, I found a program that is kind of a mix between an IDE and a text editor. It supports the syntax of the language and it does formatting, but it does not build and run the program for you. I am running Mac OS X 10.6.8. I looked up how to build C code using the Terminal application. The format is:

gcc [file]

Pretty simple. The problem is that I cannot change the directory of where the built file is outputted, nor can I change the name. By default, every file compiled is outputted in the home directory by the name of 'a.out.' How can I specify the output directory and name?

Thanks!


Solution

  • gcc has a -o option to change the output name. You can specify the path there. E.g.:

    $ ls
    program.c
    $ gcc program.c -o program
    $ ls
    program   program.c
    $ mkdir bin
    $ gcc program.c -o bin/program
    $ ls bin
    program
    $ 
    

    You should probably also want to know about a few other common options:

    • -std=c99, -std=gnu99: Use the c99 standard / with gnu extensions.
    • -Wall, -Wextra, -pedantic: Enable extra warnings.
    • -O0 -ggdb: Compile with debugging symbols. Look up how to use gdb.
    • -O2: Compile with processor-independent optimizations. Not compatible with -O0.