How to use dynamic library!

I’m learning something about LTO, is mentioned in the following article,but when I use ld.lld and,all of them do not use, I want know where is this dynamic library used.

command and code are as follows:

// use
clang a.c -flto -c
clang b.c -flto -c
clang -flto a.o b.o -o main -fuse-ld=gold
// use ld.lld
clang a.c -flto -c
clang b.c -flto -c
clang -flto a.o b.o -o main -fuse-ld=lld

// ----- a.c ---
#include <stdio.h>

extern void foo1(void);
extern void foo4(void);

void foo2(void) {

void foo3(void) {

int main(void) {

// --- b.c ---
#include <stdio.h>

extern void foo2(void);

void foo1(void) {

void foo4(void) {

As noted, provides a C interface to the LTO implementation. This is used by some out of tree linkers, but not by the LLVM gold plugin or by lld, which both link directly to the native C++ LTO API. That interface is defined in (in particular the add() and run() methods). The C API is much more limited and legacy at this point.

@pcc probably this documentation needs to be updated a bit - is there any documentation on the new C++ API? I’m not aware of any but want to confirm.

@hippyll123 while the general model of LTO compilation on that documentation page is correct, I would look at the header pointed to above and its use in lld or gold plugin for examples on the actual interface. E.g.:
For lld: and
For gold-plugin:

1 Like


Hi teresa,
Thanks for the explanation. But I am looking for some more info. I am new LTO world.
What is the job of plugin? Does it act as a “bridge” between gold linker and LLM world?

The gold linker has a plugin model for interfacing with compiler LTO support. On the LLVM side there is a gold-plugin.cpp (see the link in my prior response) that implements the various plugin handlers invoked by gold. It then interfaces with the LLVM LTO API functions. See also The LLVM gold plugin — LLVM 18.0.0git documentation to understand how to set up gold to interface with the LLVM gold plugin.

However, if you are not tied to gold, I would recommend using lld which is part of LLVM and interfaces directly with the LLVM LTO API. It is simpler to set up and use simply because you can configure your LLVM project to build it alongside everything else. See also


Understood. Thanks.