LinuxCommandLibrary

llvm-config

Get LLVM compilation flags and linker options

TLDR

Compile and link an LLVM based program

$ clang++ $(llvm-config --cxxflags --ldflags --libs) --output [path/to/output_executable] [path/to/source.cc]
copy

Print the PREFIX of your LLVM installation
$ llvm-config --prefix
copy

Print all targets supported by your LLVM build
$ llvm-config --targets-built
copy

SYNOPSIS

llvm-config [options]

PARAMETERS

--version
    Prints the LLVM version string (e.g., 17.0.6).

--prefix
    Prints the installation prefix path where LLVM is installed (e.g., /usr/local/llvm).

--src-root
    Prints the LLVM source root path used during compilation, if available.

--bindir
    Prints the directory where LLVM executables (like clang, opt) are installed.

--includedir
    Prints the directory containing LLVM C/C++ header files needed for compilation.

--libdir
    Prints the directory containing LLVM object code libraries.

--cflags
    Prints C compiler flags (e.g., include paths, defines) required to compile against LLVM libraries.

--cxxflags
    Prints C++ compiler flags (e.g., include paths, defines) required to compile against LLVM libraries.

--ldflags
    Prints linker flags (e.g., library search paths, rpath) needed to link against LLVM libraries.

--libs [component ...]
    Prints the libraries (e.g., -lLLVMCore -lLLVMIRReader) needed to link against specified LLVM components. If no components are specified, it typically provides all core libraries.

--libnames [component ...]
    Prints the library names (e.g., LLVMCore LLVMIRReader) without paths or linker flags. Useful for listing components.

--system-libs
    Prints system libraries (e.g., -lz, -lrt) that LLVM depends on for its operation.

--components
    Prints a space-separated list of all available LLVM components (e.g., IRReader, Analysis, PassManager, TransformUtils).

--targets
    Prints a space-separated list of all available LLVM targets (e.g., X86, AArch64, WebAssembly) compiled into this LLVM installation.

--build-mode
    Prints the build mode of the LLVM installation (e.g., Debug, Release, RelWithDebInfo).

--assertion-mode
    Prints whether LLVM was built with assertions enabled (e.g., Assertions, NoAssertions).

--shared-mode
    Indicates whether LLVM was built as shared or static libraries (e.g., shared, static).

--host-target
    Prints the host triple (e.g., x86_64-unknown-linux-gnu) for which LLVM was built.

--build-target
    Prints the target triple used during the LLVM build process, often the same as --host-target.

--link-shared-libs
    Used with --libs to ensure that shared libraries are linked, if LLVM was built with shared libraries.

--native-debug-libs
    Indicates if native debug libraries were included in the LLVM build (e.g., for profiling).

DESCRIPTION

llvm-config is a versatile utility script or executable provided with an LLVM installation. Its primary function is to output various configuration parameters, build information, and paths crucial for interacting with LLVM libraries and tools.

This command is invaluable for build systems like Makefiles or CMake, which need to compile and link applications against LLVM. Instead of hardcoding paths or configuration flags, developers can use llvm-config to dynamically retrieve correct values for CFLAGS, LDFLAGS, library paths, and component lists. This ensures compatibility across different LLVM installations and build environments, simplifying the process of integrating LLVM into larger projects by providing a standardized interface for LLVM's build settings.

CAVEATS

The exact output and available options for llvm-config can vary slightly between different LLVM versions. Users must ensure they are invoking the llvm-config associated with the correct LLVM installation, especially if multiple versions are present on the system (e.g., via the PATH environment variable). It is primarily used for configuring build systems to link against LLVM core libraries, rather than for invoking LLVM tools directly.

TYPICAL USAGE

llvm-config is frequently used within Makefiles or shell scripts to dynamically set compiler and linker flags for applications that use LLVM. For example, a common usage pattern is:
$(CXX) main.cpp `llvm-config --cxxflags --ldflags --libs core --libs irreader` -o my_llvm_tool
This command compiles main.cpp and links it against the LLVM core and IRReader libraries, using the correct flags provided by llvm-config.

PURPOSE FOR DEVELOPERS

For developers, llvm-config significantly simplifies the process of building tools and applications that integrate with LLVM. It abstracts away the specifics of LLVM's complex build system and installation paths, allowing developers to focus on their code rather than on environment configuration. This promotes portability and maintainability of LLVM-dependent projects across different systems and LLVM versions.

HISTORY

llvm-config has been a fundamental part of the LLVM project since its early days, serving as the canonical way for external projects to discover and utilize LLVM's build settings and libraries. Its design is similar to pkg-config, providing a standardized interface for configuration data. It has continuously evolved with LLVM releases, adding new options as the project's complexity and modularity increased, ensuring a dynamic and standardized configuration interface.

SEE ALSO

clang(1), pkg-config(1), cmake(1)

Copied to clipboard