【问题标题】:how to convert LLVM clang++ command line to cmake config?如何将 LLVM clang++ 命令行转换为 cmake 配置?
【发布时间】:2018-10-22 14:34:16
【问题描述】:

我正在使用一些教程构建 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 关闭)

【问题讨论】:

    标签: cmake llvm


    【解决方案1】:

    你做对了!

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

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

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

    应该有帮助。

    【讨论】:

    • 它现在可以工作了,你可以节省我一整天的时间。非常感谢!
    猜你喜欢
    • 2014-11-04
    • 1970-01-01
    • 2023-03-04
    • 1970-01-01
    • 2017-03-05
    • 2014-03-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多