1

I am trying to generate a dynamic(shared) library for a single C file, which uses LibClang (LLVM)

Here's are the imports of that file.

#include <clang-c/Index.h>
#include <stdio.h>
#include <stdlib.h>

I am struggling with building a dynamic library for this file using CMake

Currently, I am building it using clang compiler itself with this command, but I want to use CMake to be able to build it for other platforms with ease

clang -I/usr/lib/llvm-10/include/ -lclang -shared -fpic wrapper.c -o libwrapped_clang.so

Here's my CMakeLists.txt but this gives "undefined function clang_..." errors

cmake_minimum_required(VERSION 3.7 FATAL_ERROR)
project(wrapped_libclang VERSION 1.0.0 LANGUAGES C)

find_package(LLVM REQUIRED CONFIG)
message(STATUS "Found LLVM ${LLVM_PACKAGE_VERSION}")
message(STATUS "Using LLVMConfig.cmake in: ${LLVM_DIR}")
message(STATUS "LLVM_INCLUDE_DIRS: ${LLVM_INCLUDE_DIRS}")

include_directories(${LLVM_INCLUDE_DIRS})
add_definitions(${LLVM_DEFINITIONS})

add_library(wrapped_libclang SHARED wrapper.c)
add_executable(wrapped_clang_test wrapper.c)

llvm_map_components_to_libnames(llvm_libs support core irreader)

# Link against LLVM libraries
target_link_libraries(wrapped_clang_test ${llvm_libs})

set_target_properties(wrapped_libclang PROPERTIES
    PUBLIC_HEADER wrapper.c
    VERSION ${PROJECT_VERSION}
    SOVERSION 1
    OUTPUT_NAME "wrapped_clang"
    XCODE_ATTRIBUTE_CODE_SIGN_IDENTITY "Hex_Identity_ID_Goes_Here"
)

Any help is appreciated :/

====================== EDIT 1 =============================

Tried this, it partially worked, The dynamic library created now has all symbols from my wrapper.c file, but doesn't have any of the functions in libClang

cmake_minimum_required(VERSION 3.7 FATAL_ERROR)
project(wrapped_libclang LANGUAGES C)

find_package(Clang REQUIRED)

# Add path to LLVM modules
set(CMAKE_MODULE_PATH
  ${CMAKE_MODULE_PATH}
  "${LLVM_CMAKE_DIR}"
  )

include(AddLLVM)

include_directories(${LLVM_INCLUDE_DIRS})
include_directories(${CLANG_INCLUDE_DIRS})

add_definitions(${LLVM_DEFINITIONS})
add_definitions(${CLANG_DEFINITIONS})

add_llvm_library(wrapped_libclang SHARED wrapper.c)

set_target_properties(wrapped_libclang PROPERTIES
    PUBLIC_HEADER wrapper.c
    OUTPUT_NAME "wrapped_clang"
    XCODE_ATTRIBUTE_CODE_SIGN_IDENTITY "Hex_Identity_ID_Goes_Here"
)
Prerak Mann
  • 661
  • 7
  • 15
  • Did you see the CMake file in the response to [this question](https://stackoverflow.com/questions/55921707/setting-path-to-clang-library-in-cmake)? It looks like you manually link the Clang library (`-lclang`), but you don't have this in your CMake. – Kevin Jun 10 '20 at 20:35
  • @squareskittles I tried that (EDIT 1), the errors are gone, but my dynamic library still doesn't have symbols from libClang. – Prerak Mann Jun 11 '20 at 05:37
  • Yes, because you are still not *linking* to the Clang library with `target_link_libraries()`, try linking to `Clang::Clang` or `${CLANG_LIBS}`, or simply `clang`. – Kevin Jun 11 '20 at 12:07
  • Adding -> target_link_libraries(wrapped_libclang PRIVATE Clang::Clang) gives target not found |||||| -> target_link_libraries(wrapped_libclang PRIVATE clang-or-add-anything) gives no error but dylib still doesn't have libclang symbols |||||| -> target_link_libraries(wrapped_libclang clang) The keyword signature for target_link_libraries has already been used with the target "wrapped_libclang". All uses of target_link_libraries with a target must be either all-keyword or all-plain. – Prerak Mann Jun 11 '20 at 18:11

0 Answers0