C++:如何将 LLVM 嵌套到你的项目中去

  • IDE: Clion
  • LLVM
cmake_minimum_required(VERSION 3.9)  
project(clang_demo)  
  
find_package(LLVM REQUIRED CONFIG)  
  
message(STATUS "Found LLVM ${LLVM_PACKAGE_VERSION}")  
message(STATUS "Using LLVMConfig.cmake in: ${LLVM_DIR}")  
  
set(CMAKE_CXX_STANDARD 17)  
  
# 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})  
  
add_executable(clang_demo main.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(clang_demo ${llvm_libs})
// main.cpp
#include "llvm/IR/LLVMContext.h"  
#include "llvm/IR/Module.h"  
  
using namespace llvm;  
  
static std::unique_ptr<LLVMContext> my_Context;  
static std::unique_ptr<Module> my_Module;  
  
static void InitializeModule() {  
    my_Context = std::make_unique<LLVMContext>();  
    my_Module = std::make_unique<Module>("Hello Modlue", *my_Context);  
}  
  
int main(int argc, char *argv[]) {  
    InitializeModule();  
    my_Module->print(outs(), nullptr);  
  
    return 0;  
}

不使用 cmake 构建项目,可执行以下命令

clang++ -g mai.cpp `llvm-config --cxxflags --ldflags --system-libs --libs core` -o hello_world

获取 LLVM 配置信息

llvm-config --cxxflags --ldflags --system-libs --libs core
posted @   RioTian  阅读(176)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· Obsidian + DeepSeek:免费 AI 助力你的知识管理,让你的笔记飞起来!
· 分享4款.NET开源、免费、实用的商城系统
· 解决跨域问题的这6种方案,真香!
· 一套基于 Material Design 规范实现的 Blazor 和 Razor 通用组件库
· 5. Nginx 负载均衡配置案例(附有详细截图说明++)
点击右上角即可分享
微信分享提示