如何将LLVM clang ++命令行转换为cmake配置?

时间:2018-10-22 14:34:16

标签: cmake llvm

我正在通过一些教程来构建LLVM语言。
在某种情况下,可以使用以下命令:

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

此处的命令llvm-config --cxxflags --ldflags --system-libs --libs core orcjit native可以扩展为:

-I/usr/local/include  -fPIC -fvisibility-inlines-hidden -Werror=date-time -Werror=unguarded-availability-new -std=c++11 -Wall -Wextra -Wno-unused-parameter -Wwrite-strings -Wcast-qual -Wmissing-field-initializers -pedantic -Wno-long-long -Wcovered-switch-default -Wnon-virtual-dtor -Wdelete-non-virtual-dtor -Wstring-conversion -g  -fno-exceptions -fno-rtti -D_DEBUG -D__STDC_CONSTANT_MACROS -D__STDC_FORMAT_MACROS -D__STDC_LIMIT_MACROS
-L/usr/local/lib -Wl,-search_paths_first -Wl,-headerpad_max_install_names
-lLLVMX86Disassembler -lLLVMX86AsmParser -lLLVMX86CodeGen -lLLVMGlobalISel -lLLVMSelectionDAG -lLLVMAsmPrinter -lLLVMCodeGen -lLLVMScalarOpts -lLLVMInstCombine -lLLVMAggressiveInstCombine -lLLVMBitWriter -lLLVMX86Desc -lLLVMMCDisassembler -lLLVMX86Info -lLLVMX86AsmPrinter -lLLVMX86Utils -lLLVMOrcJIT -lLLVMTransformUtils -lLLVMExecutionEngine -lLLVMTarget -lLLVMAnalysis -lLLVMProfileData -lLLVMRuntimeDyld -lLLVMObject -lLLVMMCParser -lLLVMBitReader -lLLVMMC -lLLVMDebugInfoCodeView -lLLVMDebugInfoMSF -lLLVMCore -lLLVMBinaryFormat -lLLVMSupport -lLLVMDemangle
-lz -lcurses -lm -lxml2

使用clang ++可能效果很好,但是我想在这里用CMake进行编码。
这是我的CmakeLists.txt:

cmake_minimum_required(VERSION 3.10)

set(CMAKE_CXX_STANDARD 17)


project(SimpleProject)

find_package(LLVM 7.0.0 REQUIRED CONFIG)

message(STATUS "Found LLVM ${LLVM_PACKAGE_VERSION}")
message(STATUS "CMAKE_ROOT ${CMAKE_ROOT}")
message(STATUS "CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH}")
message(STATUS "LLVM_FOUND ${LLVM_FOUND}")
message(STATUS "LLVM_DIR ${LLVM_DIR}")
message(STATUS "LLVM_INCLUDE_DIRS: ${LLVM_INCLUDE_DIRS}")
message(STATUS "LLVM_DEFINITIONS: ${LLVM_DEFINITIONS}")

message(STATUS "Using LLVMConfig.cmake in: ${LLVM_DIR}")

# Set your project compile flags.
# E.g. if using the C++ header files
# you will need to enable C++11 support
# for your compiler.

include_directories(${LLVM_INCLUDE_DIRS})
add_definitions(${LLVM_DEFINITIONS})

# Now build our tools
add_executable(simple-tool KaleidoscopeJIT.h main.cpp)

# Find the libraries that correspond to the LLVM components
# that we wish to use
llvm_map_components_to_libnames(
        llvm_libs
        Analysis
        Core
        ExecutionEngine
        InstCombine
        Object
        OrcJIT
        RuntimeDyld
        ScalarOpts
        Support
        TransformUtils
        native
        irreader
        )

# Link against LLVM libraries
target_link_libraries(simple-tool ${llvm_libs})

使用生成的make时出现链接错误:

[ 50%] Linking CXX executable simple-tool
Undefined symbols for architecture x86_64:
  "typeinfo for llvm::ErrorInfoBase", referenced from:
      typeinfo for llvm::ErrorInfo<llvm::ErrorList, llvm::ErrorInfoBase> in main.cpp.o
  "typeinfo for llvm::orc::SymbolResolver", referenced from:
      typeinfo 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 main.cpp.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make[3]: *** [simple-tool] Error 1
make[2]: *** [CMakeFiles/simple-tool.dir/all] Error 2
make[1]: *** [CMakeFiles/simple-tool.dir/rule] Error 2
make: *** [simple-tool] Error 2

想知道如何将LLVM clang ++命令转换为CMake配置它。 Github中的完整CMake项目(对不起,github,今天10:22截止)

1 个答案:

答案 0 :(得分:0)

您做得对!

您看到的错误消息是因为LLVM是在未启用RTTI(Run-time type information)的情况下编译的。

您应该明确指定编译器标志。 LLVM公开了另一个有助于做出正确决定的属性:

if (NOT LLVM_ENABLE_RTTI)
  set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fno-rtti")
endif()

应该有帮助。