{"id":1243,"date":"2009-10-07T17:42:01","date_gmt":"2009-10-07T22:42:01","guid":{"rendered":"http:\/\/jianmingli.com\/wp\/?p=1243"},"modified":"2009-10-09T15:53:13","modified_gmt":"2009-10-09T20:53:13","slug":"gcc","status":"publish","type":"post","link":"https:\/\/jianmingli.com\/wp\/?p=1243","title":{"rendered":"GCC"},"content":{"rendered":"<span id=\"Environment_Variables\"><h3>Environment Variables<\/h3><\/span>\n<pre lang=\"bash\">\r\nexport C_INCLUDE_PATH=\/opt\/gdbm-1.8.3\/include\r\nexport CPLUS_INCLUDE_PATH=\/opt\/gdbm-1.8.3\/include \r\nexport LIBRARY_PATH=\/opt\/gdbm-1.8.3\/lib\r\n<\/pre>\n<span id=\"Include_Path_Search_order\"><h3>Include Path Search order<\/h3><\/span>\n<p>* Command line options (-I) from left to right<br \/>\n* Env variables (C_INCLUDE_PATH, CPLUS_INCLUDE_PATH)<br \/>\n* Standard default directories<br \/>\n&#8211; \/usr\/local\/include<br \/>\n&#8211; \/usr\/include<\/p>\n<span id=\"Library_Path_Search_order\"><h3>Library Path Search order<\/h3><\/span>\n<p>* Command line options (-L) from left to right<br \/>\n* Env variables (LIBRARY_PATH)<br \/>\n* Standard default directories<br \/>\n&#8211; \/usr\/local\/lib<br \/>\n&#8211; \/usr\/lib<\/p>\n<span id=\"Extended_Search_Path\"><h3>Extended Search Path<\/h3><\/span>\n<p>* Multiple -I or -L entries. For example, -I. -I\/opt\/gdbm-1.8.3\/include -I\/net\/include -L. -L\/opt\/gdbm-1.8.3\/lib -L\/net\/lib &#8230;..<br \/>\n* Use colon as separator in env variables. For example,<br \/>\nC_INCLUDE_PATH=.:\/opt\/gdbm-1.8.3\/include:\/net\/include<br \/>\nLIBRARY_PATH=.:\/opt\/gdbm-1.8.3\/lib:\/net\/lib<\/p>\n<span id=\"Shared_Library_Path\"><h3>Shared Library Path<\/h3><\/span>\n<p>LD_LIBRARY_PATH=\/opt\/gdbm-1.8.3\/lib:\/opt\/gtk-1.4\/lib<br \/>\nexport LD_LIBRARY_PATH<\/p>\n<span id=\"ANSIISO_Compliance\"><h3>ANSI\/ISO Compliance<\/h3><\/span>\n<pre>\r\n# Use -ansi\r\ngcc -Wall -ansi pi.c\r\n\r\n# Use -pedantic AND -ansi for strict ANSI\/ISO compliance\r\ngcc -Wall -ansi -pedantic gnuarray.c\r\n<\/pre>\n<span id=\"Other_Standards\"><h3>Other Standards<\/h3><\/span>\n<p>-std=c89 or -std=iso9899:1990<br \/>\n-std=iso9899:199409<br \/>\n-std=c99 or -std=iso9899:1999 <\/p>\n<span id=\"Recommended_warning_options\"><h3>Recommended warning options<\/h3><\/span>\n<pre>\r\ngcc -ansi -pedantic -Wall -W -Wconversion -Wshadow -Wcast-qual -Wwrite-strings\r\n<\/pre>\n<span id=\"C_Preprocessor_CPP\"><h2>C Preprocessor (CPP)<\/h2><\/span>\n<p>* Expands macros before source files are compiled.<br \/>\n* Preprocess without compiling: -save-temps<\/p>\n<pre lang=\"c\">\r\n# Use -E options\r\ngcc -E hello.c\r\n\r\n# Use -save-temps options\r\ngcc -c -save-temps hello.c\r\n<\/pre>\n<span id=\"Define_macros\"><h3>Define macros<\/h3><\/span>\n<pre lang=\"c\">\r\n#ifdef TEST\r\n  printf (\"Test mode\\n\");\r\n#endif\r\n<\/pre>\n<p>* Compile with gcc -Wall -DTEST dtest.c -o dtest will print &#8220;Test mode&#8221;.<br \/>\n* Define in source files or header files<\/p>\n<pre lang=\"c\">#define TEST \"Hello, World!\"<\/pre>\n<p>* Print predefined macros:<\/p>\n<pre>cpp -dM \/dev\/null<\/pre>\n<p>* Macro with values<\/p>\n<pre lang=\"c\">\r\nprintf (\"Value of NUM is %d\\n\", NUM);\r\ngcc -Wall -DNUM=100 dtestval.c\r\n<\/pre>\n<span id=\"Debugging_Core_Dump\"><h3>Debugging Core Dump<\/h3><\/span>\n<pre lang=\"c\">\r\n# Compile with -g option\r\ngcc -Wall -g null.c -o null\r\n\r\n# Set core dump file limit\r\nulimit -c unlimited\r\n\r\n# Use gdb\r\ngdb null core\r\n(gdb) backtrace\r\n(gdb) break main\r\n(gdb) run\r\n(gdb) step\r\n(gdb) set variable p = malloc(sizeof(int))\r\n(gdb) set variable *p = 255\r\n(gdb) step\r\n(gdb) step\r\n(gdb) finish\r\n(gdb) continue\r\n<\/pre>\n<span id=\"Profiling\"><h3>Profiling<\/h3><\/span>\n<p>* Compile and link with -pg option<\/p>\n<pre lang=\"bash\">\r\ngcc -Wall -c -pg collatz.c\r\ngcc -Wall -pg collatz.o\r\n<\/pre>\n<p>* Execute<\/p>\n<pre lang=\"bash\">\r\n.\/a.out\r\n<\/pre>\n<p>* Profile<\/p>\n<pre lang=\"bash\">\r\ngprof a.out\r\n<\/pre>\n<span id=\"Compile_C\"><h2>Compile C++<\/h2><\/span>\n<pre lang=\"c++\">\r\ng++ -Wall hello.cc -o hello\r\n\r\n# Compile for a specific CPU. Not portable!\r\ng++ -Wall -march=pentium4 hello.cc -o hello\r\n<\/pre>\n<span id=\"DebugKill_a_Hanged_Program\"><h3>Debug\/Kill a Hanged Program<\/h3><\/span>\n<pre lang=\"c++\">\r\n# Compile with -g option\r\ngcc -Wall -g loop.c\r\n\r\n# Run it\r\n.\/a.out\r\n\r\n# Find pid\r\nps -x\r\n\r\n# Attach gdb\r\ngdb a.out\r\n(gdb) attach 27189\r\n(gdb) kill\r\nKill the program being debugged? (y or n) y\r\n(gdb) \r\n<\/pre>\n<span id=\"Create_a_Library_with_GNU_Archiver\"><h3>Create a Library with GNU Archiver<\/h3><\/span>\n<pre lang=\"c++\">\r\n# Compile. No link (-c)\r\ngcc -Wall -c hello_fn.c\r\ngcc -Wall -c hello_fn.c\r\n\r\n# Archive. Create and replace (cr)\r\nar cr libhello.a hello_fn.o bye_fn.o\r\n\r\n# List content\r\nar t libhello.a\r\n\r\n# Use lib\r\ngcc -Wall hellolib.c libhello.a -o hellolib\r\n# or -L. (include current dir) -lhello (shortcut library link option)\r\ngcc -L. hellolib.c -lhello -o hellolib\r\n<\/pre>\n<span id=\"C_Standard_Library_Template\"><h3>C++ Standard Library Template<\/h3><\/span>\n<p>* libstdc++<\/p>\n<span id=\"Providing_Own_Template\"><h3>Providing Own Template<\/h3><\/span>\n<p>* Follow &#8220;Inclusion Compilation Model&#8221;: place template definition in header files.<\/p>\n<span id=\"How_Compiler_Works\"><h2>How Compiler Works<\/h2><\/span>\n<span id=\"Overview\"><h3>Overview<\/h3><\/span>\n<p>* Preprocessing: expands macros<\/p>\n<pre lang=\"c\">\r\ncpp hello.c > hello.i\r\n<\/pre>\n<p>* Compilation: source code to assembly code<\/p>\n<pre lang=\"c\">\r\ngcc -Wall -S hello.i\r\n<\/pre>\n<p>* Assembly: assembly code to machine code<\/p>\n<pre lang=\"c\">\r\nas hello.s -o hello.o\r\n<\/pre>\n<p>* Linking: create final executable<\/p>\n<pre lang=\"c\">\r\nld \r\n  -dynamic-linker \\\r\n  \/lib\/ld-linux.so.2 \\\r\n  \/usr\/lib\/crt1.o \\\r\n  \/usr\/lib\/crti.o \\\r\n  \/usr\/lib\/gcc-lib\/i686\/3.3.1\/crtbegin.o \\\r\n  -L\/usr\/lib\/gcc-lib\/i686\/3.3.1 \\\r\n  hello.o \\\r\n  -lgcc \\\r\n  -lgcc_eh \\\r\n  -lc \\\r\n  -lgcc \\\r\n  -lgcc_eh \\\r\n  \/usr\/lib\/gcc-lib\/i686\/3.3.1\/crtend.o \\\r\n  \/usr\/lib\/crtn.o\r\n<\/pre>\n<span id=\"Examine_Compiled_Files\"><h3>Examine Compiled Files<\/h3><\/span>\n<p>* file<\/p>\n<pre>\r\n$ file a.out\r\na.out: ELF 32-bit LSB executable, Intel 80386, version 1 (SYSV), for GNU\/Linux 2.6.9, dynamically linked (uses shared libs), for GNU\/Linux 2.6.9, not stripped\r\n<\/pre>\n<span id=\"Examine_Symbol_Table\"><h3>Examine Symbol Table<\/h3><\/span>\n<p>* nm<br \/>\n&#8211; T: defined<br \/>\n&#8211; U: undefined<\/p>\n<span id=\"Finding_Dynamically_Linked_Libraries\"><h3>Finding Dynamically Linked Libraries<\/h3><\/span>\n<p>* ldd<\/p>\n<pre>\r\n ldd hello\r\n        linux-gate.so.1 =>  (0x00362000)\r\n        libstdc++.so.6 => \/usr\/lib\/libstdc++.so.6 (0x00169000)\r\n        libm.so.6 => \/lib\/libm.so.6 (0x0077c000)\r\n        libgcc_s.so.1 => \/lib\/libgcc_s.so.1 (0x00101000)\r\n        libc.so.6 => \/lib\/libc.so.6 (0x0063a000)\r\n        \/lib\/ld-linux.so.2 (0x0061d000)<\/pre>\n<span id=\"References\"><h1>References<\/h1><\/span>\n<p>* <a href=\"http:\/\/www.network-theory.co.uk\/gcc\/intro\/\">http:\/\/www.network-theory.co.uk\/gcc\/intro\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Environment Variables export C_INCLUDE_PATH=\/opt\/gdbm-1.8.3\/include export CPLUS_INCLUDE_PATH=\/opt\/gdbm-1.8.3\/include export LIBRARY_PATH=\/opt\/gdbm-1.8.3\/lib Include Path Search order * Command line options (-I) from left to right * Env variables (C_INCLUDE_PATH, CPLUS_INCLUDE_PATH) * Standard default directories &#8211; \/usr\/local\/include &#8211; \/usr\/include Library Path Search order * Command &hellip; <a href=\"https:\/\/jianmingli.com\/wp\/?p=1243\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_exactmetrics_skip_tracking":false,"_exactmetrics_sitenote_active":false,"_exactmetrics_sitenote_note":"","_exactmetrics_sitenote_category":0,"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[11,33],"tags":[],"class_list":["post-1243","post","type-post","status-publish","format-standard","hentry","category-cpp","category-unix"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/s8cRUO-gcc","_links":{"self":[{"href":"https:\/\/jianmingli.com\/wp\/index.php?rest_route=\/wp\/v2\/posts\/1243","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/jianmingli.com\/wp\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/jianmingli.com\/wp\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/jianmingli.com\/wp\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/jianmingli.com\/wp\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1243"}],"version-history":[{"count":6,"href":"https:\/\/jianmingli.com\/wp\/index.php?rest_route=\/wp\/v2\/posts\/1243\/revisions"}],"predecessor-version":[{"id":1250,"href":"https:\/\/jianmingli.com\/wp\/index.php?rest_route=\/wp\/v2\/posts\/1243\/revisions\/1250"}],"wp:attachment":[{"href":"https:\/\/jianmingli.com\/wp\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1243"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/jianmingli.com\/wp\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1243"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/jianmingli.com\/wp\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1243"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}