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!

2
  • 1
    I know this is going to sound rude, but looking up the docs is your best bet. Start here[gcc.gnu.org/onlinedocs/gcc-4.7.0/gcc/… Commented Apr 14, 2012 at 4: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! Commented Apr 14, 2012 at 4:18

1 Answer 1

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.
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.