How to proprly link cling into my executable with cmake?

Hey Guys, Sorry for polluting this thread with endless posts (I just can’t find what i need to know :frowning: )

I’ve got cling to compile, and now am trying to figure out how to link it with a test program

If i try linking all of the archive files that were produced from the compilation (llvm, clang, cling) I get 1000 lines of undefinded references. If i include nothing, I just get a couple of undefined references from cling, but if i include libclingInterpreter.a, and libclingMetaProcessor.a as libraries, I also get a boatload of undefined references frm clang and llvm

the closest thing i found that seems to be relevent is llvm.org/docs/CMake.html#embedding.html

However, I’m not quite sure where cling comes into the equation there

Could anybody send me a link or explain to me the procedure of properly linking cling [with cmake]?

Thanks in advance
and sorry if this thread is redundant

Hi,

You’ll need to link cling, clang and llvm, in that order. There are even dependencies within each of these project that define the correct link order, e.g. for cling you want to link UserInterface, MetaProcessor and then Interpreter.

Have a look at what’s happening in tools/driver. This is where the cling binary gets linked, it should show you how to get this done.

Cheers, Axel.

Hey Axel,

So far, I’m not having that much dice, however, in hopes of reducing linking time I wanted to use shared objects

cmake ../src/ -DCMAKE_INSTALL_PREFIX:PATH=/home/d4nf/cling/prefix/ -DBUILD_SHARED_LIBS=ON

it compiled with no problems with clang as the target, however for cling, the story wasn’t so simple: bpaste.net/show/64a272df5f89

Would you have any idea what’s causing this problem?

I’ll post my solution once i find it. at least I know now that my problem isn’t a N! permutations :stuck_out_tongue:

Thanks

Hi,

yeah sorry I don’t think we ever tried that… Looks like you’re missing most of the dependencies here.

We only ever do this from static libs, building either a binary or one single shared lib out of all the static libs needed to run cling.

Cheers, Axel.

ok I managed to do it here’s my CMakeLists.txt file

readers be aware that my cling install looks like the following

the cling source is in /home/d4nf/cling/src
my out of source build is in /home/d4nf/cling/build
my install prefix directory is in /home/d4nf/cling/prefix

consider this code to use the boost license

    cmake_minimum_required(VERSION 3.0)

set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")

set (CLING_DIR /home/d4nf/cling/prefix)
set (CLING_MODULES_DIR ${CLING_DIR}/share/llvm/cmake)
message (${CLING_MODULES_DIR})
set(CMAKE_PREFIX_PATH ${CMAKE_PREFIX_PATH} ${CLING_MODULES_DIR})
set(CMAKE_MODULE_PATH "${CMAKE_MODULE_PATH} ${CLING_MODULES_DIR}") # use llvm's cmake scripts to load in the libraries
set(CLING_LIB_DIR ${CLING_DIR}/lib)
set(CLING_INCLUDE_DIR ${CLING_DIR}/include)
find_package(LLVM  CONFIG REQUIRED )
add_definitions(${LLVM_DEFINITIONS})


set (CLING_LIBRARIES
# Readers despair; the order of these files -matter-
${CLING_LIB_DIR}/libclingMetaProcessor.a
${CLING_LIB_DIR}/libclingInterpreter.a
${CLING_LIB_DIR}/libclingUtils.a

${CLING_LIB_DIR}/libclangFrontend.a
${CLING_LIB_DIR}/libclangCodeGen.a # this and the above 3 cling .a's are necessary if the rest of llvm was compiled as .so's
                                   # you'd need to make a seperate copy of the cling source, remove the tools/cling
                                   #directory, and do a make install with
${CLING_LIB_DIR}/libclangParse.a
${CLING_LIB_DIR}/libclangSema.a
${CLING_LIB_DIR}/libclangBasic.a
${CLING_LIB_DIR}/libclangSerialization.a
${CLING_LIB_DIR}/libclangDriver.a
${CLING_LIB_DIR}/libclangEdit.a
${CLING_LIB_DIR}/libclangLex.a
${CLING_LIB_DIR}/libclangTooling.a
${CLING_LIB_DIR}/libclangAnalysis.a
${CLING_LIB_DIR}/libclangAST.a


${LLVM_AVAILABLE_LIBS}

)
include_directories(${CLING_INCLUDE_DIR})
add_executable(tcling main.cpp)
target_link_libraries(tcling ${CLING_LIBRARIES})

Hi everyone,

I am also trying to generate libCling.so
At the moment I end-up building root and removing everything but libCling.so and the 3 files it depends on.
Is there a way to get the shared library from a pure cling build?

Thanks for your help!
Cheers,
Hugues

Hi Hugues,

Correct, for stand-alone cling we don’t build a shared lib. You should be able to build it yourself, in your project, by simply linking the .a archives together into a .so. Would that be okay?

Cheers, Axel.

Thanks Axel for the hint.

I am certainly not a linking master although I did try my best.
Commands like:

gcc -shared -o libCling.so -Wl,--whole-archive \
libclingMetaProcessor.a \
libclingInterpreter.a \
-Wl,--no-whole-archive \
libclingUtils.a \
libclangFrontend.a \
libclangCodeGen.a \
libclangParse.a \
libclangSema.a \
libclangBasic.a \
libclangSerialization.a \
libclangDriver.a \
libclangEdit.a \
libclangLex.a \
libclangAnalysis.a \
libclangAST.a \
`ls libLLVM*.a`

generate a sizeable file of 80megs but running ldd on them return all kinds of symbol unresolved error.

Let me know if I am missing something obvious.

If it is not a mainstream scenario enough I am ok sticking to building root6 and getting libCling.so from there.

Thanks again!
Hugues

Hi,

  • I’ll add a target to the cling build system. No time scale, though… so I’d welcome a patch!
  • Please use llvm-config for linking the llvm libraries. You can have a look at what happens when building libCling.so in ROOT.

Cheers. Axel.

Thanks Axel and thanks for the hints.

I did try my best at figuring out how libCling.so is built in root6.
It looks like it is using root’s core/meta module here: github.com/root-mirror/root/blo … ule.mk#L48
and relies on github.com/root-mirror/root/blo … makelib.sh

At that point I was not able to connect more dots. I still have the energy to try more things.
I have a root6 debian docker image and a cling debian image with everything in place for those experiments. All ready to be toyed with on aws-ec2 with a large instance to compile quickly.

My motivation is eventually to get it running on Alpine Linux for a much smaller footprint. I have the llvm patches ready. I can get the cling executable when building cling.
I tried to build libCling.so via root6 on AlpineLinux but root6 need some patches.

Cheers,
Hugues

FYI, while implementing the necessary build system changes to build libcling.so I ran into an issue with llvm’s build system. I’m waiting for them to merge my patch upstream.

Hi,

Update: we have libcling.so now.

Axel.