15

I'm trying to add LLVM to a cmake project, using cygwin as a compiler. I downloaded LLVM from cygwin's installer (just installed all of the llvm related packages). The files are there, however I cannot include LLVM in my project. I tried using the official guide for 3.5.2 (the version it installed) and my CMakeLists.txt looks like

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})

However, I get a bunch of errors like these

enter image description here

Am I doing something wrong? All I want to do is to use LLVM in my project.

Marco A.
  • 43,032
  • 26
  • 132
  • 246
anonra
  • 301
  • 1
  • 3
  • 6
  • You should figure out the libraries for the components you need. Take a look at http://stackoverflow.com/a/25783251/1938163 – Marco A. Jun 16 '15 at 12:48
  • possible duplicate of [Cmake and clang tooling linking error (outside of source tree)](http://stackoverflow.com/questions/25782537/cmake-and-clang-tooling-linking-error-outside-of-source-tree) – Mark Garcia Jun 16 '15 at 14:30

2 Answers2

18

The answer here is stale.
In newer versions of LLVM, there is an included tool to include LLVM inside a CMake project.
Please see the documentation of how to embed LLVM in your project

You'll thus have this 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})

This should work.
If you are having problems on windows, install the source package and not the binary package as suggested in this thread

if you have this error:

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

replace the ${llvm_libs} in target_link_libraries() by LLVM you can see this github thread

Narice
  • 311
  • 3
  • 6
5

As indicated by Marco A. in the comments, the problem were missing libraries. This link helped resolve the issue, and everything seems to be working normally now. https://stackoverflow.com/a/25783251/1938163

Thank you.

Community
  • 1
  • 1
anonra
  • 301
  • 1
  • 3
  • 6