问题
I'm building a LLVM language with some tutorials.
In a situation, there are command with:
clang++ -g main.cpp `llvm-config --cxxflags --ldflags --system-libs --libs core orcjit native` -O3 -o toy
here a command llvm-config --cxxflags --ldflags --system-libs --libs core orcjit native
which can expand to:
-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
It could be works fine with clang++ but I want coding with a CMake here.
Here is my 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})
It has a link error when use generated 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
Wonder how to convert the LLVM clang++ command to CMake config it. Full CMake project in Github (sorry for github is down at today 10-22)
回答1:
You did everything right!
The error message you are seeing is because LLVM was compiled without RTTI (Run-time type information) enabled.
You should specify compiler flags explicitly. LLVM exposes another property that helps to make the right decision:
if (NOT LLVM_ENABLE_RTTI)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fno-rtti")
endif()
It should help.
来源:https://stackoverflow.com/questions/52931852/how-to-convert-llvm-clang-command-line-to-cmake-config