Change build output directory when building via terminal
Asked Answered
P

1

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!

Paralogism answered 14/4, 2012 at 4:5 Comment(2)
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/…Docilla
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!Paralogism
O
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.
Older answered 14/4, 2012 at 4:11 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.