{"id":4551,"date":"2021-01-09T00:01:15","date_gmt":"2021-01-09T08:01:15","guid":{"rendered":"https:\/\/c-for-dummies.com\/blog\/?p=4551"},"modified":"2021-01-16T09:46:20","modified_gmt":"2021-01-16T17:46:20","slug":"compiler-options-at-the-command-prompt","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=4551","title":{"rendered":"Compiler Options at the Command Prompt"},"content":{"rendered":"<p>The traditional C compiler is <em>cc<\/em>. Its GNU replacement in the Linux world is <em>gcc<\/em>. A better option is the LLVM <em>clang<\/em> compiler. As commands typed at a prompt, you control these compilers by setting command line options or switches.<br \/>\n<!--more--><br \/>\nThe most common command line option is a source code filename. I&#8217;ll use <em>clang<\/em> because it&#8217;s my favorite:<\/p>\n<p><code>clang sc.c<\/code><\/p>\n<p>The above command compiles and links the source code file <code>sc.c<\/code>. Both compiling and linking halt upon an error. Otherwise, the default output file is generated, <code>a.out<\/code> or <code>a.exe<\/code> (Windows).<\/p>\n<p>In ancient times, the <em>cc<\/em> compiler only compiled. Linking was accomplished by using the <em>ld<\/em> (linker) program in Unix. The MS-DOS\/Windows equivalent was called <em>link<\/em>. 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.<\/p>\n<p>The option I specify frequently when compiling is the <code>-Wall<\/code> switch. This switch activates all warning messages for the compiler. For <em>clang<\/em>, the warnings are specific and detailed, which aids in debugging. Another good option is <code>-Weverything<\/code>, which activates all diagnostics.<\/p>\n<p>Here are other common command line switches:<\/p>\n<p><code>-o<\/code> This option directs the compiler to set a specific output filename, something other than the default <code>a.out<\/code> or <code>a.exe<\/code>. For example:<\/p>\n<p><code>clang sc.c -o program<\/code><\/p>\n<p>Above, source code file <code>sc.c<\/code> is compiled and linked. The program created is named <code>program<\/code>.<\/p>\n<p><code>-l<\/code> Use the little L to direct a specific library file to be linked it. The library file is named immediately after the <code>-l<\/code>, 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 <code>\/usr\/lib\/<\/code> and <code>\/usr\/local\/lib<\/code><\/p>\n<p><code>-c<\/code> The little C option directs the compiler to compile only; the object code file isn&#8217;t linked to form a program. Use this option when compiling multiple modules to be linked later to form a single program.<\/p>\n<p><code>-g<\/code> Little G is the debug switch, which directs the compiler and linker to include debugging information and symbols in the final program.<\/p>\n<p><code>-iquote<\/code> This option sets a location for the compiler to locate header files. It&#8217;s immediately followed by a pathname. This option replaces <code>-I<\/code>, which has been deprecated (but still works).<\/p>\n<p><code>-L<\/code> This option directs the compiler to look in the specified directory (folder) for library files.<\/p>\n<p><code>--version<\/code> 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.<\/p>\n<p>I recognize that most programmers don&#8217;t use the command prompt, definitely not as much as I do. I use it because it&#8217;s quick and fun, reminding me of earlier times. Yet, even with an IDE, you&#8217;re still invoking a command line version of the compiler, which I&#8217;ll show in <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=4553\">next week&#8217;s Lesson<\/a> when I cover options in the Code::Blocks IDE.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The C compiler has dozens if not hundreds of switches and options. Here are a few worth knowing. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=4551\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[],"class_list":["post-4551","post","type-post","status-publish","format-standard","hentry","category-main"],"_links":{"self":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/4551","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=4551"}],"version-history":[{"count":5,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/4551\/revisions"}],"predecessor-version":[{"id":4581,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/4551\/revisions\/4581"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=4551"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=4551"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=4551"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}