{"id":1632,"date":"2024-02-23T00:00:00","date_gmt":"2024-02-23T05:00:00","guid":{"rendered":"https:\/\/molecularsciences.org\/content\/?p=1632"},"modified":"2024-02-02T14:31:08","modified_gmt":"2024-02-02T19:31:08","slug":"gcc-cheat-sheet","status":"publish","type":"post","link":"https:\/\/molecularsciences.org\/content\/gcc-cheat-sheet\/","title":{"rendered":"gcc Cheat Sheet"},"content":{"rendered":"\n<p>GCC (GNU Compiler Collection) is a powerful and widely-used compiler suite for various programming languages. This cheat sheet provides essential commands and options to compile and manage code efficiently using GCC.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>Introduction:<\/strong><\/h4>\n\n\n\n<p>GCC supports several programming languages, including C, C++, and Fortran. It is a crucial tool for developers, providing a range of options to control compilation, optimization, and output.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>Compiling C Programs:<\/strong><\/h4>\n\n\n\n<figure class=\"wp-block-table\"><table><thead><tr><th>Command<\/th><th>Description<\/th><\/tr><\/thead><tbody><tr><td><code>gcc [source_file.c] -o [output_executable]<\/code><\/td><td>Compile a C program and generate an executable.<\/td><\/tr><tr><td><code>gcc -c [source_file.c]<\/code><\/td><td>Generate object files without linking.<\/td><\/tr><tr><td><code>gcc -Wall [source_file.c] -o [output_executable]<\/code><\/td><td>Enable most warning messages during compilation.<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>Compiling C++ Programs:<\/strong><\/h4>\n\n\n\n<figure class=\"wp-block-table\"><table><thead><tr><th>Command<\/th><th>Description<\/th><\/tr><\/thead><tbody><tr><td><code>g++ [source_file.cpp] -o [output_executable]<\/code><\/td><td>Compile a C++ program and generate an executable.<\/td><\/tr><tr><td><code>g++ -c [source_file.cpp]<\/code><\/td><td>Generate object files without linking.<\/td><\/tr><tr><td><code>g++ -Wall [source_file.cpp] -o [output_executable]<\/code><\/td><td>Enable most warning messages during compilation.<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>Linking and Libraries:<\/strong><\/h4>\n\n\n\n<figure class=\"wp-block-table\"><table><thead><tr><th>Command<\/th><th>Description<\/th><\/tr><\/thead><tbody><tr><td><code>gcc [source_file.c] -o [output_executable] -l[library_name]<\/code><\/td><td>Link with a specific library during compilation.<\/td><\/tr><tr><td><code>gcc -L[path_to_library] -l[library_name]<\/code><\/td><td>Specify the library path during compilation.<\/td><\/tr><tr><td><code>ldd [output_executable]<\/code><\/td><td>Display shared library dependencies.<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>Optimization:<\/strong><\/h4>\n\n\n\n<figure class=\"wp-block-table\"><table><thead><tr><th>Command<\/th><th>Description<\/th><\/tr><\/thead><tbody><tr><td><code>gcc -O[level] [source_file.c] -o [output_executable]<\/code><\/td><td>Set the optimization level (0, 1, 2, 3).<\/td><\/tr><tr><td><code>gcc -march=native [source_file.c] -o [output_executable]<\/code><\/td><td>Optimize for the host machine&#8217;s architecture.<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>Debugging:<\/strong><\/h4>\n\n\n\n<figure class=\"wp-block-table\"><table><thead><tr><th>Command<\/th><th>Description<\/th><\/tr><\/thead><tbody><tr><td><code>gcc -g [source_file.c] -o [output_executable]<\/code><\/td><td>Generate debugging information.<\/td><\/tr><tr><td><code>gdb [output_executable]<\/code><\/td><td>Start the GNU Debugger for debugging.<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>Preprocessor Directives:<\/strong><\/h4>\n\n\n\n<figure class=\"wp-block-table\"><table><thead><tr><th>Command<\/th><th>Description<\/th><\/tr><\/thead><tbody><tr><td><code>gcc -E [source_file.c]<\/code><\/td><td>Run only the preprocessor and output the result.<\/td><\/tr><tr><td><code>gcc -D[macro_name] [source_file.c] -o [output_executable]<\/code><\/td><td>Define a macro during compilation.<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>Miscellaneous:<\/strong><\/h4>\n\n\n\n<figure class=\"wp-block-table\"><table><thead><tr><th>Command<\/th><th>Description<\/th><\/tr><\/thead><tbody><tr><td><code>gcc -v<\/code><\/td><td>Display the version and configuration of GCC.<\/td><\/tr><tr><td><code>gcc -print-search-dirs<\/code><\/td><td>Print the default directories used by GCC.<\/td><\/tr><tr><td><code>gcc -H [source_file.c]<\/code><\/td><td>Display include file hierarchy during compilation.<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\">Conclusion<\/h3>\n\n\n\n<p>This GCC compiler cheat sheet provides a handy reference for developers working with C, C++, and Fortran code. Whether you are compiling, linking, optimizing, or debugging, GCC offers a multitude of options to suit your needs. Use this cheat sheet to streamline your compilation process and enhance your understanding of GCC&#8217;s capabilities.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>GCC (GNU Compiler Collection) is a powerful and widely-used compiler suite for various programming languages. This cheat sheet provides essential commands and options to compile and manage code efficiently using GCC. Introduction: GCC supports several programming languages, including C, C++, and Fortran. It is a crucial tool for developers, providing a range of options to [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[34,265],"tags":[35],"class_list":["post-1632","post","type-post","status-publish","format-standard","hentry","category-linux","category-reference","tag-linux"],"_links":{"self":[{"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/posts\/1632","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/comments?post=1632"}],"version-history":[{"count":1,"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/posts\/1632\/revisions"}],"predecessor-version":[{"id":1634,"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/posts\/1632\/revisions\/1634"}],"wp:attachment":[{"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/media?parent=1632"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/categories?post=1632"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/tags?post=1632"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}