【问题标题】:How to compile C++ codes with clang/LLVM libraries?如何使用 clang/LLVM 库编译 C++ 代码?
【发布时间】:2015-05-14 07:41:27
【问题描述】:

我是 Linux 新手,正在学习一些 clang 教程。但是,我发现即使是一个简单的文件也很难编译。所以,这里是部分代码:

#include <cstdio>
#include <string>
#include <iostream>
#include <sstream>
#include <map>
#include <utility>
#include "clang/AST/ASTConsumer.h"
#include "clang/AST/RecursiveASTVisitor.h"
#include "clang/Basic/Diagnostic.h"
#include "clang/Basic/FileManager.h"
#include "clang/Basic/SourceManager.h"
#include "clang/Basic/TargetOptions.h"
#include "clang/Basic/TargetInfo.h"
#include "clang/Frontend/CompilerInstance.h"
#include "clang/Lex/Preprocessor.h"
#include "clang/Parse/ParseAST.h"
#include "clang/Rewrite/Core/Rewriter.h"
#include "clang/Rewrite/Frontend/Rewriters.h"
#include "llvm/Support/Host.h"
#include "llvm/Support/raw_ostream.h"

using namespace clang;
using namespace std;

当我尝试使用以下命令编译简单代码(比如说 PrintFunctions.cpp)时:

clang++ -o PrintFunctions PrintFunctions.cpp

我得到了错误:

致命错误:找不到“clang/AST/ASTConsumer.h”文件

嗯,我已经检查了我的 LLVM 和 clang 已经安装好了,并且文件 'clang/AST/ASTConsumer.h' 在

/usr/lib/llvm-3.4/include/clang/AST

所以,我在指挥中一定有什么遗漏的。我不知道该怎么做...我在网上阅读了一些教程,其中大多数都使用了makefile,它们似乎很复杂。 那么,如何编译呢?如何找到更简单的makefile编写方法?

顺便说一句,我在 Ubuntu 14.04 下,clang/LLVM 版本是 3.4。

【问题讨论】:

    标签: c++ linux makefile clang llvm


    【解决方案1】:

    通常,在您的 makefile 中,您应该有如下内容:

    CXXFLAGS += `${LLVM_DIR}/bin/llvm-config --cxxflags`
    

    LDFLAGS += `${LLVM_DIR}/bin/llvm-config --ldflags`
    LLVMLIBS = `${LLVM_DIR}/bin/llvm-config --libs`
    

    [根据托马斯的评论编辑]

    您可能还会使用:

    LLVMLIBS += `${LLVM_DIR}/bin/llvm-config --system-libs`
    

    对于不属于 LLVM 但 LLVM 依赖的东西。

    [结束编辑]

    然后使用这些:

    .cpp.o: 
        ${CXX} ${CXXFLAGS} ${CXX_EXTRA} -c -o $@ $<
    
    myprog: ${OBJECTS}
        ${LD} ${LDFLAGS} -o $@ ${OBJECTS} ${LIBS}
    

    (其中${OBJECTS} 是您的目标文件列表)

    LLVM_DIR 当然应该设置为指向 LLVM 的安装位置,例如/usr//usr/local - 运行 which clang++ 以查看您的 clang 的安装位置。

    这是我的 Makefile 用于我的 Pascal 编译器,使用 LLVM 作为后端。

    【讨论】:

    • LLVM 3.5+ 还附带--system-libs,链接可能需要它。
    猜你喜欢
    • 1970-01-01
    • 2011-12-20
    • 1970-01-01
    • 2019-09-22
    • 2012-02-27
    • 1970-01-01
    • 1970-01-01
    • 2020-01-19
    • 2013-04-29
    相关资源
    最近更新 更多