【问题标题】:Add LLVM to project using cmake使用 cmake 将 LLVM 添加到项目中
【发布时间】:2015-06-16 12:32:30
【问题描述】:

我正在尝试将 LLVM 添加到 cmake 项目,使用 cygwin 作为编译器。我从 cygwin 的安装程序下载了 LLVM(刚刚安装了所有与 llvm 相关的软件包)。这些文件在那里,但是我不能在我的项目中包含 LLVM。我尝试使用 3.5.2 的官方指南(它安装的版本),我的 CMakeLists.txt 看起来像

cmake_minimum_required(VERSION 3.2)
project(Lang)

find_package(LLVM REQUIRED CONFIG)

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


include_directories(${LLVM_INCLUDE_DIRS})
add_definitions(${LLVM_DEFINITIONS})
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")

set(SOURCE_FILES main.cpp)

add_executable(Lang ${SOURCE_FILES})

llvm_map_components_to_libnames(llvm_libs support core irreader)

# Link against LLVM libraries
target_link_libraries(Lang ${llvm_libs})

但是,我得到了一堆这样的错误

我做错了吗?我想做的就是在我的项目中使用 LLVM。

【问题讨论】:

标签: c++ cmake llvm llvm-c++-api llvm-3.0


【解决方案1】:

这里的答案是陈旧的。
在较新版本的 LLVM 中,有一个包含工具可将 LLVM 包含在 CMake 项目中。
请看how to embed LLVM in your project的文档

因此,您将在您的项目 CMakeLists.txt 中拥有它:

cmake_minimum_required(VERSION 3.4.3)
project(SimpleProject)

find_package(LLVM REQUIRED CONFIG)

message(STATUS "Found LLVM ${LLVM_PACKAGE_VERSION}")
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 tool.cpp)

# Find the libraries that correspond to the LLVM components
# that we wish to use
llvm_map_components_to_libnames(llvm_libs support core irreader)

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

这应该可行。
如果您在 windows 上遇到问题,请按照this thread 中的建议安装源包而不是二进制包

如果你有这个错误:

CommandLine Error: Option 'help-list' registered more than once!
LLVM ERROR: inconsistency in registered CommandLine options

target_link_libraries() 中的${llvm_libs} 替换为LLVM 你可以看到this github thread

【讨论】:

  • 这实际上与问题中的代码相同
【解决方案2】:

正如 Marco A. 在 cmets 中所指出的,问题在于缺少库。这个链接帮助解决了这个问题,现在一切似乎都正常了。 https://stackoverflow.com/a/25783251/1938163

谢谢。

【讨论】:

  • 不客气 :) 将其标记为答案,如果您愿意,可以扩展解决方案。
猜你喜欢
  • 1970-01-01
  • 2019-07-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-11-15
  • 1970-01-01
  • 2017-02-16
  • 1970-01-01
相关资源
最近更新 更多