{"id":32,"date":"2016-05-06T16:52:03","date_gmt":"2016-05-06T08:52:03","guid":{"rendered":"http:\/\/blogs.gentoo.org\/gsoc2016-native-clang\/?p=32"},"modified":"2017-03-08T03:32:21","modified_gmt":"2017-03-07T19:32:21","slug":"build-a-gnu-free-c-program-on-gentoo","status":"publish","type":"post","link":"https:\/\/blogs.gentoo.org\/gsoc2016-native-clang\/2016\/05\/06\/build-a-gnu-free-c-program-on-gentoo\/","title":{"rendered":"Build a GNU-free C++ program on Gentoo"},"content":{"rendered":"<p>This post is a following-up of my previous\u00a0one:\u00a0<a href=\"https:\/\/blogs.gentoo.org\/gsoc2016-native-clang\/2016\/05\/05\/build-a-freestanding-libcxx\/\">Build a freestanding libc++<\/a>. Here I&#8217;ll demonstrate how to link a C++ program with the freestanding libc++ we just built. The resulting executable will have no dependence on glibc or any GCC component,\u00a0thus is GNU-free.<\/p>\n<p>As the libc++ in use is linked with musl, the C++ program to be linked with libc++ need also\u00a0be linked with musl. In the previous post we already got a musl-based toolchain via <code>crossdev<\/code>, but its C++ compiler is g++, which unfortunately doesn&#8217;t support libc++. We&#8217;ll have to use a vanilla clang++ instead to compile our C++ program then.<\/p>\n<p>The problem with using a vanilla clang++ is that it&#8217;s not musl-aware, i.e. it doesn&#8217;t know where to find musl&#8217;s headers and libraries. Such information can only\u00a0be fed to clang++ via some cumbersome command-line arguments. Assume our C++ runtime libraries (including libc++) built in previous\u00a0post are installed under directory $LOCAL; the complete command for compiling a GNU-free C++ program looks like this:<\/p>\n<p><code>clang++ hello.cc \\<br \/>\n-nostdinc -isystem \/usr\/x86_64-pc-linux-musl\/usr\/include \\<br \/>\n-I $LOCAL\/include\/c++\/v1 \\<br \/>\n-L \/usr\/x86_64-pc-linux-musl\/usr\/lib -L $LOCAL\/lib \\<br \/>\n-nostartfiles \/usr\/x86_64-pc-linux-musl\/usr\/lib\/crt1.o \\<br \/>\n-Wl,-dynamic-linker,\/usr\/x86_64-pc-linux-musl\/lib\/ld-musl-x86_64.so.1 \\<br \/>\n-Wl,-rpath,\/usr\/x86_64-pc-linux-musl\/usr\/lib,-rpath,$LOCAL\/lib \\<br \/>\n-nodefaultlibs -stdlib=libc++ -lc -lc++<br \/>\n<\/code><\/p>\n<p>This is indeed a long command. Because clang doesn&#8217;t have something like GCC&#8217;s specs file, we have to elaborately specify every configuration on the command line. This is also what I&#8217;m going to improve this summer. Hopefully we&#8217;ll make clang more friendly to musl, so we don&#8217;t have to issue\u00a0such obscure commands when clang is eventually deployed as Gentoo&#8217;s default compiler.<\/p>\n<p>Now let me explain these command options in some detail:<\/p>\n<ul>\n<li>&#8211;<strong>nostdinc<\/strong>\u00a0tells clang not to include the standard headers, following by two arguments\u00a0specifying location of C and C++ headers, then another two specifying location of shared libraries. This is necessary because clang is not configured with musl and thus has\u00a0no way to find the correct headers and libs.<\/li>\n<li>&#8211;<strong>nostartfiles<\/strong>\u00a0tells clang to link the program with musl&#8217;s version of start files instead of glibc&#8217;s.<\/li>\n<li>&#8211;<strong>dynamic-linker<\/strong>\u00a0and &#8211;<strong>rpath<\/strong>\u00a0are options passed to the linker. They tell the program\u00a0what dynamic linker to use and where to find necessary dynamic libraries at runtime.<\/li>\n<li>&#8211;<strong>nodefaultlibs<\/strong>, like -stdinc, tells clang to link with musl and the C++ runtime libraries we specify, instead of the default ones.<\/li>\n<\/ul>\n<p>For convenience, I put this really long command in a shell script named musl-clang++ and replace &#8220;hello.cc&#8221; with &#8220;$@&#8221; for general use. Let&#8217;s see if it works:<\/p>\n<p><code>$ musl-clang++ hello.cc -o hello<br \/>\n$ readelf -d hello | grep NEEDED<br \/>\n0x0000000000000001 (NEEDED) Shared library: [libc.so]<br \/>\n0x0000000000000001 (NEEDED) Shared library: [libc++.so.1]<br \/>\n0x0000000000000001 (NEEDED) Shared library: [libc++abi.so.1]<br \/>\n$ .\/hello<br \/>\nHello<br \/>\n<\/code><br \/>\nYay!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This post is a following-up of my previous\u00a0one:\u00a0Build a freestanding libc++. Here I&#8217;ll demonstrate how to link a C++ program with the freestanding libc++ we just built. The resulting executable will have no dependence on glibc or any GCC component,\u00a0thus is GNU-free. As the libc++ in use is linked with musl, the C++ program to &hellip; <a href=\"https:\/\/blogs.gentoo.org\/gsoc2016-native-clang\/2016\/05\/06\/build-a-gnu-free-c-program-on-gentoo\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Build a GNU-free C++ program on Gentoo<\/span><\/a><\/p>\n","protected":false},"author":161,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[2],"tags":[],"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/blogs.gentoo.org\/gsoc2016-native-clang\/wp-json\/wp\/v2\/posts\/32"}],"collection":[{"href":"https:\/\/blogs.gentoo.org\/gsoc2016-native-clang\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.gentoo.org\/gsoc2016-native-clang\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.gentoo.org\/gsoc2016-native-clang\/wp-json\/wp\/v2\/users\/161"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.gentoo.org\/gsoc2016-native-clang\/wp-json\/wp\/v2\/comments?post=32"}],"version-history":[{"count":5,"href":"https:\/\/blogs.gentoo.org\/gsoc2016-native-clang\/wp-json\/wp\/v2\/posts\/32\/revisions"}],"predecessor-version":[{"id":44,"href":"https:\/\/blogs.gentoo.org\/gsoc2016-native-clang\/wp-json\/wp\/v2\/posts\/32\/revisions\/44"}],"wp:attachment":[{"href":"https:\/\/blogs.gentoo.org\/gsoc2016-native-clang\/wp-json\/wp\/v2\/media?parent=32"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.gentoo.org\/gsoc2016-native-clang\/wp-json\/wp\/v2\/categories?post=32"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.gentoo.org\/gsoc2016-native-clang\/wp-json\/wp\/v2\/tags?post=32"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}