{"id":3949,"date":"2021-05-07T03:38:05","date_gmt":"2021-05-07T03:38:05","guid":{"rendered":"https:\/\/www.gubatron.com\/blog\/?p=3949"},"modified":"2021-05-07T03:38:05","modified_gmt":"2021-05-07T03:38:05","slug":"things-to-remember-when-compiling-linking-c-c-software","status":"publish","type":"post","link":"https:\/\/www.gubatron.com\/blog\/things-to-remember-when-compiling-linking-c-c-software\/","title":{"rendered":"Things to remember when compiling\/linking C\/C++ software"},"content":{"rendered":"<style>.gist table { margin-bottom: 0; }<\/style>\n<div style=\"tab-size: 8\" id=\"gist20448420\" class=\"gist\">\n<div class=\"gist-file\" translate=\"no\" data-color-mode=\"light\" data-light-theme=\"light\">\n<div class=\"gist-data\">\n<div class=\"js-gist-file-update-container js-task-list-container\">\n<div id=\"file-compiling_building_c_cpp_notes-md\" class=\"file my-2\">\n<div id=\"file-compiling_building_c_cpp_notes-md-readme\" class=\"Box-body readme blob tmp-p-5 tmp-p-xl-6 \"\n    style=\"overflow: auto\" tabindex=\"0\" role=\"region\"\n    aria-label=\"compiling_building_c_cpp_notes.md content, created by gubatron on 08:55AM on March 17, 2015.\"\n  ><\/p>\n<article class=\"markdown-body entry-content container-lg\" itemprop=\"text\">\n<div class=\"markdown-heading\" dir=\"auto\">\n<h1 class=\"heading-element\" dir=\"auto\">Things to remember when compiling\/linking C\/C++ software<\/h1>\n<p><a id=\"user-content-things-to-remember-when-compilinglinking-cc-software\" class=\"anchor\" aria-label=\"Permalink: Things to remember when compiling\/linking C\/C++ software\" href=\"#things-to-remember-when-compilinglinking-cc-software\"><svg class=\"octicon octicon-link\" viewBox=\"0 0 16 16\" version=\"1.1\" width=\"16\" height=\"16\" aria-hidden=\"true\"><path d=\"m7.775 3.275 1.25-1.25a3.5 3.5 0 1 1 4.95 4.95l-2.5 2.5a3.5 3.5 0 0 1-4.95 0 .751.751 0 0 1 .018-1.042.751.751 0 0 1 1.042-.018 1.998 1.998 0 0 0 2.83 0l2.5-2.5a2.002 2.002 0 0 0-2.83-2.83l-1.25 1.25a.751.751 0 0 1-1.042-.018.751.751 0 0 1-.018-1.042Zm-4.69 9.64a1.998 1.998 0 0 0 2.83 0l1.25-1.25a.751.751 0 0 1 1.042.018.751.751 0 0 1 .018 1.042l-1.25 1.25a3.5 3.5 0 1 1-4.95-4.95l2.5-2.5a3.5 3.5 0 0 1 4.95 0 .751.751 0 0 1-.018 1.042.751.751 0 0 1-1.042.018 1.998 1.998 0 0 0-2.83 0l-2.5 2.5a1.998 1.998 0 0 0 0 2.83Z\"><\/path><\/svg><\/a><\/div>\n<p dir=\"auto\">by Angel Leon. March 17, 2015;<\/p>\n<p dir=\"auto\"><em>Last update on December 14, 2023<\/em><\/p>\n<p dir=\"auto\"><em>Updated on February 27, 2023<\/em><\/p>\n<p dir=\"auto\"><em>Updated August 29, 2019.<\/em><\/p>\n<div class=\"markdown-heading\" dir=\"auto\">\n<h2 class=\"heading-element\" dir=\"auto\">Include Paths<\/h2>\n<p><a id=\"user-content-include-paths\" class=\"anchor\" aria-label=\"Permalink: Include Paths\" href=\"#include-paths\"><svg class=\"octicon octicon-link\" viewBox=\"0 0 16 16\" version=\"1.1\" width=\"16\" height=\"16\" aria-hidden=\"true\"><path d=\"m7.775 3.275 1.25-1.25a3.5 3.5 0 1 1 4.95 4.95l-2.5 2.5a3.5 3.5 0 0 1-4.95 0 .751.751 0 0 1 .018-1.042.751.751 0 0 1 1.042-.018 1.998 1.998 0 0 0 2.83 0l2.5-2.5a2.002 2.002 0 0 0-2.83-2.83l-1.25 1.25a.751.751 0 0 1-1.042-.018.751.751 0 0 1-.018-1.042Zm-4.69 9.64a1.998 1.998 0 0 0 2.83 0l1.25-1.25a.751.751 0 0 1 1.042.018.751.751 0 0 1 .018 1.042l-1.25 1.25a3.5 3.5 0 1 1-4.95-4.95l2.5-2.5a3.5 3.5 0 0 1 4.95 0 .751.751 0 0 1-.018 1.042.751.751 0 0 1-1.042.018 1.998 1.998 0 0 0-2.83 0l-2.5 2.5a1.998 1.998 0 0 0 0 2.83Z\"><\/path><\/svg><\/a><\/div>\n<p dir=\"auto\">On the compilation phase, you will usually need to specify the different include paths so that the interfaces (.h, .hpp) which define structs, classes, constants, and functions can be found.<\/p>\n<p dir=\"auto\">With <code>gcc<\/code> and <code>llvm<\/code> include paths are passed with <code>-I\/path\/to\/includes<\/code>, you can pass as many <code>-I<\/code> as you need.<\/p>\n<p dir=\"auto\">In Windows, <code>cl.exe<\/code> takes include paths with the following syntax:<br \/>\n<code>\/I\"c:\\path\\to\\includes\\<\/code> you can also pass as many as you need.<\/p>\n<p dir=\"auto\">Some software uses macro definition variables that should be passed during compile time to decide what code to include.<\/p>\n<div class=\"markdown-heading\" dir=\"auto\">\n<h2 class=\"heading-element\" dir=\"auto\">Compilation flags<\/h2>\n<p><a id=\"user-content-compilation-flags\" class=\"anchor\" aria-label=\"Permalink: Compilation flags\" href=\"#compilation-flags\"><svg class=\"octicon octicon-link\" viewBox=\"0 0 16 16\" version=\"1.1\" width=\"16\" height=\"16\" aria-hidden=\"true\"><path d=\"m7.775 3.275 1.25-1.25a3.5 3.5 0 1 1 4.95 4.95l-2.5 2.5a3.5 3.5 0 0 1-4.95 0 .751.751 0 0 1 .018-1.042.751.751 0 0 1 1.042-.018 1.998 1.998 0 0 0 2.83 0l2.5-2.5a2.002 2.002 0 0 0-2.83-2.83l-1.25 1.25a.751.751 0 0 1-1.042-.018.751.751 0 0 1-.018-1.042Zm-4.69 9.64a1.998 1.998 0 0 0 2.83 0l1.25-1.25a.751.751 0 0 1 1.042.018.751.751 0 0 1 .018 1.042l-1.25 1.25a3.5 3.5 0 1 1-4.95-4.95l2.5-2.5a3.5 3.5 0 0 1 4.95 0 .751.751 0 0 1-.018 1.042.751.751 0 0 1-1.042.018 1.998 1.998 0 0 0-2.83 0l-2.5 2.5a1.998 1.998 0 0 0 0 2.83Z\"><\/path><\/svg><\/a><\/div>\n<p dir=\"auto\">These compilation-time variables are passed using <code>-D<\/code>,<br \/>\ne.g. <code>-DMYSOFTWARE_COMPILATION_VARIABLE<\/code> <code>-DDO_SOMETHING=1<\/code> <code>-DDISABLE_DEPRECATED_FUNCTIONS=0<\/code><\/p>\n<p dir=\"auto\">These compilation time flags are by convention usually put into a single variable named <code>CXXFLAGS<\/code>, which is then passed to the compiler as a parameter for convenience when you&#8217;re building your compilation\/make script.<\/p>\n<div class=\"markdown-heading\" dir=\"auto\">\n<h2 class=\"heading-element\" dir=\"auto\">Object files<\/h2>\n<p><a id=\"user-content-object-files\" class=\"anchor\" aria-label=\"Permalink: Object files\" href=\"#object-files\"><svg class=\"octicon octicon-link\" viewBox=\"0 0 16 16\" version=\"1.1\" width=\"16\" height=\"16\" aria-hidden=\"true\"><path d=\"m7.775 3.275 1.25-1.25a3.5 3.5 0 1 1 4.95 4.95l-2.5 2.5a3.5 3.5 0 0 1-4.95 0 .751.751 0 0 1 .018-1.042.751.751 0 0 1 1.042-.018 1.998 1.998 0 0 0 2.83 0l2.5-2.5a2.002 2.002 0 0 0-2.83-2.83l-1.25 1.25a.751.751 0 0 1-1.042-.018.751.751 0 0 1-.018-1.042Zm-4.69 9.64a1.998 1.998 0 0 0 2.83 0l1.25-1.25a.751.751 0 0 1 1.042.018.751.751 0 0 1 .018 1.042l-1.25 1.25a3.5 3.5 0 1 1-4.95-4.95l2.5-2.5a3.5 3.5 0 0 1 4.95 0 .751.751 0 0 1-.018 1.042.751.751 0 0 1-1.042.018 1.998 1.998 0 0 0-2.83 0l-2.5 2.5a1.998 1.998 0 0 0 0 2.83Z\"><\/path><\/svg><\/a><\/div>\n<p dir=\"auto\">When you compile your .c, or .cpp files, you will end up with object files.<br \/>\nThese files usually have <code>.o<\/code> extensions on Linux, on Windows they might be under <code>.obj<\/code> extensions.<\/p>\n<p dir=\"auto\">You can create an <code>.o<\/code> file for a single or for many source files.<\/p>\n<div class=\"markdown-heading\" dir=\"auto\">\n<h2 class=\"heading-element\" dir=\"auto\">Static Library files<\/h2>\n<p><a id=\"user-content-static-library-files\" class=\"anchor\" aria-label=\"Permalink: Static Library files\" href=\"#static-library-files\"><svg class=\"octicon octicon-link\" viewBox=\"0 0 16 16\" version=\"1.1\" width=\"16\" height=\"16\" aria-hidden=\"true\"><path d=\"m7.775 3.275 1.25-1.25a3.5 3.5 0 1 1 4.95 4.95l-2.5 2.5a3.5 3.5 0 0 1-4.95 0 .751.751 0 0 1 .018-1.042.751.751 0 0 1 1.042-.018 1.998 1.998 0 0 0 2.83 0l2.5-2.5a2.002 2.002 0 0 0-2.83-2.83l-1.25 1.25a.751.751 0 0 1-1.042-.018.751.751 0 0 1-.018-1.042Zm-4.69 9.64a1.998 1.998 0 0 0 2.83 0l1.25-1.25a.751.751 0 0 1 1.042.018.751.751 0 0 1 .018 1.042l-1.25 1.25a3.5 3.5 0 1 1-4.95-4.95l2.5-2.5a3.5 3.5 0 0 1 4.95 0 .751.751 0 0 1-.018 1.042.751.751 0 0 1-1.042.018 1.998 1.998 0 0 0-2.83 0l-2.5 2.5a1.998 1.998 0 0 0 0 2.83Z\"><\/path><\/svg><\/a><\/div>\n<p dir=\"auto\">When you have several <code>.o<\/code> files, you can put them together as a library, a static library. In Linux\/Mac these static libraries are simply archive files, or <code>.a<\/code> files. In windows, static library files exist under the <code>.lib<\/code> extension.<\/p>\n<p dir=\"auto\"><strong>They are created like this in Linux\/Mac:<\/strong><\/p>\n<p dir=\"auto\"><code>ar -cvq libctest.a ctest1.o ctest2.o ctest3.o<\/code><\/p>\n<p dir=\"auto\"><code>libctest.a<\/code> will contain <code>ctest1.o<\/code>,<code>ctest2.o<\/code> and <code>ctest2.o<\/code><\/p>\n<p dir=\"auto\"><strong>They are created like this on Windows:<\/strong><\/p>\n<p dir=\"auto\"><code>LIB.EXE \/OUT:MYLIB.LIB FILE1.OBJ FILE2.OBJ FILE3.OBJ<\/code><\/p>\n<div class=\"markdown-heading\" dir=\"auto\">\n<h2 class=\"heading-element\" dir=\"auto\">Shared Libraries (Dynamic Libraries)<\/h2>\n<p><a id=\"user-content-shared-libraries-dynamic-libraries\" class=\"anchor\" aria-label=\"Permalink: Shared Libraries (Dynamic Libraries)\" href=\"#shared-libraries-dynamic-libraries\"><svg class=\"octicon octicon-link\" viewBox=\"0 0 16 16\" version=\"1.1\" width=\"16\" height=\"16\" aria-hidden=\"true\"><path d=\"m7.775 3.275 1.25-1.25a3.5 3.5 0 1 1 4.95 4.95l-2.5 2.5a3.5 3.5 0 0 1-4.95 0 .751.751 0 0 1 .018-1.042.751.751 0 0 1 1.042-.018 1.998 1.998 0 0 0 2.83 0l2.5-2.5a2.002 2.002 0 0 0-2.83-2.83l-1.25 1.25a.751.751 0 0 1-1.042-.018.751.751 0 0 1-.018-1.042Zm-4.69 9.64a1.998 1.998 0 0 0 2.83 0l1.25-1.25a.751.751 0 0 1 1.042.018.751.751 0 0 1 .018 1.042l-1.25 1.25a3.5 3.5 0 1 1-4.95-4.95l2.5-2.5a3.5 3.5 0 0 1 4.95 0 .751.751 0 0 1-.018 1.042.751.751 0 0 1-1.042.018 1.998 1.998 0 0 0-2.83 0l-2.5 2.5a1.998 1.998 0 0 0 0 2.83Z\"><\/path><\/svg><\/a><\/div>\n<p dir=\"auto\">Shared or dynamic libraries, such as <code>.so<\/code> in Linux, <code>.dylib<\/code> in Mac, and <code>.dll<\/code> in Windows, are critical for reducing executable sizes and memory consumption, but they differ in their implementation and behavior.<\/p>\n<p dir=\"auto\">In Linux, <code>.so<\/code> files are created like this:<\/p>\n<pre><code>gcc -Wall -fPIC -c *.c\ngcc -shared -Wl,-soname,libctest.so.1 -o libctest.so.1.0 *.o\n<\/code><\/pre>\n<ul dir=\"auto\">\n<li><code>-Wall<\/code> enables all warnings.<\/li>\n<li><code>-c<\/code> means compile only, don&#8217;t run the linker.<\/li>\n<li><code>-fPIC<\/code> means &#8220;Position Independent Code&#8221;, a requirement for shared libraries in Linux.<\/li>\n<li><code>-shared<\/code> makes the object file created shareable by different executables.<\/li>\n<li><code>-Wl<\/code> passes a comma separated list of arguments to the linker.<\/li>\n<li><code>-soname<\/code> means &#8220;shared object name&#8221; to use.<\/li>\n<li><code>-o &lt;my.so&gt;<\/code> means output, in this case the output shared library<\/li>\n<\/ul>\n<p dir=\"auto\">In Mac, <code>.dylib<\/code> files are created similarly:<\/p>\n<p dir=\"auto\"><code>clang -dynamiclib -o libtest.dylib file1.o file2.o -L\/some\/library\/path -lname_of_library_without_lib_prefix<\/code><\/p>\n<div class=\"markdown-heading\" dir=\"auto\">\n<h3 class=\"heading-element\" dir=\"auto\">Dynamic Link Libraries (DLLs) in Windows<\/h3>\n<p><a id=\"user-content-dynamic-link-libraries-dlls-in-windows\" class=\"anchor\" aria-label=\"Permalink: Dynamic Link Libraries (DLLs) in Windows\" href=\"#dynamic-link-libraries-dlls-in-windows\"><svg class=\"octicon octicon-link\" viewBox=\"0 0 16 16\" version=\"1.1\" width=\"16\" height=\"16\" aria-hidden=\"true\"><path d=\"m7.775 3.275 1.25-1.25a3.5 3.5 0 1 1 4.95 4.95l-2.5 2.5a3.5 3.5 0 0 1-4.95 0 .751.751 0 0 1 .018-1.042.751.751 0 0 1 1.042-.018 1.998 1.998 0 0 0 2.83 0l2.5-2.5a2.002 2.002 0 0 0-2.83-2.83l-1.25 1.25a.751.751 0 0 1-1.042-.018.751.751 0 0 1-.018-1.042Zm-4.69 9.64a1.998 1.998 0 0 0 2.83 0l1.25-1.25a.751.751 0 0 1 1.042.018.751.751 0 0 1 .018 1.042l-1.25 1.25a3.5 3.5 0 1 1-4.95-4.95l2.5-2.5a3.5 3.5 0 0 1 4.95 0 .751.751 0 0 1-.018 1.042.751.751 0 0 1-1.042.018 1.998 1.998 0 0 0-2.83 0l-2.5 2.5a1.998 1.998 0 0 0 0 2.83Z\"><\/path><\/svg><\/a><\/div>\n<p dir=\"auto\">DLLs in Windows function differently from shared objects in Unix\/Linux systems. They tie both exports and imports to specific DLL names, creating a more rigid binding. This difference is essential for understanding how symbols and libraries are managed in different environments.<\/p>\n<p dir=\"auto\">In Windows, <code>.dll<\/code> files are created like this:<\/p>\n<p dir=\"auto\"><code>LINK.EXE \/DLL \/OUT:MYLIB.DLL FILE1.OBJ FILE2.OBJ FILE3OBJ<\/code><\/p>\n<div class=\"markdown-heading\" dir=\"auto\">\n<h3 class=\"heading-element\" dir=\"auto\">Linking with MSVC and Binutils<\/h3>\n<p><a id=\"user-content-linking-with-msvc-and-binutils\" class=\"anchor\" aria-label=\"Permalink: Linking with MSVC and Binutils\" href=\"#linking-with-msvc-and-binutils\"><svg class=\"octicon octicon-link\" viewBox=\"0 0 16 16\" version=\"1.1\" width=\"16\" height=\"16\" aria-hidden=\"true\"><path d=\"m7.775 3.275 1.25-1.25a3.5 3.5 0 1 1 4.95 4.95l-2.5 2.5a3.5 3.5 0 0 1-4.95 0 .751.751 0 0 1 .018-1.042.751.751 0 0 1 1.042-.018 1.998 1.998 0 0 0 2.83 0l2.5-2.5a2.002 2.002 0 0 0-2.83-2.83l-1.25 1.25a.751.751 0 0 1-1.042-.018.751.751 0 0 1-.018-1.042Zm-4.69 9.64a1.998 1.998 0 0 0 2.83 0l1.25-1.25a.751.751 0 0 1 1.042.018.751.751 0 0 1 .018 1.042l-1.25 1.25a3.5 3.5 0 1 1-4.95-4.95l2.5-2.5a3.5 3.5 0 0 1 4.95 0 .751.751 0 0 1-.018 1.042.751.751 0 0 1-1.042.018 1.998 1.998 0 0 0-2.83 0l-2.5 2.5a1.998 1.998 0 0 0 0 2.83Z\"><\/path><\/svg><\/a><\/div>\n<p dir=\"auto\">When linking with MSVC, unlike Unix\/Linux linkers where you point directly to the <code>.so<\/code>, you link against an import library (<code>.lib<\/code>) or in rare cases the <code>.exp<\/code>. These import libraries contain import thunks and a symbol index, facilitating the linking process. The <code>.lib<\/code> format is used by both MSVC and GCC on Linux, but with different contents (COFF files in MSVC, ELF <code>.o<\/code> files in GCC).<\/p>\n<div class=\"markdown-heading\" dir=\"auto\">\n<h2 class=\"heading-element\" dir=\"auto\">Linking to existing libraries<\/h2>\n<p><a id=\"user-content-linking-to-existing-libraries\" class=\"anchor\" aria-label=\"Permalink: Linking to existing libraries\" href=\"#linking-to-existing-libraries\"><svg class=\"octicon octicon-link\" viewBox=\"0 0 16 16\" version=\"1.1\" width=\"16\" height=\"16\" aria-hidden=\"true\"><path d=\"m7.775 3.275 1.25-1.25a3.5 3.5 0 1 1 4.95 4.95l-2.5 2.5a3.5 3.5 0 0 1-4.95 0 .751.751 0 0 1 .018-1.042.751.751 0 0 1 1.042-.018 1.998 1.998 0 0 0 2.83 0l2.5-2.5a2.002 2.002 0 0 0-2.83-2.83l-1.25 1.25a.751.751 0 0 1-1.042-.018.751.751 0 0 1-.018-1.042Zm-4.69 9.64a1.998 1.998 0 0 0 2.83 0l1.25-1.25a.751.751 0 0 1 1.042.018.751.751 0 0 1 .018 1.042l-1.25 1.25a3.5 3.5 0 1 1-4.95-4.95l2.5-2.5a3.5 3.5 0 0 1 4.95 0 .751.751 0 0 1-.018 1.042.751.751 0 0 1-1.042.018 1.998 1.998 0 0 0-2.83 0l-2.5 2.5a1.998 1.998 0 0 0 0 2.83Z\"><\/path><\/svg><\/a><\/div>\n<p dir=\"auto\">When linking your software you may be faced with a situation on which you want to link against several standard shared libraries.<br \/>\nIf all the libraries you need exist in a single folder, you can set the <code>LD_LIBRARY_PATH<\/code> to that folder. <em><strong>By common standard all shared libraries are prefixed with the word <code>lib<\/code><\/strong><\/em>. If a library exists in <code>LD_LIBRARY_PATH<\/code> and you want to link against it, you don&#8217;t need to pass the entire path to the library, you simply pass <code>-lname<\/code> and you will link your executable to the symbols of <code>libname.so<\/code> which should be somewhere inside <code>LD_LIBRARY_PATH<\/code>.<\/p>\n<p dir=\"auto\">Tip: You should probably stay away from altering your <code>LD_LIBRARY_PATH<\/code>, if you do, make sure you keep its original value, and when you&#8217;re done restore it, as you might screw the build processes of other software in the system which might depend on what&#8217;s on the <code>LD_LIBRARY_PATH<\/code>.<\/p>\n<div class=\"markdown-heading\" dir=\"auto\">\n<h3 class=\"heading-element\" dir=\"auto\">What if libraries are in different folders?<\/h3>\n<p><a id=\"user-content-what-if-libraries-are-in-different-folders\" class=\"anchor\" aria-label=\"Permalink: What if libraries are in different folders?\" href=\"#what-if-libraries-are-in-different-folders\"><svg class=\"octicon octicon-link\" viewBox=\"0 0 16 16\" version=\"1.1\" width=\"16\" height=\"16\" aria-hidden=\"true\"><path d=\"m7.775 3.275 1.25-1.25a3.5 3.5 0 1 1 4.95 4.95l-2.5 2.5a3.5 3.5 0 0 1-4.95 0 .751.751 0 0 1 .018-1.042.751.751 0 0 1 1.042-.018 1.998 1.998 0 0 0 2.83 0l2.5-2.5a2.002 2.002 0 0 0-2.83-2.83l-1.25 1.25a.751.751 0 0 1-1.042-.018.751.751 0 0 1-.018-1.042Zm-4.69 9.64a1.998 1.998 0 0 0 2.83 0l1.25-1.25a.751.751 0 0 1 1.042.018.751.751 0 0 1 .018 1.042l-1.25 1.25a3.5 3.5 0 1 1-4.95-4.95l2.5-2.5a3.5 3.5 0 0 1 4.95 0 .751.751 0 0 1-.018 1.042.751.751 0 0 1-1.042.018 1.998 1.998 0 0 0-2.83 0l-2.5 2.5a1.998 1.998 0 0 0 0 2.83Z\"><\/path><\/svg><\/a><\/div>\n<p dir=\"auto\">If you have some other <code>libbar.so<\/code> library on another folder outside <code>LD_LIBRARY_PATH<\/code> you can explictly pass the full path to that library <code>\/path\/to\/that\/other\/library\/libbar.so<\/code>, or you can specify the folder that contains it <code>-L\/path\/to\/that\/other\/library<\/code> and then the short hand form <code>-lbar<\/code>. This latter option makes more sense if the second folder contains several other libraries.<\/p>\n<div class=\"markdown-heading\" dir=\"auto\">\n<h2 class=\"heading-element\" dir=\"auto\">Useful tools<\/h2>\n<p><a id=\"user-content-useful-tools\" class=\"anchor\" aria-label=\"Permalink: Useful tools\" href=\"#useful-tools\"><svg class=\"octicon octicon-link\" viewBox=\"0 0 16 16\" version=\"1.1\" width=\"16\" height=\"16\" aria-hidden=\"true\"><path d=\"m7.775 3.275 1.25-1.25a3.5 3.5 0 1 1 4.95 4.95l-2.5 2.5a3.5 3.5 0 0 1-4.95 0 .751.751 0 0 1 .018-1.042.751.751 0 0 1 1.042-.018 1.998 1.998 0 0 0 2.83 0l2.5-2.5a2.002 2.002 0 0 0-2.83-2.83l-1.25 1.25a.751.751 0 0 1-1.042-.018.751.751 0 0 1-.018-1.042Zm-4.69 9.64a1.998 1.998 0 0 0 2.83 0l1.25-1.25a.751.751 0 0 1 1.042.018.751.751 0 0 1 .018 1.042l-1.25 1.25a3.5 3.5 0 1 1-4.95-4.95l2.5-2.5a3.5 3.5 0 0 1 4.95 0 .751.751 0 0 1-.018 1.042.751.751 0 0 1-1.042.018 1.998 1.998 0 0 0-2.83 0l-2.5 2.5a1.998 1.998 0 0 0 0 2.83Z\"><\/path><\/svg><\/a><\/div>\n<p dir=\"auto\">Sometimes you may be dealing with issues like <code>undefined symbol<\/code> errors, and you may want to inspect what symbols (functions) are defined in your library.<\/p>\n<p dir=\"auto\">On Mac there&#8217;s <code>otool<\/code>, on Linux\/Mac there&#8217;s <code>nm<\/code>, on Windows there&#8217;s <code>depends.exe<\/code> (a GUI tool that can be used to see both dependencies and the symbol&#8217;s tables. Taking a look at the &#8220;Entry Point&#8221; column will help you understand clearly the difference between symbols linking to a shared library vs symbols linking statically to the same library)<\/p>\n<div class=\"markdown-heading\" dir=\"auto\">\n<h3 class=\"heading-element\" dir=\"auto\">Useful command options<\/h3>\n<p><a id=\"user-content-useful-command-options\" class=\"anchor\" aria-label=\"Permalink: Useful command options\" href=\"#useful-command-options\"><svg class=\"octicon octicon-link\" viewBox=\"0 0 16 16\" version=\"1.1\" width=\"16\" height=\"16\" aria-hidden=\"true\"><path d=\"m7.775 3.275 1.25-1.25a3.5 3.5 0 1 1 4.95 4.95l-2.5 2.5a3.5 3.5 0 0 1-4.95 0 .751.751 0 0 1 .018-1.042.751.751 0 0 1 1.042-.018 1.998 1.998 0 0 0 2.83 0l2.5-2.5a2.002 2.002 0 0 0-2.83-2.83l-1.25 1.25a.751.751 0 0 1-1.042-.018.751.751 0 0 1-.018-1.042Zm-4.69 9.64a1.998 1.998 0 0 0 2.83 0l1.25-1.25a.751.751 0 0 1 1.042.018.751.751 0 0 1 .018 1.042l-1.25 1.25a3.5 3.5 0 1 1-4.95-4.95l2.5-2.5a3.5 3.5 0 0 1 4.95 0 .751.751 0 0 1-.018 1.042.751.751 0 0 1-1.042.018 1.998 1.998 0 0 0-2.83 0l-2.5 2.5a1.998 1.998 0 0 0 0 2.83Z\"><\/path><\/svg><\/a><\/div>\n<p dir=\"auto\"><em><strong>See shared library dependencies on Mac with <code>otool<\/code><\/strong><\/em><\/p>\n<pre><code>otool -L libjlibtorrent.dylib \nlibjlibtorrent.dylib:\n\tlibjlibtorrent.dylib (compatibility version 0.0.0, current version 0.0.0)\n\t\/usr\/lib\/libc++.1.dylib (compatibility version 1.0.0, current version 120.0.0)\n\t\/usr\/lib\/libSystem.B.dylib (compatibility version 1.0.0, current version 1213.0.0)\n<\/code><\/pre>\n<p dir=\"auto\"><em><strong>See shared symbols with <code>nm<\/code> (Linux\/Mac)<\/strong><\/em><br \/>\nWith nm, you can see the symbol&#8217;s name list.<br \/>\nFamiliarize yourself with the meaning of the symbol types:<\/p>\n<ul dir=\"auto\">\n<li><code>T<\/code> (text section symbol)<\/li>\n<li><code>U<\/code> (undefined &#8211; useful for those <code>undefined symbol<\/code> error),<\/li>\n<li><code>I<\/code> (indirect symbol).<\/li>\n<li><\/li>\n<\/ul>\n<p dir=\"auto\">If the symbol is local (non-external) the symbol type is presented in lowercase letters, for example a lowercase <code>u<\/code> represents an undefined reference to a private external in another module in the same library.<\/p>\n<p dir=\"auto\"><code>nm<\/code>&#8216;s documentation says that if you&#8217;re working on Mac and you see that the symbol is preceeded by <code>+<\/code> or <code>-<\/code> it means it&#8217;s an ObjectiveC method, if you&#8217;re familiar with ObjectiveC you will know that <code>+<\/code> is for class methods and <code>-<\/code> is for instance methods, but in practice it seems to be a bit more explicit and you will often see <code>objc<\/code> or <code>OBJC<\/code> prefixed to those methods.<\/p>\n<p dir=\"auto\"><code>nm<\/code> is best used along with <code>grep<\/code> \ud83d\ude09<\/p>\n<p dir=\"auto\"><em><strong>Find all Undefined symbols<\/strong><\/em><\/p>\n<pre><code>nm -u libMacOSXUtilsLeopard.jnilib\n_CFRelease\n_LSSharedFileListCopySnapshot\n_LSSharedFileListCreate\n_LSSharedFileListInsertItemURL\n_LSSharedFileListItemRemove\n_LSSharedFileListItemResolve\n_NSFullUserName\n_OBJC_CLASS_$_NSArray\n_OBJC_CLASS_$_NSAutoreleasePool\n_OBJC_CLASS_$_NSDictionary\n_OBJC_CLASS_$_NSMutableArray\n_OBJC_CLASS_$_NSMutableDictionary\n_OBJC_CLASS_$_NSString\n_OBJC_CLASS_$_NSURL\n__Block_copy\n__NSConcreteGlobalBlock\n__dyld_register_func_for_add_image\n__objc_empty_cache\n__objc_empty_vtable\n_calloc\n_class_addMethod\n_class_getInstanceMethod\n_class_getInstanceSize\n_class_getInstanceVariable\n_class_getIvarLayout\n<\/code><\/pre>\n<div class=\"markdown-heading\" dir=\"auto\">\n<h3 class=\"heading-element\" dir=\"auto\">My C++ code compiles but it won&#8217;t link<\/h3>\n<p><a id=\"user-content-my-c-code-compiles-but-it-wont-link\" class=\"anchor\" aria-label=\"Permalink: My C++ code compiles but it won't link\" href=\"#my-c-code-compiles-but-it-wont-link\"><svg class=\"octicon octicon-link\" viewBox=\"0 0 16 16\" version=\"1.1\" width=\"16\" height=\"16\" aria-hidden=\"true\"><path d=\"m7.775 3.275 1.25-1.25a3.5 3.5 0 1 1 4.95 4.95l-2.5 2.5a3.5 3.5 0 0 1-4.95 0 .751.751 0 0 1 .018-1.042.751.751 0 0 1 1.042-.018 1.998 1.998 0 0 0 2.83 0l2.5-2.5a2.002 2.002 0 0 0-2.83-2.83l-1.25 1.25a.751.751 0 0 1-1.042-.018.751.751 0 0 1-.018-1.042Zm-4.69 9.64a1.998 1.998 0 0 0 2.83 0l1.25-1.25a.751.751 0 0 1 1.042.018.751.751 0 0 1 .018 1.042l-1.25 1.25a3.5 3.5 0 1 1-4.95-4.95l2.5-2.5a3.5 3.5 0 0 1 4.95 0 .751.751 0 0 1-.018 1.042.751.751 0 0 1-1.042.018 1.998 1.998 0 0 0-2.83 0l-2.5 2.5a1.998 1.998 0 0 0 0 2.83Z\"><\/path><\/svg><\/a><\/div>\n<p dir=\"auto\">Linking is simply &#8220;linking&#8221; a bunch of .o files to make an executable.<\/p>\n<p dir=\"auto\">Each one of these .o&#8217;s may be compiled on their own out of their .cpp files, but when one references symbols that are supposed to exist in other .o&#8217;s and they&#8217;re not to be found then you get linking errors.<\/p>\n<p dir=\"auto\">Perhaps through forward declarations you managed your compilation phase to pass, but then you get a bunch of symbol not found errors.<br \/>\nMake sure to read them slowly, see where these symbols are being referenced, you will see that these issues occur due to namespace visibility in most cases.<\/p>\n<p dir=\"auto\">Perhaps you copied the signature of a method that exists in a private space elsewhere into some other namespace where your code wasn&#8217;t compiling, all you did was make it compilable, but the actual symbol might not be visible outside the scope where it&#8217;s truly defined and implemented.<\/p>\n<p dir=\"auto\">Function symbols can be private if they&#8217;re declared inside anonymous namespaces, or if they&#8217;re declared as <code>static<\/code> functions.<\/p>\n<p dir=\"auto\">An example:<\/p>\n<pre><code>Undefined symbols for architecture x86_64:\n  \"FlushStateToDisk(CValidationState&amp;, FlushStateMode)\", referenced from:\n      Network::TxMessage::handle(CNode*, CDataStream&amp;, long long, std::__1::basic_string&lt;char, std::__1::char_traits&lt;char&gt;, std::__1::allocator&lt;char&gt; &gt;&amp;, bool, bool) in libbitcoin_server.a(libbitcoin_server_a-TxMessage.o)\n<\/code><\/pre>\n<p dir=\"auto\">Here, when I read the code of <code>Network::TxMessage::handle(...)<\/code> there was a call to <code>FlushStateToDisk<\/code>, which was declared in <code>main.h<\/code>, and coded in <code>main.cpp<\/code>. My <code>TxMessage.cpp<\/code> did include <code>main.h<\/code>, the compilation was fine, I had a <code>TxMessage.o<\/code> file and a <code>main.o<\/code>, but the linker was complaining.<\/p>\n<p dir=\"auto\">The issue was that <code>FlushStateToDisk<\/code> was declared as a <code>static<\/code>, therefore only visible inside <code>main.o<\/code>, once I removed the <code>static<\/code> from the declaration and implementation the error went away and my executable was linked. Similar things happen when functions are declared in anonymous spaces in other files, even if you forward declare them on your local <code>.h<\/code><\/p>\n<p dir=\"auto\">In other cases your code compiles and you get this error linking because your library can&#8217;t be added using -lfoo, and adding its containing folder to -L doesn&#8217;t cut it, in this case you just add the full path to the library in your compilation command: <code>gcc \/path\/to\/the\/missing\/library.o ... my_source.cpp -o my_executable<\/code><\/p>\n<div class=\"markdown-heading\" dir=\"auto\">\n<h3 class=\"heading-element\" dir=\"auto\">Reminder:<\/h3>\n<p><a id=\"user-content-reminder\" class=\"anchor\" aria-label=\"Permalink: Reminder:\" href=\"#reminder\"><svg class=\"octicon octicon-link\" viewBox=\"0 0 16 16\" version=\"1.1\" width=\"16\" height=\"16\" aria-hidden=\"true\"><path d=\"m7.775 3.275 1.25-1.25a3.5 3.5 0 1 1 4.95 4.95l-2.5 2.5a3.5 3.5 0 0 1-4.95 0 .751.751 0 0 1 .018-1.042.751.751 0 0 1 1.042-.018 1.998 1.998 0 0 0 2.83 0l2.5-2.5a2.002 2.002 0 0 0-2.83-2.83l-1.25 1.25a.751.751 0 0 1-1.042-.018.751.751 0 0 1-.018-1.042Zm-4.69 9.64a1.998 1.998 0 0 0 2.83 0l1.25-1.25a.751.751 0 0 1 1.042.018.751.751 0 0 1 .018 1.042l-1.25 1.25a3.5 3.5 0 1 1-4.95-4.95l2.5-2.5a3.5 3.5 0 0 1 4.95 0 .751.751 0 0 1-.018 1.042.751.751 0 0 1-1.042.018 1.998 1.998 0 0 0-2.83 0l-2.5 2.5a1.998 1.998 0 0 0 0 2.83Z\"><\/path><\/svg><\/a><\/div>\n<p dir=\"auto\"><strong>DO NOT EXPORT CFLAGS, CPPFLAGS and the like on your <code>.bash_profile<\/code>\/<code>.bashrc<\/code><\/strong>, it can lead to unintended building consequences in many projects. I&#8217;ve wasted so many hours due to this mistake.<\/p>\n<\/article><\/div>\n<\/p><\/div>\n<\/div><\/div>\n<div class=\"gist-meta\">\n        <a href=\"https:\/\/gist.github.com\/gubatron\/32f82053596c24b6bec6\/raw\/10508cf254f1aa483c0424a8f433fa7eaf25322d\/compiling_building_c_cpp_notes.md\" style=\"float:right\" class=\"Link--inTextBlock\">view raw<\/a><br \/>\n        <a href=\"https:\/\/gist.github.com\/gubatron\/32f82053596c24b6bec6#file-compiling_building_c_cpp_notes-md\" class=\"Link--inTextBlock\"><br \/>\n          compiling_building_c_cpp_notes.md<br \/>\n        <\/a><br \/>\n        hosted with &#10084; by <a class=\"Link--inTextBlock\" href=\"https:\/\/github.com\">GitHub<\/a>\n      <\/div>\n<\/p><\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Things to remember when compiling\/linking C\/C++ software by Angel Leon. March 17, 2015; Last update on December 14, 2023 Updated on February 27, 2023 Updated August 29, 2019. Include Paths On the compilation phase, you will usually need to specify the different include paths so that the interfaces (.h, .hpp) which define structs, classes, constants, [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"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":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[15],"tags":[232,239,1645,1646],"class_list":["post-3949","post","type-post","status-publish","format-standard","hentry","category-code","tag-building","tag-c","tag-compilation","tag-linking"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p5Unzf-11H","jetpack-related-posts":[{"id":3383,"url":"https:\/\/www.gubatron.com\/blog\/things-to-remember-when-compilinglinking-cc-software\/","url_meta":{"origin":3949,"position":0},"title":"# Things to remember when compiling\/linking C\/C++ software","author":"gubatron","date":"March 18, 2015","format":false,"excerpt":"by Angel Leon. March 17, 2015. Include Paths On the compilation phase, you will usually need to specify the different include paths so that the interfaces (.h, .hpp) which define structs, classes, constans, and functions can be found. With gcc and llvm include paths are passed with -I\/path\/to\/includes, you can\u2026","rel":"","context":"In &quot;Code&quot;","block_context":{"text":"Code","link":"https:\/\/www.gubatron.com\/blog\/category\/code\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/www.gubatron.com\/blog\/wp-content\/uploads\/2015\/03\/make-all-executables.png?fit=680%2C510&ssl=1&resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.gubatron.com\/blog\/wp-content\/uploads\/2015\/03\/make-all-executables.png?fit=680%2C510&ssl=1&resize=350%2C200 1x, https:\/\/i0.wp.com\/www.gubatron.com\/blog\/wp-content\/uploads\/2015\/03\/make-all-executables.png?fit=680%2C510&ssl=1&resize=525%2C300 1.5x"},"classes":[]},{"id":470,"url":"https:\/\/www.gubatron.com\/blog\/how-to-build-pyqt4-for-windows\/","url_meta":{"origin":3949,"position":1},"title":"How to build PyQt4 for Windows","author":"gubatron","date":"February 23, 2007","format":false,"excerpt":"- Make sure you don't have cygwin or C:devkitPromsysbin in your path, or else your make files can end up trying to run the Makefile using 'sh' and all the makefile we're about to use are meant to run from the windows command line. If for some odd reason you\u2026","rel":"","context":"In &quot;Code&quot;","block_context":{"text":"Code","link":"https:\/\/www.gubatron.com\/blog\/category\/code\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":3236,"url":"https:\/\/www.gubatron.com\/blog\/how-to-actually-build-bitcoin-on-mac-osx-10-9-1\/","url_meta":{"origin":3949,"position":2},"title":"How to actually build bitcoin on Mac OSX 10.9.1","author":"gubatron","date":"February 3, 2014","format":false,"excerpt":"First of all, if you have Macports, do yourself a favor and get rid of it. Then make sure you have Homebrew installed and all the packages installed by it up to date. 1. Let's install all the dependencies for Bitcoin hacking. brew install autoconf automake berkeley-db4 boost miniupnpc openssl\u2026","rel":"","context":"In &quot;BitCoin&quot;","block_context":{"text":"BitCoin","link":"https:\/\/www.gubatron.com\/blog\/category\/bitcoin-2\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":3215,"url":"https:\/\/www.gubatron.com\/blog\/building-cgminer-from-source-on-osx\/","url_meta":{"origin":3949,"position":3},"title":"building cgminer from source on OSX","author":"gubatron","date":"January 19, 2014","format":false,"excerpt":"so you cloned the cgminer repo from github to build on your OSX machine and you get this bullshit error $ .\/autogen.sh readlink: illegal option -- f usage: readlink [-n] [file ...] usage: dirname path touch: \/ltmain.sh: Permission denied Use of chdir('') or chdir(undef) as chdir() is deprecated at \/usr\/local\/bin\/autoreconf\u2026","rel":"","context":"In &quot;Code&quot;","block_context":{"text":"Code","link":"https:\/\/www.gubatron.com\/blog\/category\/code\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":235,"url":"https:\/\/www.gubatron.com\/blog\/open-sourcing-since-the-early-days\/","url_meta":{"origin":3949,"position":4},"title":"Open Sourcing since the early days","author":"gubatron","date":"January 11, 2006","format":false,"excerpt":"Back in 1998 I was on my first year of Software Engineering in UCAB, our Algorithms and Programming I (by Prof. Omar Mendez and Alvaro Reb\u00f3n) course was dictated using a functional language which at the time sounded esoteric to us, Haskell. (I'm glad I started with Haskell, We knew\u2026","rel":"","context":"In &quot;Gubatron&quot;","block_context":{"text":"Gubatron","link":"https:\/\/www.gubatron.com\/blog\/category\/gubatron\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":1336,"url":"https:\/\/www.gubatron.com\/blog\/compiling-ghost-white-crab-on-linux-x86_64-ubuntu-linux\/","url_meta":{"origin":3949,"position":5},"title":"Compiling Ghost White Crab on Linux x86_64 Ubuntu Linux","author":"gubatron","date":"July 26, 2009","format":false,"excerpt":"Compiling this software is usually just a matter of running 'make', but if you try to do so in Ubuntu 64bit, you'll encounter the following error on the config_test.log file skipping incompatible \/usr\/lib\/gcc\/x86_64-linux-gnu\/4.3.3\/libgcc.a when searching for -lgcc To solve this issue just do the following: apt-get install libc6-dev-i386 This makes\u2026","rel":"","context":"In &quot;Geeklife&quot;","block_context":{"text":"Geeklife","link":"https:\/\/www.gubatron.com\/blog\/category\/geeklife\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]}],"_links":{"self":[{"href":"https:\/\/www.gubatron.com\/blog\/wp-json\/wp\/v2\/posts\/3949","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.gubatron.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.gubatron.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.gubatron.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.gubatron.com\/blog\/wp-json\/wp\/v2\/comments?post=3949"}],"version-history":[{"count":1,"href":"https:\/\/www.gubatron.com\/blog\/wp-json\/wp\/v2\/posts\/3949\/revisions"}],"predecessor-version":[{"id":3950,"href":"https:\/\/www.gubatron.com\/blog\/wp-json\/wp\/v2\/posts\/3949\/revisions\/3950"}],"wp:attachment":[{"href":"https:\/\/www.gubatron.com\/blog\/wp-json\/wp\/v2\/media?parent=3949"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.gubatron.com\/blog\/wp-json\/wp\/v2\/categories?post=3949"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.gubatron.com\/blog\/wp-json\/wp\/v2\/tags?post=3949"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}