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"
)