在CLion中包含llvm库的正确方法是什么?

时间:2019-02-05 15:40:55

标签: c++ cmake clion llvm-ir

我最近安装了LLVM 7,并尝试包括必要的文件以使用CLion中的llvm库启动项目。 但是,它抱怨找不到某些文件。

我的CMakeLists.txt文件具有以下内容:

cmake_minimum_required(VERSION 3.12)
project(TestCmake)
set(CMAKE_CXX_STANDARD 11)

link_directories(llvm/build/include/) # linked wrongly..
include_directories(llvm/llvm/include/) #linked wrongly.

set(BUILD_2 main_2)
set(SOURCE_FILES_2
        # testing. llvm files.
        tests/codegen_tests/fac.cpp
)

add_executable(${BUILD_2} ${SOURCE_FILES_2})

我知道链接的方式是错误的,但是我不知道如何解决。 我之所以这样,是因为CLion可以找到已定义的库(因此,我可以看到何时调用不存在的库函数,如果有的话,我会弹出一个窗口)。

我在运行项目时当前遇到以下错误:

   In file included from c4/llvm/llvm/include/llvm/IR/Module.h:23:0,
                 from c4/tests/codegen_tests/fac.cpp:1:
c4/llvm/llvm/include/llvm/IR/Attributes.h:74:38: fatal error: llvm/IR/Attributes.inc: No such file or directory
compilation terminated.
CMakeFiles/main_2.dir/build.make:62: recipe for target 'CMakeFiles/main_2.dir/tests/codegen_tests/fac.cpp.o' failed
make[3]: *** [CMakeFiles/main_2.dir/tests/codegen_tests/fac.cpp.o] Error 1
CMakeFiles/Makefile2:109: recipe for target 'CMakeFiles/main_2.dir/all' failed
make[2]: *** [CMakeFiles/main_2.dir/all] Error 2
CMakeFiles/Makefile2:121: recipe for target 'CMakeFiles/main_2.dir/rule' failed
make[1]: *** [CMakeFiles/main_2.dir/rule] Error 2
Makefile:153: recipe for target 'main_2' failed
make: *** [main_2] Error 2

项目结构如下所示Project Structure

对此有任何帮助,我们将不胜感激。谢谢。

1 个答案:

答案 0 :(得分:0)

我遇到了类似的问题,LLVM documentation:解决了我的问题。

cmake_minimum_required(VERSION 3.4.3)
project(TestCmake)

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})
相关问题