5

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!

Martin Tuskevicius
  • 2,590
  • 4
  • 29
  • 46
  • 1
    I know this is going to sound rude, but looking up the docs is your best bet. Start here[http://gcc.gnu.org/onlinedocs/gcc-4.7.0/gcc/Overall-Options.html#Overall-Options] –  Apr 14 '12 at 04:09
  • It's ok. I'm still learning C and I am used to just pressing Command + S and pressing a green arrow on Eclipse to run my Java projects. I didn't know there were docs for the compiler. Thanks! – Martin Tuskevicius Apr 14 '12 at 04:18

1 Answers1

11

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.
Kevin
  • 53,822
  • 15
  • 101
  • 132