{"id":396005,"date":"2025-12-07T15:18:01","date_gmt":"2025-12-07T14:18:01","guid":{"rendered":"https:\/\/dutable.com\/?p=396005"},"modified":"2025-12-07T15:39:36","modified_gmt":"2025-12-07T14:39:36","slug":"gcc-compiler-options-flags-guide","status":"publish","type":"post","link":"https:\/\/dutable.com\/gcc-compiler-options-flags-guide\/","title":{"rendered":"Understanding the GCC Compiler: Options, Flags, and Practical Usage"},"content":{"rendered":"<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_85 counter-hierarchy ez-toc-counter ez-toc-light-blue ez-toc-container-direction\">\n<div class=\"ez-toc-title-container\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Table of Contents<\/p>\n<span class=\"ez-toc-title-toggle\"><a href=\"#\" class=\"ez-toc-pull-right ez-toc-btn ez-toc-btn-xs ez-toc-btn-default ez-toc-toggle\" aria-label=\"Toggle Table of Content\"><span class=\"ez-toc-js-icon-con\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #999;color:#999\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #999;color:#999\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/span><\/a><\/span><\/div>\n<nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/dutable.com\/gcc-compiler-options-flags-guide\/#Table_of_Contents\" >Table of Contents<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/dutable.com\/gcc-compiler-options-flags-guide\/#What_Is_GCC_and_How_It_Works\" >What Is GCC and How It Works<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/dutable.com\/gcc-compiler-options-flags-guide\/#Installation_of_GCC_in_Unix_Environments\" >Installation of GCC in Unix Environments<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/dutable.com\/gcc-compiler-options-flags-guide\/#Basic_Compilation_Workflow\" >Basic Compilation Workflow<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/dutable.com\/gcc-compiler-options-flags-guide\/#The_Stages_of_GCC_Compilation\" >The Stages of GCC Compilation<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/dutable.com\/gcc-compiler-options-flags-guide\/#Essential_GCC_Options\" >Essential GCC Options<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-7\" href=\"https:\/\/dutable.com\/gcc-compiler-options-flags-guide\/#Debugging_Flags\" >Debugging Flags<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-8\" href=\"https:\/\/dutable.com\/gcc-compiler-options-flags-guide\/#Warning_Flags\" >Warning Flags<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-9\" href=\"https:\/\/dutable.com\/gcc-compiler-options-flags-guide\/#Optimization_Flags\" >Optimization Flags<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-10\" href=\"https:\/\/dutable.com\/gcc-compiler-options-flags-guide\/#Both_the_linking_process_and_Library_Management\" >Both the linking process and Library Management<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-11\" href=\"https:\/\/dutable.com\/gcc-compiler-options-flags-guide\/#Analysis_of_Performance_and_Profiling\" >Analysis of Performance and Profiling<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-12\" href=\"https:\/\/dutable.com\/gcc-compiler-options-flags-guide\/#Application_of_GCC_in_Unix_Real-World_Environments\" >Application of GCC in Unix Real-World Environments<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-13\" href=\"https:\/\/dutable.com\/gcc-compiler-options-flags-guide\/#Creating_Simple_Makefiles\" >Creating Simple Makefiles<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-14\" href=\"https:\/\/dutable.com\/gcc-compiler-options-flags-guide\/#Cases_and_Processes\" >Cases and Processes.<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-15\" href=\"https:\/\/dutable.com\/gcc-compiler-options-flags-guide\/#Best_Practices_in_Compiling_under_GCC\" >Best Practices in Compiling under GCC<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-16\" href=\"https:\/\/dutable.com\/gcc-compiler-options-flags-guide\/#Conclusion\" >Conclusion<\/a><\/li><\/ul><\/nav><\/div>\n\n<p class=\"wp-block-paragraph\">GNU Compiler Collection (GCC) is a compiler system that is considered one of the most popular in the world particularly in Unix-like platforms. From academic environments to mainstream software engineering, GCC has continued to be a key to the creation, optimization, debugging and maintenance of C programs. GCC is a multilingual suite that can interpret a variety of programming languages, such as C, C++, Fortran, Go, and Objective-C; as such, allowing programmers to have a thorough control over the way the source code is converted into machine-executable code.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">This is not enough in the contemporary world of software development where one learns how to compile a file with gcc file.c. In real-life projects, it is necessary to optimize, debug, test long warnings and build finely. The knowledge of GCC options and flags allows developers and learners to create efficient programs, detect possible errors and optimize the performance of the code.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">This article is a very extensive source of guidance to GCC- fundamental commands, compiler phases, commonly used flags, debugging, optimization, warning systems, linking, profiling, and workflow examples of Unix systems.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Table_of_Contents\"><\/span><strong>Table of Contents<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Introduction<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>What Is GCC and How It Works<\/li>\n\n\n\n<li>Installation of GCC in Unix Environment.<\/li>\n\n\n\n<li>Basic Compilation Workflow<\/li>\n\n\n\n<li>Knowledge about GCC Compilation Stages.<\/li>\n\n\n\n<li>Essential GCC Options<\/li>\n\n\n\n<li>Input and Output Options<\/li>\n\n\n\n<li>Preprocessing Options<\/li>\n\n\n\n<li>Compilation Options<\/li>\n\n\n\n<li>Assembly Options<\/li>\n\n\n\n<li>Linking Options<\/li>\n\n\n\n<li>Debugging Flags<\/li>\n\n\n\n<li>Warning Flags<\/li>\n\n\n\n<li>Optimization Flags<\/li>\n\n\n\n<li>Connection and Library Management.<\/li>\n\n\n\n<li>Performance Analysis and Profiling.<\/li>\n\n\n\n<li>Applications of GCC in Unix Real World.<\/li>\n\n\n\n<li>Real-life Applications and Processes.<\/li>\n\n\n\n<li>Best Practices in Compiling with GCC.<\/li>\n\n\n\n<li>Conclusion<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"What_Is_GCC_and_How_It_Works\"><\/span><strong>What Is GCC and How It Works<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">GNU Compiler Collection is a set of compilers and developer tools that are administered by the Free Software Foundation (FSF). GCC started in the 1980s as the compiler of the GNU operating system and has expanded to become a cross-platform and highly programmable compiler that can code to a wide variety of CPU architectures.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">GCC consists of a number of elements:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Preprocessor (cpp)<\/li>\n\n\n\n<li>Compiler (cc1, cc1plus, etc.)<\/li>\n\n\n\n<li>Assembler (as)<\/li>\n\n\n\n<li>Linker (ld)<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">The source code is converted into a lower level representation by each stage until an executable binary is generated.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>GCC is often used in:<\/li>\n\n\n\n<li>Linux and Unix systems<\/li>\n\n\n\n<li>Embedded systems<\/li>\n\n\n\n<li>High-performance computing<\/li>\n\n\n\n<li>Development of the operating system.<\/li>\n\n\n\n<li>Education and research<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">Modular design enables it to be flexible to almost any development environment.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Installation_of_GCC_in_Unix_Environments\"><\/span><strong>Installation of GCC in Unix Environments<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">GCC is provided with most Unix systems (Linux, BSD, and via Homebrew on macOS) or is easy to install.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Debian\/Ubuntu<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">sudo apt update<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">sudo apt-get install build-essential<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Fedora\/CentOS\/RHEL<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">sudo dnf install gcc gcc-c++<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>macOS (Homebrew)<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">brew install gcc<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">After installation one can verify GCC by:<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">gcc &#8211;version<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Basic_Compilation_Workflow\"><\/span><strong>Basic Compilation Workflow<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">The simplest GCC command is one that assembles a C source code into an executable:<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">gcc program.c -o program<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">program.c &#8211; source file<br>-o program executable name.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In the case of no -o option, the default executable name in GCC is <strong>a.out<\/strong>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Running the program:<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">.\/program<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"The_Stages_of_GCC_Compilation\"><\/span><strong>The Stages of GCC Compilation<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">GCC in the compilation of a C program has four dominant steps:<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Preprocessing<\/strong><strong><br><\/strong> Expands macros, adds header files, and deletes comments.<br>gcc -E file.c -o file.i<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Compilation<\/strong><strong><br><\/strong> Converts preprocessed code into assembly.<br>gcc -S file.i -o file.s<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Assembly<\/strong><strong><br><\/strong> Converts assembly into machine code (object file).<br>gcc -c file.s -o file.o<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Linking<\/strong><strong><br><\/strong> Combines object files and libraries into an executable.<br>gcc file.o -o program<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The examination of these stages assists developers to have a better understanding of errors and identify problems.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Essential_GCC_Options\"><\/span><strong>Essential GCC Options<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>A. Input and Output Options<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Specify the Output File<\/strong><strong><br><\/strong> -o output_name<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Compile Without Linking<\/strong><strong><br><\/strong> -c file.c<br>Useful when handling multiple source files.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>B. Preprocessing Options<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>View Preprocessed Output<\/strong><strong><br><\/strong> -E file.c<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Define a Macro<\/strong><strong><br><\/strong> -DDEBUG<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Additional Header Directories<\/strong><strong><br><\/strong> -I\/path\/to\/include<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>C. Compilation Options<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Generate Assembly<\/strong><strong><br><\/strong> -S file.c<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Include Debug Symbols<\/strong><strong><br><\/strong> -g<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>D. Assembly Options<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">-Wa,&lt;options&gt;<br>Example:<br>-Wa,-adhln \u2014 generates annotated assembly output<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>E. Linking Options<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Link with a Library<\/strong><strong><br><\/strong> -lm<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Add Library Search Directory<\/strong><strong><br><\/strong> -L\/path\/to\/lib<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Static Linking<\/strong><strong><br><\/strong> -static<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Link multiple object files<\/strong><strong><br><\/strong> gcc main.o utils.o math.o -o program<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Debugging_Flags\"><\/span><strong>Debugging Flags<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>-g \u2014 Include Debugging Symbols<\/strong><strong><br><\/strong> gcc -g file.c -o program<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>-ggdb \u2014 gdb-optimized debug data<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Address Sanitizer<\/strong><strong><br><\/strong> -fsanitize=address<br>Detects memory leaks, buffer overflows, use-after-free.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Undefined Behavior Sanitizer<\/strong><strong><br><\/strong> -fsanitize=undefined<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>-fno-omit-frame-pointer<\/strong><strong><br><\/strong> Improves stack traces.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Warning_Flags\"><\/span><strong>Warning Flags<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>-Wall<\/strong> \u2013 Enables common warnings<br><strong>-Wextra<\/strong> \u2013 Adds additional warnings<br><strong>-Werror<\/strong> \u2013 Treats warnings as errors<br><strong>-Wpedantic<\/strong> \u2013 Strict ISO C compliance<br><strong>-Wshadow<\/strong> \u2013 Detects variable shadowing<br><strong>-Wformat<\/strong> \u2013 Checks printf\/scanf formats<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Recommended bundle:<\/strong><strong><br><\/strong> gcc -Wall -Wextra -Werror file.c -o program<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Optimization_Flags\"><\/span><strong>Optimization Flags<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>-O0<\/strong> \u2014 No optimization<br><strong>-O1<\/strong> \u2014 Light optimization<br><strong>-O2<\/strong> \u2014 Recommended general optimization<br><strong>-O3<\/strong> \u2014 Strong optimization<br><strong>-Ofast<\/strong> \u2014 Unsafe but fast<br><strong>-Os<\/strong> \u2014 Optimize for size<br><strong>-march=native<\/strong> \u2014 Optimize for local CPU<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Example:<br>gcc -march=native -O2 file.c -o program<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Both_the_linking_process_and_Library_Management\"><\/span><strong>Both the linking process and Library Management<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Static vs Dynamic Linking<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Static<\/strong><strong><br><\/strong> gcc main.c -static -o program<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Dynamic (default)<\/strong><strong><br><\/strong> Smaller binaries require libraries at runtime.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Linking Shared Libraries<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Add path:<br>-L\/usr\/local\/lib<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Link library:<br>-lmylib<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Example:<br>gcc main.c -L\/usr\/local\/lib -lmylib -o program<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>pkg-config example<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">gcc main.c $(pkg-config &#8211;cflags &#8211;libs gtk+-3.0) -o app<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Analysis_of_Performance_and_Profiling\"><\/span><strong>Analysis of Performance and Profiling<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>gprof<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">gcc -pg program.c -o program<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">gprof program gmon.out &gt; analysis.txt<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Valgrind<\/strong><strong><br><\/strong> valgrind .\/program<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>perf (Linux)<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">perf record .\/program<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">perf report<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Application_of_GCC_in_Unix_Real-World_Environments\"><\/span><strong>Application of GCC in Unix Real-World Environments<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Used with:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Makefiles<\/li>\n\n\n\n<li>Shell scripts<\/li>\n\n\n\n<li>Version control<\/li>\n\n\n\n<li>CI\/CD<\/li>\n\n\n\n<li>Debuggers<\/li>\n\n\n\n<li>Profilers<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Compiling Multiple Files<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">gcc -c main.c<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">gcc -c utils.c<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">gcc -c math.c<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">gcc main.o utils.o math.o -o program<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Creating_Simple_Makefiles\"><\/span><strong>Creating Simple Makefiles<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">CC = gcc<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">CFLAGS = -Wall -Wextra -O2<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">all: program<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">program: main.o utils.o<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">$(CC) $(CFLAGS) main.o utils.o -o program<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">clean:<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">rm -f *.o program<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Run: make<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Cases_and_Processes\"><\/span><strong>Cases and Processes.<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Example 1 \u2013 Debug<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">gcc -g -Wextra -Wall program.c -o program<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">gdb .\/program<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Example 2 \u2013 Optimize for speed<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">gcc -O3 -march=native fastcode.c -o fastcode<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Example 3 \u2013 Sanitizers<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">gcc -fsanitize=address -fsanitize=undefined -g main.c -o main<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Example 4 \u2013 Create Shared Library<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">gcc -fPIC -c libmath.c<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">gcc -shared -o libmath.so libmath.o<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">gcc main.c -L. -lmath -o program<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Best_Practices_in_Compiling_under_GCC\"><\/span><strong>Best Practices in Compiling under GCC<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Always enable warnings<\/li>\n\n\n\n<li>Use debugging symbols during development<\/li>\n\n\n\n<li>Use sanitizers early<\/li>\n\n\n\n<li>Optimize only release builds<\/li>\n\n\n\n<li>Use Makefiles<\/li>\n\n\n\n<li>Profile before optimizing<\/li>\n\n\n\n<li>Keep GCC updated<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Conclusion\"><\/span><strong>Conclusion<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">GCC has been one of the surest and strongest compilers in Unix-like systems. Its wide range of choices and options gives developers a very fine-grained ability to control how code is processed, such as preprocessing, compilation, linking and optimizing. Not only GCC can be used to make binaries more efficient and optimized, but it can also improve debugging, maintenance, and the overall quality of software.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Knowing how to enable debugging flags, warning systems, level of optimization, linking libraries, and how to use profiling tools, students are able to radically transform their programming process. No matter who you are, a student or a novice developer, mastering the art of using GCC will help you throughout the innumerable projects that you will undertake.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">When you use the techniques and commands mentioned in this article, you will be in a better position to write high-quality C programs and operate with confidence in Unix environments.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n","protected":false},"excerpt":{"rendered":"<p>GNU Compiler Collection (GCC) is a compiler system that is considered one of the most popular in the world particularly in Unix-like platforms. From academic environments to mainstream software engineering, GCC has continued to be a key to the creation, optimization, debugging and maintenance of C programs. GCC is a multilingual suite that can interpret&#8230;<\/p>\n","protected":false},"author":1905,"featured_media":396021,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4580,4577,12164],"tags":[29087,30609,30610],"class_list":["post-396005","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-education","category-science-technology","category-technology","tag-c-programming","tag-gcc-compiler","tag-unix-development"],"_links":{"self":[{"href":"https:\/\/dutable.com\/wp-json\/wp\/v2\/posts\/396005","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/dutable.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/dutable.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/dutable.com\/wp-json\/wp\/v2\/users\/1905"}],"replies":[{"embeddable":true,"href":"https:\/\/dutable.com\/wp-json\/wp\/v2\/comments?post=396005"}],"version-history":[{"count":2,"href":"https:\/\/dutable.com\/wp-json\/wp\/v2\/posts\/396005\/revisions"}],"predecessor-version":[{"id":396029,"href":"https:\/\/dutable.com\/wp-json\/wp\/v2\/posts\/396005\/revisions\/396029"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/dutable.com\/wp-json\/wp\/v2\/media\/396021"}],"wp:attachment":[{"href":"https:\/\/dutable.com\/wp-json\/wp\/v2\/media?parent=396005"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/dutable.com\/wp-json\/wp\/v2\/categories?post=396005"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/dutable.com\/wp-json\/wp\/v2\/tags?post=396005"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}