llvm 运行 万花筒示例

llvm run Kaleidoscope examples

我正在使用 mac osX。我从 git 存储库下载了 llvm,现在我正在尝试构建示例(第 6 章)。我安装了 llvm: 当 运行: which llvm-config 我得到 /usr/local/bin/llvm-config

当 运行

clang++ -g toy.cpp `llvm-config --cxxflags --ldflags --system-libs --libs core mcjit native` -O3 -o toy

根据以下建议:http://llvm.org/docs/tutorial/LangImpl06.html

我得到:

Undefined symbols for architecture x86_64:
  "llvm::orc::SimpleCompiler::operator()(llvm::Module&)", referenced from:
      llvm::orc::LegacyIRCompileLayer<llvm::orc::LegacyRTDyldObjectLinkingLayer, llvm::orc::SimpleCompiler>::addModule(unsigned long long, std::__1::unique_ptr<llvm::Module, std::__1::default_delete<llvm::Module> >) in toy-e99926.o
  "llvm::orc::SymbolResolver::anchor()", referenced from:
      vtable for llvm::orc::LegacyLookupFnResolver<llvm::orc::KaleidoscopeJIT::KaleidoscopeJIT()::'lambda'(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&)> in toy-e99926.o
  "llvm::orc::SymbolStringPtr::Tombstone", referenced from:
      llvm::orc::JITDylib::~JITDylib() in toy-e99926.o
      llvm::orc::JITDylib::MaterializingInfo::~MaterializingInfo() in toy-e99926.o
      llvm::orc::LegacyLookupFnResolver<llvm::orc::KaleidoscopeJIT::KaleidoscopeJIT()::'lambda'(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&)>::getResponsibilitySet(llvm::DenseSet<llvm::orc::SymbolStringPtr, llvm::DenseMapInfo<llvm::orc::SymbolStringPtr> > const&) in toy-e99926.o
      llvm::orc::getResponsibilitySetWithLegacyFn<llvm::orc::KaleidoscopeJIT::KaleidoscopeJIT()::'lambda'(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&)>(llvm::Expected<llvm::DenseSet<llvm::orc::SymbolStringPtr, llvm::DenseMapInfo<llvm::orc::SymbolStringPtr> > >, llvm::DenseSet<llvm::orc::SymbolStringPtr, llvm::DenseMapInfo<llvm::orc::SymbolStringPtr> > const&, llvm::orc::KaleidoscopeJIT::KaleidoscopeJIT()::'lambda'(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&)) in toy-e99926.o
      std::__1::pair<llvm::DenseMapIterator<llvm::orc::SymbolStringPtr, llvm::detail::DenseSetEmpty, llvm::DenseMapInfo<llvm::orc::SymbolStringPtr>, llvm::detail::DenseSetPair<llvm::orc::SymbolStringPtr>, false>, bool> llvm::DenseMapBase<llvm::DenseMap<llvm::orc::SymbolStringPtr, llvm::detail::DenseSetEmpty, llvm::DenseMapInfo<llvm::orc::SymbolStringPtr>, llvm::detail::DenseSetPair<llvm::orc::SymbolStringPtr> >, llvm::orc::SymbolStringPtr, llvm::detail::DenseSetEmpty, llvm::DenseMapInfo<llvm::orc::SymbolStringPtr>, llvm::detail::DenseSetPair<llvm::orc::SymbolStringPtr> >::try_emplace<llvm::detail::DenseSetEmpty&>(llvm::orc::SymbolStringPtr const&, llvm::detail::DenseSetEmpty&&&) in toy-e99926.o
      llvm::detail::DenseSetPair<llvm::orc::SymbolStringPtr>* llvm::DenseMapBase<llvm::DenseMap<llvm::orc::SymbolStringPtr, llvm::detail::DenseSetEmpty, llvm::DenseMapInfo<llvm::orc::SymbolStringPtr>, llvm::detail::DenseSetPair<llvm::orc::SymbolStringPtr> >, llvm::orc::SymbolStringPtr, llvm::detail::DenseSetEmpty, llvm::DenseMapInfo<llvm::orc::SymbolStringPtr>, llvm::detail::DenseSetPair<llvm::orc::SymbolStringPtr> >::InsertIntoBucketImpl<llvm::orc::SymbolStringPtr>(llvm::orc::SymbolStringPtr const&, llvm::orc::SymbolStringPtr const&, llvm::detail::DenseSetPair<llvm::orc::SymbolStringPtr>*) in toy-e99926.o
      llvm::DenseMapBase<llvm::DenseMap<llvm::orc::SymbolStringPtr, llvm::detail::DenseSetEmpty, llvm::DenseMapInfo<llvm::orc::SymbolStringPtr>, llvm::detail::DenseSetPair<llvm::orc::SymbolStringPtr> >, llvm::orc::SymbolStringPtr, llvm::detail::DenseSetEmpty, llvm::DenseMapInfo<llvm::orc::SymbolStringPtr>, llvm::detail::DenseSetPair<llvm::orc::SymbolStringPtr> >::moveFromOldBuckets(llvm::detail::DenseSetPair<llvm::orc::SymbolStringPtr>*, llvm::detail::DenseSetPair<llvm::orc::SymbolStringPtr>*) in toy-e99926.o
      ...
  "llvm::orc::ExecutionSession::legacyFailQuery(llvm::orc::AsynchronousSymbolQuery&, llvm::Error)", referenced from:
      llvm::orc::lookupWithLegacyFn<llvm::orc::KaleidoscopeJIT::KaleidoscopeJIT()::'lambda'(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&)>(llvm::DenseSet<llvm::orc::SymbolStringPtr, llvm::DenseMapInfo<llvm::orc::SymbolStringPtr> >, llvm::orc::ExecutionSession&, llvm::orc::AsynchronousSymbolQuery&, llvm::DenseSet<llvm::orc::SymbolStringPtr, llvm::DenseMapInfo<llvm::orc::SymbolStringPtr> > const&, llvm::orc::KaleidoscopeJIT::KaleidoscopeJIT()::'lambda'(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&)) in toy-e99926.o
  "llvm::orc::ExecutionSession::ExecutionSession(std::__1::shared_ptr<llvm::orc::SymbolStringPool>)", referenced from:
      llvm::orc::KaleidoscopeJIT::KaleidoscopeJIT() in toy-e99926.o
  "llvm::orc::AsynchronousSymbolQuery::handleFullyReady()", referenced from:
      llvm::orc::lookupWithLegacyFn<llvm::orc::KaleidoscopeJIT::KaleidoscopeJIT()::'lambda'(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&)>(llvm::DenseSet<llvm::orc::SymbolStringPtr, llvm::DenseMapInfo<llvm::orc::SymbolStringPtr> >, llvm::orc::ExecutionSession&, llvm::orc::AsynchronousSymbolQuery&, llvm::DenseSet<llvm::orc::SymbolStringPtr, llvm::DenseMapInfo<llvm::orc::SymbolStringPtr> > const&, llvm::orc::KaleidoscopeJIT::KaleidoscopeJIT()::'lambda'(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&)) in toy-e99926.o
  "llvm::orc::AsynchronousSymbolQuery::notifySymbolReady()", referenced from:
      llvm::orc::lookupWithLegacyFn<llvm::orc::KaleidoscopeJIT::KaleidoscopeJIT()::'lambda'(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&)>(llvm::DenseSet<llvm::orc::SymbolStringPtr, llvm::DenseMapInfo<llvm::orc::SymbolStringPtr> >, llvm::orc::ExecutionSession&, llvm::orc::AsynchronousSymbolQuery&, llvm::DenseSet<llvm::orc::SymbolStringPtr, llvm::DenseMapInfo<llvm::orc::SymbolStringPtr> > const&, llvm::orc::KaleidoscopeJIT::KaleidoscopeJIT()::'lambda'(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&)) in toy-e99926.o
  "llvm::orc::AsynchronousSymbolQuery::handleFullyResolved()", referenced from:
      llvm::orc::lookupWithLegacyFn<llvm::orc::KaleidoscopeJIT::KaleidoscopeJIT()::'lambda'(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&)>(llvm::DenseSet<llvm::orc::SymbolStringPtr, llvm::DenseMapInfo<llvm::orc::SymbolStringPtr> >, llvm::orc::ExecutionSession&, llvm::orc::AsynchronousSymbolQuery&, llvm::DenseSet<llvm::orc::SymbolStringPtr, llvm::DenseMapInfo<llvm::orc::SymbolStringPtr> > const&, llvm::orc::KaleidoscopeJIT::KaleidoscopeJIT()::'lambda'(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&)) in toy-e99926.o
  "llvm::orc::AsynchronousSymbolQuery::resolve(llvm::orc::SymbolStringPtr const&, llvm::JITEvaluatedSymbol)", referenced from:
      llvm::orc::lookupWithLegacyFn<llvm::orc::KaleidoscopeJIT::KaleidoscopeJIT()::'lambda'(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&)>(llvm::DenseSet<llvm::orc::SymbolStringPtr, llvm::DenseMapInfo<llvm::orc::SymbolStringPtr> >, llvm::orc::ExecutionSession&, llvm::orc::AsynchronousSymbolQuery&, llvm::DenseSet<llvm::orc::SymbolStringPtr, llvm::DenseMapInfo<llvm::orc::SymbolStringPtr> > const&, llvm::orc::KaleidoscopeJIT::KaleidoscopeJIT()::'lambda'(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&)) in toy-e99926.o
  "llvm::orc::JITSymbolResolverAdapter::JITSymbolResolverAdapter(llvm::orc::ExecutionSession&, llvm::orc::SymbolResolver&, llvm::orc::MaterializationResponsibility*)", referenced from:
      llvm::orc::LegacyRTDyldObjectLinkingLayer::ConcreteLinkedObject<std::__1::shared_ptr<llvm::RuntimeDyld::MemoryManager> >::finalize() in toy-e99926.o
  "vtable for llvm::orc::JITSymbolResolverAdapter", referenced from:
      llvm::orc::LegacyRTDyldObjectLinkingLayer::ConcreteLinkedObject<std::__1::shared_ptr<llvm::RuntimeDyld::MemoryManager> >::finalize() in toy-e99926.o
  NOTE: a missing vtable usually means the first non-inline virtual member function has no definition.
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

有谁知道如何修复它?

运行宁这样:

clang++ -g toy.cpp $(llvm-config --cxxflags --ldflags --system-libs --libs) -O3 -o toy

似乎有效

我确认您需要做的就是按照快速入门部分中的这些步骤http://llvm.org/docs/CMake.html,然后运行按照我在上面介绍的方式执行命令,至少它适用于第 6 章[=12] =]

作为奖金

Clion MacOs X 的 Cmake 文件和 linux(linux 未测试)可能看起来像这样。 确保 llvm 安装正确(按照上述步骤)

cmake_minimum_required(VERSION 3.10)
project(project_name )

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)

set(CMAKE_C_COMPILER clang)
set(CMAKE_CXX_COMPILER clang++)

link_libraries()
execute_process(COMMAND llvm-config --libs OUTPUT_VARIABLE LIBS)
execute_process(COMMAND llvm-config --system-libs OUTPUT_VARIABLE SYS_LIBS)
execute_process(COMMAND llvm-config --ldflags OUTPUT_VARIABLE LDF)
#message(STATUS "Found LLVM" ${LIBS})

string(STRIP ${LIBS} LIBS)
string(STRIP ${SYS_LIBS} SYS_LIBS)
string(STRIP ${LDF} LDF)

link_libraries(${LIBS} ${SYS_LIBS} ${LDF})

execute_process(COMMAND llvm-config --cxxflags OUTPUT_VARIABLE CMAKE_CXX_FLAGS)
string(STRIP ${CMAKE_CXX_FLAGS} CMAKE_CXX_FLAGS)

add_executable(project_name   toy.cpp)