2015-05-14 27 views
2

我是Linux新手,并通过一些clang教程工作。但是,我发现即使是一个简单的文件也很难编译。所以,这里是部分代码:用以下命令如何用clang/LLVM库编译C++代码?

#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 

,我得到的错误:

fatal error: 'clang/AST/ASTConsumer.h' file not found

嗯,我已经检查了我的LLVM和铛已经安装好,并且文件“铛/ AST/ASTConsumer.h”是

下找到

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

所以,一定有一些我错过了命令。我不知道该怎么做......我已经在线阅读了一些教程,其中大部分都使用了makefile,而且它们似乎很复杂。 那么,如何编译它呢?如何找到一个更简单的方法来编写makefile?

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

回答

6

通常,在你的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 [email protected] $< 

myprog: ${OBJECTS} 
    ${LD} ${LDFLAGS} -o [email protected] ${OBJECTS} ${LIBS} 

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

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

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

+1

LLVM 3.5+还附带了可用于链接的'--system-libs'。 – Thomas

+0

@Thomas:是的,没错。 –