[cfe-users] Using llvm-ar & llvm-ranlib vs ar & ranlib for creating static libraries when thinLTO is enabled.

Teresa Johnson via cfe-users cfe-users at lists.llvm.org
Mon Aug 13 08:46:49 PDT 2018


That's correct, we recommend use of the llvm tools for working with
bitcode. Note, however, that the GNU ar and ranlib do accept a plugin via
--plugin, so you could try using those with the LLVM gold plugin to work
with bitcode. I personally use the llvm-ar/llvm-ranlib however.
Teresa

On Mon, Aug 13, 2018 at 8:38 AM David Blaikie <dblaikie at gmail.com> wrote:

> (Teresa, perhaps you can correct me if I'm wrong here)
>
> On Thu, Aug 9, 2018 at 2:21 PM Mateusz Zych via cfe-users <
> cfe-users at lists.llvm.org> wrote:
>
>> Hi :)
>>
>> I am trying to compile simple project consisting of an executable "app"
>> and static library "bar".
>> Note that I'm enabling thinLTO for both "app" and "bar".
>>
>> ////////////////////////////////////////
>> //               bar.h                //
>> ////////////////////////////////////////
>> #ifndef BAR
>> #define BAR
>>
>> void bar ();
>>
>> #endif
>> ////////////////////////////////////////
>>
>> ////////////////////////////////////////
>> //              bar.cpp               //
>> ////////////////////////////////////////
>> #include "bar.h"
>> #include <iostream>
>>
>> void bar ()
>> {
>>     std::cout << "Bar" << std::endl;
>> }
>> ////////////////////////////////////////
>>
>> ////////////////////////////////////////
>> //              app.cpp               //
>> ////////////////////////////////////////
>> #include "bar.h"
>> #include <iostream>
>>
>> int main()
>> {
>> std::cout << "App" << std::endl;
>> bar();
>> }
>> ////////////////////////////////////////
>>
>>
>> When I'm using llvm-ar and llvm-ranlib to create static library
>> everything works fine:
>>
>> $ clang++ -flto=thin -c bar.cpp -o bar.o
>> $ llvm-ar cr bar.a bar.o
>> $ llvm-ranlib bar.a
>> $ clang++ -flto=thin -c app.cpp -o app.o
>> $ clang++ -flto=thin -o app app.o bar.a
>>
>>
>> However using default ar and ranlib provided by my GNU/Linux distribution
>> results in linking failure:
>>
>> $ clang++ -flto=thin -c bar.cpp -o bar.o
>> $ ar cr bar.a bar.o
>> $ ranlib bar.a
>> $ clang++ -flto=thin -c app.cpp -o app.o
>> $ clang++ -flto=thin -o app app.o bar.a
>> bar.a: *error adding symbols: Archive has no index; run ranlib to add
>> one*
>> clang: error: linker command failed with exit code 1 (use -v to see
>> invocation)
>>
>>
>> Note that for GCC using default ar and ranlib works fine:
>>
>> $ g++ -flto -c bar.cpp -o bar.o
>> $ ar cr bar.a bar.o
>> $ ranlib bar.a
>> $ g++ -flto -c app.cpp -o app.o
>> $ g++ -flto -o app app.o bar.a
>>
>>
>> Obviously using gcc-ar and gcc-ranlib also works fine:
>>
>> $ g++ -flto -c bar.cpp -o bar.o
>> $ gcc-ar cr bar.a bar.o
>> $ gcc-ranlib bar.a
>> $ g++ -flto -c app.cpp -o app.o
>> $ g++ -flto -o app app.o bar.a
>>
>>
>> My question is: *Can I use ar and ranlib provided by GNU/Linux with
>> clang++?*
>>
>
> Not when using LTO, so far as I know. Otherwise, yes. (no idea if
> llvm-ar/llvm-ranlib are totally general purpose replacements for those
> tools, but I guess they are roughly/generally so)
>
>
>>
>> If not, then to properly setup clang as a default compiler on GNU/Linux
>> machine,
>> I should not only run update-alternatives for cc and c++ commands,
>> but also somehow do that for ar and ranlib commands?
>>
>> $ sudo update-alternatives --config cc
>> $ sudo update-alternatives --config c++
>>
>>
>> That's very problematic since update-alternatives doesn't work with ar
>> and ranlib:
>>
>> $ sudo update-alternatives --config ar
>> update-alternatives: error: no alternatives for ar
>> $ sudo update-alternatives --config ranlib
>> update-alternatives: error: no alternatives for ranlib
>>
>>
> Looks like you can teach update-alternatives to support new commands with
> the --install option?
>
>
>>
>> If default ar and ranlib is expected to not work with clang++,
>> then how should I setup Clang as a default compiler on GNU/Linux?
>>
>> Thanks, Mateusz Zych
>> _______________________________________________
>> cfe-users mailing list
>> cfe-users at lists.llvm.org
>> http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-users
>>
>

-- 
Teresa Johnson |  Software Engineer |  tejohnson at google.com |  408-460-2413
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-users/attachments/20180813/bfa45f28/attachment.html>


More information about the cfe-users mailing list