代码之家  ›  专栏  ›  技术社区  ›  LoranceChen

如何将llvm clang++命令行转换为cmake config?

  •  0
  • LoranceChen  · 技术社区  · 7 年前

    我正在用一些教程构建一个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 config it。 完整的CMAKE项目 Github (很抱歉Github今天10-22点关门)

    1 回复  |  直到 7 年前
        1
  •  0
  •   AlexDenisov    7 年前

    你做的一切都对!

    您看到的错误消息是因为llvm是在没有rtti的情况下编译的( Run-time type information )已启用。

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

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

    应该会有帮助的。

    推荐文章