Skip to content

How to link c++ standard library in LLVM's JIT? #81

Closed
@lanistor

Description

@lanistor

I have custom functions written by c++, and they are using lots of c++ standard libraries, such as iostream, map, vector, i can add my functions to JIT using addObjectFile, but the standard libraries' functions cannot be found, how cound i link them with my JIT?

My case:
ffi.cpp

#include <iostream>

extern "C" int add(int a, int b) {
  std::cout << ">>add function run" << std::endl;
  return a + b;
}

main.cpp, the built/ffi is created by clang++ ffi.cpp -c -o build/ffi

int main(int argc, char *argv[]) {

  InitLLVM X(argc, argv);
  InitializeNativeTarget();
  InitializeNativeTargetAsmPrinter();
  ThreadSafeContext context(std::make_unique<LLVMContext>());

  ExitOnError ExitOnErr;

  auto JTMB = ExitOnErr(JITTargetMachineBuilder::detectHost());
  JTMB.setCodeModel(CodeModel::Small);

  auto jit =
      ExitOnErr(LLJITBuilder()
                    .setJITTargetMachineBuilder(std::move(JTMB))
                    .create());

  char ffi_file[] = "build/ffi";
  jit->addObjectFile(ExitOnErr(errorOrToExpected(MemoryBuffer::getFileAsStream(ffi_file))));

The add function can be found, but the functions of iostream cannot be found, and run with error:

JIT session error: Symbols not found: [ __Unwind_Resume, __ZNKSt3__16locale9use_facetERNS0_2idE, __ZNKSt3__18ios_base6getlocEv, __ZNSt3__112basic_stringIcNS_11char_traitsIcEENS_9allocatorIcEEE6__initEmc, __ZNSt3__112basic_stringIcNS_11char_traitsIcEENS_9allocatorIcEEED1Ev, __ZNSt3__113basic_ostreamIcNS_11char_traitsIcEEE3putEc, __ZNSt3__113basic_ostreamIcNS_11char_traitsIcEEE5flushEv, __ZNSt3__113basic_ostreamIcNS_11char_traitsIcEEE6sentryC1ERS3_, __ZNSt3__113basic_ostreamIcNS_11char_traitsIcEEE6sentryD1Ev, __ZNSt3__14coutE, __ZNSt3__15ctypeIcE2idE, __ZNSt3__16localeD1Ev, __ZNSt3__18ios_base33__set_badbit_and_consider_rethrowEv, __ZNSt3__18ios_base5clearEj, __ZSt9terminatev, ___cxa_begin_catch, ___cxa_end_catch, ___gxx_personality_v0, _memset, _strlen ]
Failed to materialize symbols: { (0x7fd514708170, { __ZNSt3__124__put_character_sequenceIcNS_11char_traitsIcEEEERNS_13basic_ostreamIT_T0_EES7_PKS4_m, __ZNSt3__1lsINS_11char_traitsIcEEEERNS_13basic_ostreamIcT_EES6_PKc, __ZNSt3__111char_traitsIcE11eq_int_typeEii, __ZNSt3__111char_traitsIcE3eofEv, __ZNSt3__111char_traitsIcE6lengthEPKc, _main, _link, _add, ___clang_call_terminate, __ZNSt3__116__pad_and_outputIcNS_11char_traitsIcEEEENS_19ostreambuf_iteratorIT_T0_EES6_PKS4_S8_S8_RNS_8ios_baseES4_ }) }

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions