Statically link libclang

I'm trying to statically link libclang in one of my tools [1]. The problem I have is to get the correct order of the LLVM and libclang libraries when linking. On macOS it seems I can pass the libraries to the linker in any order but that doesn't work on Linux.

I've looked at llvm-config, but that doesn't contain any information about libclang, only LLVM. I've tried specifying the order of the libraries exactly as specified in "tools/clang/tools/libclang/CMakeFiles/libclang.dir/link.txt" after invoking cmake and that works.

Is there a way to get/extract the correct order of the libraries programmatically without having to hard code the order? Ideally something that works across different versions of libclang.


I came up with a workaround. Extract all LLVM and Clang libraries by iterating the LLVM lib directory. Pass all libraries prefixed with libLLVM and libclang to the linker. Use --start-group and --end-group to help resolve undefined symbols. These flags will instruct the linker to repeatedly search the libraries until no undefined symbols remain. This is slower but works.