Compiler Options at the Command Prompt

The traditional C compiler is cc. Its GNU replacement in the Linux world is gcc. A better option is the LLVM clang compiler. As commands typed at a prompt, you control these compilers by setting command line options or switches.

The most common command line option is a source code filename. I’ll use clang because it’s my favorite:

clang sc.c

The above command compiles and links the source code file sc.c. Both compiling and linking halt upon an error. Otherwise, the default output file is generated, a.out or a.exe (Windows).

In ancient times, the cc compiler only compiled. Linking was accomplished by using the ld (linker) program in Unix. The MS-DOS/Windows equivalent was called link. The linker created the program file. Today, however, compiling and linking are performed by the compiler itself. Internally, I assume the compiler calls the linker. The result is one command to build a simple program from a single source code file.

The option I specify frequently when compiling is the -Wall switch. This switch activates all warning messages for the compiler. For clang, the warnings are specific and detailed, which aids in debugging. Another good option is -Weverything, which activates all diagnostics.

Here are other common command line switches:

-o This option directs the compiler to set a specific output filename, something other than the default a.out or a.exe. For example:

clang sc.c -o program

Above, source code file sc.c is compiled and linked. The program created is named program.

-l Use the little L to direct a specific library file to be linked it. The library file is named immediately after the -l, and it must be in one of the default locations or in a specific location specified by using the -L switch. The traditional default locations are /usr/lib/ and /usr/local/lib

-c The little C option directs the compiler to compile only; the object code file isn’t linked to form a program. Use this option when compiling multiple modules to be linked later to form a single program.

-g Little G is the debug switch, which directs the compiler and linker to include debugging information and symbols in the final program.

-iquote This option sets a location for the compiler to locate header files. It’s immediately followed by a pathname. This option replaces -I, which has been deprecated (but still works).

-L This option directs the compiler to look in the specified directory (folder) for library files.

--version Specify this switch to have the compiler spew out its version number. No program is generated, yet I use this option to confirm that the compiler is installed and to double check the version number should I have problems compiling between platforms.

I recognize that most programmers don’t use the command prompt, definitely not as much as I do. I use it because it’s quick and fun, reminding me of earlier times. Yet, even with an IDE, you’re still invoking a command line version of the compiler, which I’ll show in next week’s Lesson when I cover options in the Code::Blocks IDE.

3 thoughts on “Compiler Options at the Command Prompt

  1. gcc has a number of optimizing options which I have to admit I have never got round to experimenting with. It’s on my ridiculously long todo list to try them out in some sort of scientific way and write an article on the results.

    Unless you want to do it of course . . . !

  2. Forgot to mention that there are a lot of articles and blog posts comparing execution speeds of language x compared to language y. Very often language y is C or C++, but I have never seen anybody mention compiler optimizations so any results are incomplete and don’t show the whole picture.

  3. I suppose the optimization options are on my to-do list as well. The problem I face is that I’ve seldom written anything large enough to warrant it. Or, if the need is truly pressing, I do inline assembly. But, seriously, the options for a modern compiler are quite vast.

Leave a Reply