Statically linking llvm into a library, results in unresolved externals.

I’m not really experienced with cmake, but the last few days I tried to figure out, why I can’t build a static library which contains llvm.

I’m trying to build this on windows.

I’ve created a subdirectory, where I link the library project and build a executable, this executable works fine. But as soon as I try to create a separate project, where I link the library I get a lot of “unresolved external symbol” errors.

I don’t really understand why, it works for the internal project but not for the external one and what I can do, so that the symbols will be exported with the library. Especially since the missing symbols seams to be llvm symbols, which I don’t even use directly in my code.

This is my library Cmakelist.txt file:

cmake_minimum_required(VERSION 3.26)

set(namespace "my_lib")
project("MyLibrary"
        VERSION 0.0.1
)

# Create the compile_commands.json (used so the lsp knows which files to use)
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
set(CMAKE_CXX_STANDARD 23)

set(MY_PUBLIC_HEADERS
        include/my_lib/Run.h
)

set(MY_SOURCE_FILES
        lib/my_lib/Run.cpp
)

# LLVM
set(Configuration Release)
find_package(LLVM 17 REQUIRED CONFIG)
include_directories(${LLVM_INCLUDE_DIRS})
separate_arguments(LLVM_DEFINITIONS_LIST NATIVE_COMMAND ${LLVM_DEFINITIONS})
add_definitions(${LLVM_DEFINITIONS_LIST})

add_library(${PROJECT_NAME} STATIC)

target_sources(${PROJECT_NAME}
        PRIVATE
        ${MY_SOURCE_FILES}
        ${MY_PUBLIC_HEADERS}
)

target_include_directories(${PROJECT_NAME}
        PRIVATE
        ${CMAKE_CURRENT_SOURCE_DIR}/lib
        PUBLIC
        $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
        $<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>
)

llvm_map_components_to_libnames(
        llvm_libs
        ${LLVM_TARGETS_TO_BUILD}
        orcjit
        support
        core
        irreader
        codegen
        mc
        mcparser
        option
)

target_link_libraries(${PROJECT_NAME}
        PRIVATE ${llvm_libs} # Not sure if this should be private or public or idk
)

add_subdirectory(executable)

This is the internal executable CMakeLists.txt file:

set(MY_LIB_BINARY_TARGET_NAME "${PROJECT_NAME}Executable")

add_executable(${MY_LIB_BINARY_TARGET_NAME} main.cpp)

target_link_libraries(${MY_LIB_BINARY_TARGET_NAME}
        PRIVATE ${PROJECT_NAME}
)

This is the external executable CMakeLists.txt file:

cmake_minimum_required(VERSION 3.26)
project(test_my_library)

set(CMAKE_CXX_STANDARD 23)

add_executable(test_my_library main.cpp)

target_include_directories(test_my_library PRIVATE ".../include")

target_link_libraries(test_my_library ".../MyLibrary.lib")

Then I got the public Run.h file:

#ifndef MY_LIB_RUN_H
#define MY_LIB_RUN_H

#include "string"

namespace my_library {
    void run(std::string moduleName, std::string filePath) ;
}

#endif //MY_LIB_RUN_H

and the Run.cpp:

#include <fstream>
#include <filesystem>

#include "llvm/Support/TargetSelect.h"
#include "llvm/Support/raw_ostream.h"
#include "llvm/ExecutionEngine/Orc/ThreadSafeModule.h"

#include "jit/Jit.h"

namespace my_library {
    void run(std::string moduleName, std::string filePath) {
        std::ifstream stream;
        stream.open(filePath);

        // Parse file and so on

        llvm::InitializeNativeTarget();
        llvm::InitializeNativeTargetAsmPrinter();
        llvm::InitializeNativeTargetAsmParser();

        auto jit = cantFail(JIT::Create(), "Error occurred while initializing JIT");
        current_module->setDataLayout(jit->getDataLayout());
        auto resourceTracker = jit->getMainJITDylib().createResourceTracker();
        auto threadSafeModule = llvm::orc::ThreadSafeModule(std::move(current_module),
                                                            std::move(context));
        cantFail(jit->addModule(std::move(threadSafeModule), resourceTracker), "Failed to add module");

        auto mainLookup = jit->lookup("main");
        if (auto err = mainLookup.takeError()) {
            llvm::logAllUnhandledErrors(std::move(err), llvm::errs(), "[JIT Error] ");
            return;
        }

        auto *main = (int (*)()) mainLookup->getAddress().getValue();
        int result = main();

        cantFail(resourceTracker->remove(), "Error removing resource tracker");
    }
}

The errors I get are:

MyLibrary.lib(Run.cpp.obj) : error LNK2019: unresolved external symbol LLVMInitializeX86TargetInfo referenced in function "bool __cdecl llvm::InitializeNativeTarget(void)" (?InitializeNativeTarget@llvm@@YA_NXZ)
MyLibrary.lib(Run.cpp.obj) : error LNK2019: unresolved external symbol LLVMInitializeX86Target referenced in function "bool __cdecl llvm::InitializeNativeTarget(void)" (?InitializeNativeTarget@llvm@@YA_NXZ)
MyLibrary.lib(Run.cpp.obj) : error LNK2019: unresolved external symbol LLVMInitializeX86TargetMC referenced in function "bool __cdecl llvm::InitializeNativeTarget(void)" (?InitializeNativeTarget@llvm@@YA_NXZ)
MyLibrary.lib(Run.cpp.obj) : error LNK2019: unresolved external symbol LLVMInitializeX86AsmPrinter referenced in function "bool __cdecl llvm::InitializeNativeTargetAsmPrinter(void)" (?InitializeNativeTargetAsmPrinter@llvm@@YA_NXZ)
MyLibrary.lib(Run.cpp.obj) : error LNK2019: unresolved external symbol LLVMInitializeX86AsmParser referenced in function "bool __cdecl llvm::InitializeNativeTargetAsmParser(void)" (?InitializeNativeTargetAsmParser@llvm@@YA_NXZ)
MyLibrary.lib(Run.cpp.obj) : error LNK2019: unresolved external symbol "protected: void __cdecl llvm::SmallVectorBase<unsigned int>::grow_pod(void *,unsigned __int64,unsigned __int64)" (?grow_pod@?$SmallVectorBase@I@llvm@@IEAAXPEAX_K1@Z) referenced in function "public: class llvm::SmallVector<unsigned int,8> & __cdecl llvm::SmallVector<unsigned int,8>::operator=(class llvm::SmallVector<unsigned int,8> const &)" (??4?$SmallVector@I$07@llvm@@QEAAAEAV01@AEBV01@@Z)
MyLibrary.lib(Run.cpp.obj) : error LNK2019: unresolved external symbol "protected: void __cdecl llvm::SmallVectorBase<unsigned __int64>::grow_pod(void *,unsigned __int64,unsigned __int64)" (?grow_pod@?$SmallVectorBase@_K@llvm@@IEAAXPEAX_K1@Z) referenced in function "public: class llvm::SmallVector<unsigned char,8> & __cdecl llvm::SmallVector<unsigned char,8>::operator=(class llvm::SmallVector<unsigned char,8> const &)" (??4?$SmallVector@E$07@llvm@@QEAAAEAV01@AEBV01@@Z)
MyLibrary.lib(Run.cpp.obj) : error LNK2019: unresolved external symbol "public: virtual __cdecl llvm::raw_ostream::~raw_ostream(void)" (??1raw_ostream@llvm@@UEAA@XZ) referenced in function "int `public: __cdecl llvm::raw_string_ostream::raw_string_ostream(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > &)'::`1'::dtor$0" (?dtor$0@?0???0raw_string_ostream@llvm@@QEAA@AEAV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@Z@4HA)
MyLibrary.lib(Run.cpp.obj) : error LNK2001: unresolved external symbol "public: virtual class llvm::raw_ostream & __cdecl llvm::raw_ostream::changeColor(enum llvm::raw_ostream::Colors,bool,bool)" (?changeColor@raw_ostream@llvm@@UEAAAEAV12@W4Colors@12@_N1@Z)
MyLibrary.lib(Run.cpp.obj) : error LNK2001: unresolved external symbol "public: virtual class llvm::raw_ostream & __cdecl llvm::raw_ostream::resetColor(void)" (?resetColor@raw_ostream@llvm@@UEAAAEAV12@XZ)
MyLibrary.lib(Run.cpp.obj) : error LNK2001: unresolved external symbol "public: virtual class llvm::raw_ostream & __cdecl llvm::raw_ostream::reverseColor(void)" (?reverseColor@raw_ostream@llvm@@UEAAAEAV12@XZ)
MyLibrary.lib(Run.cpp.obj) : error LNK2001: unresolved external symbol "protected: virtual unsigned __int64 __cdecl llvm::raw_ostream::preferred_buffer_size(void)const " (?preferred_buffer_size@raw_ostream@llvm@@MEBA_KXZ)
MyLibrary.lib(Run.cpp.obj) : error LNK2019: unresolved external symbol "private: void __cdecl llvm::raw_ostream::SetBufferAndMode(char *,unsigned __int64,enum llvm::raw_ostream::BufferKind)" (?SetBufferAndMode@raw_ostream@llvm@@AEAAXPEAD_KW4BufferKind@12@@Z) referenced in function "public: __cdecl llvm::raw_string_ostream::raw_string_ostream(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > &)" (??0raw_string_ostream@llvm@@QEAA@AEAV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@Z)
MyLibrary.lib(Run.cpp.obj) : error LNK2019: unresolved external symbol "private: void __cdecl llvm::raw_ostream::flush_nonempty(void)" (?flush_nonempty@raw_ostream@llvm@@AEAAXXZ) referenced in function "public: void __cdecl llvm::raw_ostream::SetUnbuffered(void)" (?SetUnbuffered@raw_ostream@llvm@@QEAAXXZ)
MyLibrary.lib(Run.cpp.obj) : error LNK2001: unresolved external symbol "private: virtual void __cdecl llvm::raw_ostream::anchor(void)" (?anchor@raw_ostream@llvm@@EEAAXXZ)
MyLibrary.lib(Run.cpp.obj) : error LNK2019: unresolved external symbol "class llvm::raw_fd_ostream & __cdecl llvm::errs(void)" (?errs@llvm@@YAAEAVraw_fd_ostream@1@XZ) referenced in function "void __cdecl my_library::run(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >)" (?run@my_library@@YAXV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@0@Z)
MyLibrary.lib(Run.cpp.obj) : error LNK2001: unresolved external symbol "private: virtual void __cdecl llvm::raw_string_ostream::write_impl(char const *,unsigned __int64)" (?write_impl@raw_string_ostream@llvm@@EEAAXPEBD_K@Z)
MyLibrary.lib(Run.cpp.obj) : error LNK2019: unresolved external symbol "public: __cdecl llvm::LLVMContext::~LLVMContext(void)" (??1LLVMContext@llvm@@QEAA@XZ) referenced in function "void __cdecl std::_Destroy_in_place<struct llvm::orc::ThreadSafeContext::State>(struct llvm::orc::ThreadSafeContext::State &)" (??$_Destroy_in_place@UState@ThreadSafeContext@orc@llvm@@@std@@YAXAEAUState@ThreadSafeContext@orc@llvm@@@Z)
MyLibrary.lib(Run.cpp.obj) : error LNK2019: unresolved external symbol "void __cdecl llvm::deallocate_buffer(void *,unsigned __int64,unsigned __int64)" (?deallocate_buffer@llvm@@YAXPEAX_K1@Z) referenced in function "protected: __cdecl llvm::detail::UniqueFunctionBase<class std::unique_ptr<class llvm::RuntimeDyld::MemoryManager,struct std::default_delete<class llvm::RuntimeDyld::MemoryManager> > >::~UniqueFunctionBase<class std::unique_ptr<class llvm::RuntimeDyld::MemoryManager,struct std::default_delete<class llvm::RuntimeDyld::MemoryManager> > >(void)" (??1?$UniqueFunctionBase@V?$unique_ptr@VMemoryManager@RuntimeDyld@llvm@@U?$default_delete@VMemoryManager@RuntimeDyld@llvm@@@std@@@std@@$$V@detail@llvm@@IEAA@XZ)
MyLibrary.lib(Run.cpp.obj) : error LNK2019: unresolved external symbol "private: void __cdecl llvm::DataLayout::clear(void)" (?clear@DataLayout@llvm@@AEAAXXZ) referenced in function "public: class llvm::DataLayout & __cdecl llvm::DataLayout::operator=(class llvm::DataLayout const &)" (??4DataLayout@llvm@@QEAAAEAV01@AEBV01@@Z)
MyLibrary.lib(Run.cpp.obj) : error LNK2019: unresolved external symbol "public: __cdecl llvm::DataLayout::~DataLayout(void)" (??1DataLayout@llvm@@QEAA@XZ) referenced in function "class std::unique_ptr<class my_library::JIT,struct std::default_delete<class my_library::JIT> > __cdecl llvm::cantFail<class std::unique_ptr<class my_library::JIT,struct std::default_delete<class my_library::JIT> > >(class llvm::Expected<class std::unique_ptr<class my_library::JIT,struct std::default_delete<class my_library::JIT> > >,char const *)" (??$cantFail@V?$unique_ptr@VJIT@my_library@@U?$default_delete@VJIT@my_library@@@std@@@std@@@llvm@@YA?AV?$unique_ptr@VJIT@my_library@@U?$default_delete@VJIT@my_library@@@std@@@std@@V?$Expected@V?$unique_ptr@VJIT@my_library@@U?$default_delete@VJIT@my_library@@@std@@@std@@@0@PEBD@Z)
MyLibrary.lib(Run.cpp.obj) : error LNK2019: unresolved external symbol "public: __cdecl llvm::Module::~Module(void)" (??1Module@llvm@@QEAA@XZ) referenced in function "public: __cdecl llvm::orc::ThreadSafeModule::ThreadSafeModule(class std::unique_ptr<class llvm::Module,struct std::default_delete<class llvm::Module> >,class std::unique_ptr<class llvm::LLVMContext,struct std::default_delete<class llvm::LLVMContext> >)" (??0ThreadSafeModule@orc@llvm@@QEAA@V?$unique_ptr@VModule@llvm@@U?$default_delete@VModule@llvm@@@std@@@std@@V?$unique_ptr@VLLVMContext@llvm@@U?$default_delete@VLLVMContext@llvm@@@std@@@4@@Z)
MyLibrary.lib(Run.cpp.obj) : error LNK2019: unresolved external symbol "public: void __cdecl llvm::Module::setDataLayout(class llvm::DataLayout const &)" (?setDataLayout@Module@llvm@@QEAAXAEBVDataLayout@2@@Z) referenced in function "void __cdecl my_library::run(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >)" (?run@my_library@@YAXV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@0@Z)
MyLibrary.lib(Run.cpp.obj) : error LNK2019: unresolved external symbol "public: void __cdecl llvm::Module::print(class llvm::raw_ostream &,class llvm::AssemblyAnnotationWriter *,bool,bool)const " (?print@Module@llvm@@QEBAXAEAVraw_ostream@2@PEAVAssemblyAnnotationWriter@2@_N2@Z) referenced in function "class llvm::raw_ostream & __cdecl llvm::operator<<(class llvm::raw_ostream &,class llvm::Module const &)" (??6llvm@@YAAEAVraw_ostream@0@AEAV10@AEBVModule@0@@Z)
MyLibrary.lib(Run.cpp.obj) : error LNK2019: unresolved external symbol "void __cdecl llvm::logAllUnhandledErrors(class llvm::Error,class llvm::raw_ostream &,class llvm::Twine)" (?logAllUnhandledErrors@llvm@@YAXVError@1@AEAVraw_ostream@1@VTwine@1@@Z) referenced in function "void __cdecl my_library::run(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >)" (?run@my_library@@YAXV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@0@Z)
MyLibrary.lib(Run.cpp.obj) : error LNK2019: unresolved external symbol "public: static class llvm::Expected<class std::unique_ptr<class llvm::orc::SelfExecutorProcessControl,struct std::default_delete<class llvm::orc::SelfExecutorProcessControl> > > __cdecl llvm::orc::SelfExecutorProcessControl::Create(class std::shared_ptr<class llvm::orc::SymbolStringPool>,class std::unique_ptr<class llvm::orc::TaskDispatcher,struct std::default_delete<class llvm::orc::TaskDispatcher> >,class std::unique_ptr<class llvm::jitlink::JITLinkMemoryManager,struct std::default_delete<class llvm::jitlink::JITLinkMemoryManager> >)" (?Create@SelfExecutorProcessControl@orc@llvm@@SA?AV?$Expected@V?$unique_ptr@VSelfExecutorProcessControl@orc@llvm@@U?$default_delete@VSelfExecutorProcessControl@orc@llvm@@@std@@@std@@@3@V?$shared_ptr@VSymbolStringPool@orc@llvm@@@std@@V?$unique_ptr@VTaskDispatcher@orc@llvm@@U?$default_delete@VTaskDispatcher@orc@llvm@@@std@@@6@V?$unique_ptr@VJITLinkMemoryManager@jitlink@llvm@@U?$default_delete@VJITLinkMemoryManager@jitlink@llvm@@@std@@@6@@Z) referenced in function "public: static class llvm::Expected<class std::unique_ptr<class my_library::JIT,struct std::default_delete<class my_library::JIT> > > __cdecl my_library::JIT::Create(void)" (?Create@JIT@my_library@@SA?AV?$Expected@V?$unique_ptr@VJIT@my_library@@U?$default_delete@VJIT@my_library@@@std@@@std@@@llvm@@XZ)
MyLibrary.lib(Run.cpp.obj) : error LNK2019: unresolved external symbol "public: __cdecl llvm::orc::ResourceTracker::~ResourceTracker(void)" (??1ResourceTracker@orc@llvm@@QEAA@XZ) referenced in function "public: __cdecl llvm::IntrusiveRefCntPtr<class llvm::orc::ResourceTracker>::~IntrusiveRefCntPtr<class llvm::orc::ResourceTracker>(void)" (??1?$IntrusiveRefCntPtr@VResourceTracker@orc@llvm@@@llvm@@QEAA@XZ)
MyLibrary.lib(Run.cpp.obj) : error LNK2019: unresolved external symbol "public: class llvm::Error __cdecl llvm::orc::ResourceTracker::remove(void)" (?remove@ResourceTracker@orc@llvm@@QEAA?AVError@3@XZ) referenced in function "void __cdecl my_library::run(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >)" (?run@my_library@@YAXV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@0@Z)
MyLibrary.lib(Run.cpp.obj) : error LNK2019: unresolved external symbol "public: class llvm::IntrusiveRefCntPtr<class llvm::orc::ResourceTracker> __cdecl llvm::orc::JITDylib::getDefaultResourceTracker(void)" (?getDefaultResourceTracker@JITDylib@orc@llvm@@QEAA?AV?$IntrusiveRefCntPtr@VResourceTracker@orc@llvm@@@3@XZ) referenced in function "public: class llvm::Error __cdecl my_library::JIT::addModule(class llvm::orc::ThreadSafeModule,class llvm::IntrusiveRefCntPtr<class llvm::orc::ResourceTracker>)" (?addModule@JIT@my_library@@QEAA?AVError@llvm@@VThreadSafeModule@orc@4@V?$IntrusiveRefCntPtr@VResourceTracker@orc@llvm@@@4@@Z)
MyLibrary.lib(Run.cpp.obj) : error LNK2019: unresolved external symbol "public: class llvm::IntrusiveRefCntPtr<class llvm::orc::ResourceTracker> __cdecl llvm::orc::JITDylib::createResourceTracker(void)" (?createResourceTracker@JITDylib@orc@llvm@@QEAA?AV?$IntrusiveRefCntPtr@VResourceTracker@orc@llvm@@@3@XZ) referenced in function "void __cdecl my_library::run(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >)" (?run@my_library@@YAXV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@0@Z)
MyLibrary.lib(Run.cpp.obj) : error LNK2019: unresolved external symbol "public: __cdecl llvm::orc::ExecutionSession::ExecutionSession(class std::unique_ptr<class llvm::orc::ExecutorProcessControl,struct std::default_delete<class llvm::orc::ExecutorProcessControl> >)" (??0ExecutionSession@orc@llvm@@QEAA@V?$unique_ptr@VExecutorProcessControl@orc@llvm@@U?$default_delete@VExecutorProcessControl@orc@llvm@@@std@@@std@@@Z) referenced in function "class std::unique_ptr<class llvm::orc::ExecutionSession,struct std::default_delete<class llvm::orc::ExecutionSession> > __cdecl std::make_unique<class llvm::orc::ExecutionSession,class std::unique_ptr<class llvm::orc::SelfExecutorProcessControl,struct std::default_delete<class llvm::orc::SelfExecutorProcessControl> >,0>(class std::unique_ptr<class llvm::orc::SelfExecutorProcessControl,struct std::default_delete<class llvm::orc::SelfExecutorProcessControl> > &&)" (??$make_unique@VExecutionSession@orc@llvm@@V?$unique_ptr@VSelfExecutorProcessControl@orc@llvm@@U?$default_delete@VSelfExecutorProcessControl@orc@llvm@@@std@@@std@@$0A@@std@@YA?AV?$unique_ptr@VExecutionSession@orc@llvm@@U?$default_delete@VExecutionSession@orc@llvm@@@std@@@0@$$QEAV?$unique_ptr@VSelfExecutorProcessControl@orc@llvm@@U?$default_delete@VSelfExecutorProcessControl@orc@llvm@@@std@@@0@@Z)
MyLibrary.lib(Run.cpp.obj) : error LNK2019: unresolved external symbol "public: __cdecl llvm::orc::ExecutionSession::~ExecutionSession(void)" (??1ExecutionSession@orc@llvm@@QEAA@XZ) referenced in function "class std::unique_ptr<class my_library::JIT,struct std::default_delete<class my_library::JIT> > __cdecl llvm::cantFail<class std::unique_ptr<class my_library::JIT,struct std::default_delete<class my_library::JIT> > >(class llvm::Expected<class std::unique_ptr<class my_library::JIT,struct std::default_delete<class my_library::JIT> > >,char const *)" (??$cantFail@V?$unique_ptr@VJIT@my_library@@U?$default_delete@VJIT@my_library@@@std@@@std@@@llvm@@YA?AV?$unique_ptr@VJIT@my_library@@U?$default_delete@VJIT@my_library@@@std@@@std@@V?$Expected@V?$unique_ptr@VJIT@my_library@@U?$default_delete@VJIT@my_library@@@std@@@std@@@0@PEBD@Z)
MyLibrary.lib(Run.cpp.obj) : error LNK2019: unresolved external symbol "public: class llvm::Error __cdecl llvm::orc::ExecutionSession::endSession(void)" (?endSession@ExecutionSession@orc@llvm@@QEAA?AVError@3@XZ) referenced in function "class std::unique_ptr<class my_library::JIT,struct std::default_delete<class my_library::JIT> > __cdecl llvm::cantFail<class std::unique_ptr<class my_library::JIT,struct std::default_delete<class my_library::JIT> > >(class llvm::Expected<class std::unique_ptr<class my_library::JIT,struct std::default_delete<class my_library::JIT> > >,char const *)" (??$cantFail@V?$unique_ptr@VJIT@my_library@@U?$default_delete@VJIT@my_library@@@std@@@std@@@llvm@@YA?AV?$unique_ptr@VJIT@my_library@@U?$default_delete@VJIT@my_library@@@std@@@std@@V?$Expected@V?$unique_ptr@VJIT@my_library@@U?$default_delete@VJIT@my_library@@@std@@@std@@@0@PEBD@Z)
MyLibrary.lib(Run.cpp.obj) : error LNK2019: unresolved external symbol "public: class llvm::orc::JITDylib & __cdecl llvm::orc::ExecutionSession::createBareJITDylib(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >)" (?createBareJITDylib@ExecutionSession@orc@llvm@@QEAAAEAVJITDylib@23@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@Z) referenced in function "public: __cdecl my_library::JIT::JIT(class std::unique_ptr<class llvm::orc::ExecutionSession,struct std::default_delete<class llvm::orc::ExecutionSession> >,class llvm::orc::JITTargetMachineBuilder,class llvm::DataLayout)" (??0JIT@my_library@@QEAA@V?$unique_ptr@VExecutionSession@orc@llvm@@U?$default_delete@VExecutionSession@orc@llvm@@@std@@@std@@VJITTargetMachineBuilder@orc@llvm@@VDataLayout@6@@Z)
MyLibrary.lib(Run.cpp.obj) : error LNK2019: unresolved external symbol "public: class llvm::Expected<class llvm::orc::ExecutorSymbolDef> __cdecl llvm::orc::ExecutionSession::lookup(class llvm::ArrayRef<class llvm::orc::JITDylib *>,class llvm::StringRef,enum llvm::orc::SymbolState)" (?lookup@ExecutionSession@orc@llvm@@QEAA?AV?$Expected@VExecutorSymbolDef@orc@llvm@@@3@V?$ArrayRef@PEAVJITDylib@orc@llvm@@@3@VStringRef@3@W4SymbolState@23@@Z) referenced in function "public: class llvm::Expected<class llvm::orc::ExecutorSymbolDef> __cdecl my_library::JIT::lookup(class llvm::StringRef)" (?lookup@JIT@my_library@@QEAA?AV?$Expected@VExecutorSymbolDef@orc@llvm@@@llvm@@VStringRef@4@@Z)
MyLibrary.lib(Run.cpp.obj) : error LNK2019: unresolved external symbol "public: __cdecl llvm::orc::MangleAndInterner::MangleAndInterner(class llvm::orc::ExecutionSession &,class llvm::DataLayout const &)" (??0MangleAndInterner@orc@llvm@@QEAA@AEAVExecutionSession@12@AEBVDataLayout@2@@Z) referenced in function "public: __cdecl my_library::JIT::JIT(class std::unique_ptr<class llvm::orc::ExecutionSession,struct std::default_delete<class llvm::orc::ExecutionSession> >,class llvm::orc::JITTargetMachineBuilder,class llvm::DataLayout)" (??0JIT@my_library@@QEAA@V?$unique_ptr@VExecutionSession@orc@llvm@@U?$default_delete@VExecutionSession@orc@llvm@@@std@@@std@@VJITTargetMachineBuilder@orc@llvm@@VDataLayout@6@@Z)
MyLibrary.lib(Run.cpp.obj) : error LNK2019: unresolved external symbol "public: virtual __cdecl llvm::orc::IRLayer::~IRLayer(void)" (??1IRLayer@orc@llvm@@UEAA@XZ) referenced in function "class std::unique_ptr<class my_library::JIT,struct std::default_delete<class my_library::JIT> > __cdecl llvm::cantFail<class std::unique_ptr<class my_library::JIT,struct std::default_delete<class my_library::JIT> > >(class llvm::Expected<class std::unique_ptr<class my_library::JIT,struct std::default_delete<class my_library::JIT> > >,char const *)" (??$cantFail@V?$unique_ptr@VJIT@my_library@@U?$default_delete@VJIT@my_library@@@std@@@std@@@llvm@@YA?AV?$unique_ptr@VJIT@my_library@@U?$default_delete@VJIT@my_library@@@std@@@std@@V?$Expected@V?$unique_ptr@VJIT@my_library@@U?$default_delete@VJIT@my_library@@@std@@@std@@@0@PEBD@Z)
MyLibrary.lib(Run.cpp.obj) : error LNK2019: unresolved external symbol "public: __cdecl llvm::orc::IRCompileLayer::IRCompileLayer(class llvm::orc::ExecutionSession &,class llvm::orc::ObjectLayer &,class std::unique_ptr<class llvm::orc::IRCompileLayer::IRCompiler,struct std::default_delete<class llvm::orc::IRCompileLayer::IRCompiler> >)" (??0IRCompileLayer@orc@llvm@@QEAA@AEAVExecutionSession@12@AEAVObjectLayer@12@V?$unique_ptr@VIRCompiler@IRCompileLayer@orc@llvm@@U?$default_delete@VIRCompiler@IRCompileLayer@orc@llvm@@@std@@@std@@@Z) referenced in function "public: __cdecl my_library::JIT::JIT(class std::unique_ptr<class llvm::orc::ExecutionSession,struct std::default_delete<class llvm::orc::ExecutionSession> >,class llvm::orc::JITTargetMachineBuilder,class llvm::DataLayout)" (??0JIT@my_library@@QEAA@V?$unique_ptr@VExecutionSession@orc@llvm@@U?$default_delete@VExecutionSession@orc@llvm@@@std@@@std@@VJITTargetMachineBuilder@orc@llvm@@VDataLayout@6@@Z)
MyLibrary.lib(Run.cpp.obj) : error LNK2019: unresolved external symbol "public: __cdecl llvm::orc::JITTargetMachineBuilder::JITTargetMachineBuilder(class llvm::Triple)" (??0JITTargetMachineBuilder@orc@llvm@@QEAA@VTriple@2@@Z) referenced in function "public: static class llvm::Expected<class std::unique_ptr<class my_library::JIT,struct std::default_delete<class my_library::JIT> > > __cdecl my_library::JIT::Create(void)" (?Create@JIT@my_library@@SA?AV?$Expected@V?$unique_ptr@VJIT@my_library@@U?$default_delete@VJIT@my_library@@@std@@@std@@@llvm@@XZ)
MyLibrary.lib(Run.cpp.obj) : error LNK2019: unresolved external symbol "public: class llvm::Expected<class std::unique_ptr<class llvm::TargetMachine,struct std::default_delete<class llvm::TargetMachine> > > __cdecl llvm::orc::JITTargetMachineBuilder::createTargetMachine(void)" (?createTargetMachine@JITTargetMachineBuilder@orc@llvm@@QEAA?AV?$Expected@V?$unique_ptr@VTargetMachine@llvm@@U?$default_delete@VTargetMachine@llvm@@@std@@@std@@@3@XZ) referenced in function "public: class llvm::Expected<class llvm::DataLayout> __cdecl llvm::orc::JITTargetMachineBuilder::getDefaultDataLayoutForTarget(void)" (?getDefaultDataLayoutForTarget@JITTargetMachineBuilder@orc@llvm@@QEAA?AV?$Expected@VDataLayout@llvm@@@3@XZ)
MyLibrary.lib(Run.cpp.obj) : error LNK2019: unresolved external symbol "public: __cdecl llvm::orc::ConcurrentIRCompiler::ConcurrentIRCompiler(class llvm::orc::JITTargetMachineBuilder,class llvm::ObjectCache *)" (??0ConcurrentIRCompiler@orc@llvm@@QEAA@VJITTargetMachineBuilder@12@PEAVObjectCache@2@@Z) referenced in function "class std::unique_ptr<class llvm::orc::ConcurrentIRCompiler,struct std::default_delete<class llvm::orc::ConcurrentIRCompiler> > __cdecl std::make_unique<class llvm::orc::ConcurrentIRCompiler,class llvm::orc::JITTargetMachineBuilder,0>(class llvm::orc::JITTargetMachineBuilder &&)" (??$make_unique@VConcurrentIRCompiler@orc@llvm@@VJITTargetMachineBuilder@23@$0A@@std@@YA?AV?$unique_ptr@VConcurrentIRCompiler@orc@llvm@@U?$default_delete@VConcurrentIRCompiler@orc@llvm@@@std@@@0@$$QEAVJITTargetMachineBuilder@orc@llvm@@@Z)
MyLibrary.lib(Run.cpp.obj) : error LNK2019: unresolved external symbol "public: static class llvm::Expected<class std::unique_ptr<class llvm::orc::DynamicLibrarySearchGenerator,struct std::default_delete<class llvm::orc::DynamicLibrarySearchGenerator> > > __cdecl llvm::orc::DynamicLibrarySearchGenerator::Load(char const *,char,class std::function<bool __cdecl(class llvm::orc::SymbolStringPtr const &)>)" (?Load@DynamicLibrarySearchGenerator@orc@llvm@@SA?AV?$Expected@V?$unique_ptr@VDynamicLibrarySearchGenerator@orc@llvm@@U?$default_delete@VDynamicLibrarySearchGenerator@orc@llvm@@@std@@@std@@@3@PEBDDV?$function@$$A6A_NAEBVSymbolStringPtr@orc@llvm@@@Z@std@@@Z) referenced in function "public: __cdecl my_library::JIT::JIT(class std::unique_ptr<class llvm::orc::ExecutionSession,struct std::default_delete<class llvm::orc::ExecutionSession> >,class llvm::orc::JITTargetMachineBuilder,class llvm::DataLayout)" (??0JIT@my_library@@QEAA@V?$unique_ptr@VExecutionSession@orc@llvm@@U?$default_delete@VExecutionSession@orc@llvm@@@std@@@std@@VJITTargetMachineBuilder@orc@llvm@@VDataLayout@6@@Z)
MyLibrary.lib(Run.cpp.obj) : error LNK2019: unresolved external symbol "public: __cdecl llvm::orc::RTDyldObjectLinkingLayer::RTDyldObjectLinkingLayer(class llvm::orc::ExecutionSession &,class llvm::unique_function<class std::unique_ptr<class llvm::RuntimeDyld::MemoryManager,struct std::default_delete<class llvm::RuntimeDyld::MemoryManager> > __cdecl(void)>)" (??0RTDyldObjectLinkingLayer@orc@llvm@@QEAA@AEAVExecutionSession@12@V?$unique_function@$$A6A?AV?$unique_ptr@VMemoryManager@RuntimeDyld@llvm@@U?$default_delete@VMemoryManager@RuntimeDyld@llvm@@@std@@@std@@XZ@2@@Z) referenced in function "public: __cdecl my_library::JIT::JIT(class std::unique_ptr<class llvm::orc::ExecutionSession,struct std::default_delete<class llvm::orc::ExecutionSession> >,class llvm::orc::JITTargetMachineBuilder,class llvm::DataLayout)" (??0JIT@my_library@@QEAA@V?$unique_ptr@VExecutionSession@orc@llvm@@U?$default_delete@VExecutionSession@orc@llvm@@@std@@@std@@VJITTargetMachineBuilder@orc@llvm@@VDataLayout@6@@Z)
MyLibrary.lib(Run.cpp.obj) : error LNK2019: unresolved external symbol "public: virtual __cdecl llvm::orc::RTDyldObjectLinkingLayer::~RTDyldObjectLinkingLayer(void)" (??1RTDyldObjectLinkingLayer@orc@llvm@@UEAA@XZ) referenced in function "class std::unique_ptr<class my_library::JIT,struct std::default_delete<class my_library::JIT> > __cdecl llvm::cantFail<class std::unique_ptr<class my_library::JIT,struct std::default_delete<class my_library::JIT> > >(class llvm::Expected<class std::unique_ptr<class my_library::JIT,struct std::default_delete<class my_library::JIT> > >,char const *)" (??$cantFail@V?$unique_ptr@VJIT@my_library@@U?$default_delete@VJIT@my_library@@@std@@@std@@@llvm@@YA?AV?$unique_ptr@VJIT@my_library@@U?$default_delete@VJIT@my_library@@@std@@@std@@V?$Expected@V?$unique_ptr@VJIT@my_library@@U?$default_delete@VJIT@my_library@@@std@@@std@@@0@PEBD@Z)
MyLibrary.lib(Run.cpp.obj) : error LNK2019: unresolved external symbol "public: __cdecl llvm::SectionMemoryManager::SectionMemoryManager(class llvm::SectionMemoryManager::MemoryMapper *)" (??0SectionMemoryManager@llvm@@QEAA@PEAVMemoryMapper@01@@Z) referenced in function "protected: static class std::unique_ptr<class llvm::RuntimeDyld::MemoryManager,struct std::default_delete<class llvm::RuntimeDyld::MemoryManager> > __cdecl llvm::detail::UniqueFunctionBase<class std::unique_ptr<class llvm::RuntimeDyld::MemoryManager,struct std::default_delete<class llvm::RuntimeDyld::MemoryManager> > >::CallImpl<class `public: __cdecl my_library::JIT::JIT(class std::unique_ptr<class llvm::orc::ExecutionSession,struct std::default_delete<class llvm::orc::ExecutionSession> >,class llvm::orc::JITTargetMachineBuilder,class llvm::DataLayout)'::`1'::<lambda_6_> >(void *)" (??$CallImpl@V<lambda_6_>@?0???0JIT@my_library@@QEAA@V?$unique_ptr@VExecutionSession@orc@llvm@@U?$default_delete@VExecutionSession@orc@llvm@@@std@@@std@@VJITTargetMachineBuilder@orc@llvm@@VDataLayout@8@@Z@@?$UniqueFunctionBase@V?$unique_ptr@VMemoryManager@RuntimeDyld@llvm@@U?$default_delete@VMemoryManager@RuntimeDyld@llvm@@@std@@@std@@$$V@detail@llvm@@KA?AV?$unique_ptr@VMemoryManager@RuntimeDyld@llvm@@U?$default_delete@VMemoryManager@RuntimeDyld@llvm@@@std@@@std@@PEAX@Z)

I get similar errors for some other files as well.

Maybe someone got some pointers, where to start and/or how to solve this issue?
As far as I understand, the .lib file contains some .obj files, which will reference this symbols? Also I would assume that the functions/objects I’m using internally use the symbols missing?
Why are the symbols even missing and how could I tell cmake to include them?

Since you have this:

target_link_libraries(${PROJECT_NAME}
    PRIVATE ${llvm_libs}
)

and your library is STATIC, other projects that are linking to MyLibrary.lib would need to know where these ${llvm_libs} libraries are located, because they are needed for linking too, and so you’d need to find_package() LLVM in those other projects too.

As for PRIVATE scope in target_link_libraries(), that is for public headers propagation, so you are telling here that LLVM’s public headers shouldn’t be available to projects that are linking to your library. Or at least that’s how I understand it.

I tried changing it to PUBLIC and SHARED, but it doesn’t seems to change anything.

Just for clarification, I have some public headers like the Run.h, which itself does not include or reference anything of llvm, only Run.cpp has references to it.

What I don’t understand, or would assume, that I create a interface with the run function in Run.h, so I don’t understand why the information required internally, of the llvm symbols by Run.cpp, is not included in the library? (There probably is a reason for that I guess?)

I also tried to include LLVM in the other project, which then works, but of course I don’t want my library to be dependent on another library. I also use spdlog and some boost libraries, which appear to no have the same issue.

Because you’re linking to the library target as a file, not as a CMake target:

I recommend going through the tutorial. The relevant bits here is called “exporting a target”, covered in Step 11.

Static libraries contain only your code. Consumers of your library must still get the libraries that provide the symbols you use. If they were copied as needed, the diamond problem comes into effect:

  • A uses symbols from C
  • B uses symbols from C

If everything is static and both A and B copy symbols from C, if D wants to use A and B, which copy of the symbols should be “trusted” if there are any conflicts?

Ohh ok, I was following a guide online, to create the Config file, but I assumed, locally for testing it might be easier to just include the library directly.

Thx for the links!

So I found some time to fiddle around, I got it kind of working, where I have created a Configuration File, following a tutorial.

I think there is still some parts missing, because the libraries of llvm are not found in the project consuming my library.

But what I still can’t wrap my head around, I want to create one single library file, which contains everything that is needed, all libraries it requires itself, so I can use it in other projects, without having to install other dependencies. I would have assumed that this is a STATIC library?

the libraries of llvm are not found in the project consuming my library

For this to work:

  • either that project (the one consuming your library) needs to execute find_package(LLVM CONFIG REQUIRED) (assuming that this is the actual LLVM package name and that it is discoverable in CONFIG mode);
  • or your library config should contain find_dependency() macro that would find LLVM package.

I want to create one single library file, which contains everything that is needed

If I got your intention right, this would be only possible in case when your library is SHARED and your scope of linking to your dependencies is PRIVATE. Then consuming project would only need your library package.

You can theoretically make it work with your library being STATIC too, by “cooking” a “super” library archive, which would incorporate all the symbols - both your library and your dependencies, but such an approach usually is not recommended, as far as I understood from reading various sources and manuals.

Here are some more details about the matter. That article also contains an overview of linking scopes and their effects (applying to your question about discovering LLVM in consuming project).

Not a typical STATIC library, no. There is an issue requesting support for such a library type though.