[cfe-dev] How to build shared and static libclang* files

Wink Saville via cfe-dev cfe-dev at lists.llvm.org
Tue May 7 17:56:37 PDT 2019


So I've got a proof of concept for building both SHARED and STATIC libraries.
The simple implementation that creates libclangARCMigrate.a and
libclangARCMigrate.so
is on my llvm-project fork:
https://github.com/winksaville/llvm-project/commit/fb314421c1dd404648e7046c6ef074e93d28e4c1

As it turns out llvm_add_library already supported building both, but
I had to have it build the shared library first rather than the static library.
Without this change clang can be built but it fails when run:

  $ ./bin/clang-9 --version
  : CommandLine Error: Option 'use-dbg-addr' registered more than once!
  LLVM ERROR: inconsistency in registered CommandLine options

The reason it fails is because it was linked with libclangARCMigrate.so.9svn
rahter than the correct library, libclangARCMigrate.a.


As you can see in the link above the code changes were fairly small:

  clang/cmake/modules/AddClang.cmake add_clang_library:

   - Add support for STATIC, SHARED or both
   - Add support for OUTPUT_NAME

  llvm/cmake/modules/AddLLVM.cmake llvm_add_library:

   - Changed so when both SHARED and STATIC are passed SHARED is built
     first. Without this change llvm_add_library causes the shared libraries
     to be linked to clang rather than the static library. This compiles
     successfully but clang fails when run with:

  clang/lib/ARCMigrate/CMakeLists.txt:

   - Added STATIC SHARED so both libclangARCMigrate.a and
     libclangARCMigrate.so are built


The result of building with the change is both static and shared libraries
of libclangARCMigrate are created:

  $ ls -al lib/*ARCM*
  -rw-r--r-- 1 wink users  8541496 May  7 17:37 lib/libclangARCMigrate.a
  lrwxrwxrwx 1 wink users       26 May  7 17:37
lib/libclangARCMigrate.so -> libclangARCMigrate.so.9svn
  -rwxr-xr-x 1 wink users 37597048 May  7 17:37 lib/libclangARCMigrate.so.9svn

And clang runs:

  $ ./bin/clang --version
  clang version 9.0.0 (git at github.com:winksaville/llvm-project
fb314421c1dd404648e7046c6ef074e93d28e4c1)
  Target: x86_64-unknown-linux-gnu
  Thread model: posix
  InstalledDir:
/home/wink/prgs/llvm/llvm-project/build-Support-building-shared-and-static-libraries-simultaneously/./bin


Feed back welcome!

-- Wink



More information about the cfe-dev mailing list