2012-12-07 10 views
0

我從here.獲取這些代碼請按照以下步驟操作。但是有錯誤,當我make錯誤:'clang'不是命名空間名稱

using namespace clang; 
int main() 
{ 
    CompilerInstance ci; 
    ci.createDiagnostics(0,NULL); // create DiagnosticsEngine 
    ci.createFileManager(); // create FileManager 
    ci.createSourceManager(ci.getFileManager()); // create SourceManager 
    ci.createPreprocessor(); // create Preprocessor 
    const FileEntry *pFile = ci.getFileManager().getFile("hello.c"); 
    ci.getSourceManager().createMainFileID(pFile); 
    ci.getPreprocessor().EnterMainSourceFile(); 
    ci.getDiagnosticClient().BeginSourceFile(ci.getLangOpts(), &ci.getPreprocessor()); 
    Token tok; 
    do { 
     ci.getPreprocessor().Lex(tok); 
     if(ci.getDiagnostics().hasErrorOccurred()) 
      break; 
     ci.getPreprocessor().DumpToken(tok); 
     std::cerr << std::endl; 
    } while (tok.isNot(clang::tok::eof)); 
    ci.getDiagnosticClient().EndSourceFile(); 
} 

Makefile它。

CXX := g++ 
    RTTIFLAG := -fno-rtti 
    CXXFLAGS := $(shell llvm-config --cxxflags) $(RTTIFLAG) 
    LLVMLDFLAGS := $(shell llvm-config --ldflags --libs) 
    DDD := $(shell echo $(LLVMLDFLAGS)) 
    SOURCES = main.cpp 
    OBJECTS = $(SOURCES:.cpp=.o) 
    EXES = $(OBJECTS:.o=) 
    CLANGLIBS = \ 
      -L /usr/local/lib \ 
      -lclangFrontend \ 
      -lclangParse \ 
      -lclangSema \ 
      -lclangAnalysis \ 
      -lclangAST \ 
      -lclangLex \ 
      -lclangBasic \ 
      -lclangDriver \ 
      -lclangSerialization \ 
      -lLLVMMC \ 
      -lLLVMSupport \ 
    all: $(OBJECTS) $(EXES) 
    %: %.o 
    $(CXX) -o [email protected] $< $(CLANGLIBS) $(LLVMLDFLAGS) 

錯誤消息我得到的,當我make

g++ -I/usr/include -DNDEBUG -D_GNU_SOURCE -D__STDC_CONSTANT_MACROS -D__STDC_FORMAT_MACROS -D__STDC_LIMIT_MACROS -fvisibility-inlines-hidden -fno-exceptions -fPIC -Woverloaded-virtual -Wcast-qual -fno-rtti -c -o main.o main.cpp 
main.cpp:1:21: error: ‘clang’ is not a namespace-name 
main.cpp:1:26: error: expected namespace-name before ‘;’ token 
main.cpp: In function ‘int main()’: 
main.cpp:4:9: error: ‘CompilerInstance’ was not declared in this scope 
main.cpp:4:26: error: expected ‘;’ before ‘ci’ 
main.cpp:5:9: error: ‘ci’ was not declared in this scope 
main.cpp:5:32: error: ‘NULL’ was not declared in this scope 
main.cpp:9:15: error: ‘FileEntry’ does not name a type 
main.cpp:10:48: error: ‘pFile’ was not declared in this scope 
main.cpp:13:9: error: ‘Token’ was not declared in this scope 
main.cpp:13:15: error: expected ‘;’ before ‘tok’ 
main.cpp:15:38: error: ‘tok’ was not declared in this scope 
main.cpp:19:13: error: ‘cerr’ is not a member of ‘std’ 
main.cpp:19:26: error: ‘endl’ is not a member of ‘std’ 
main.cpp:20:19: error: ‘tok’ was not declared in this scope 
main.cpp:20:29: error: ‘clang’ has not been declared 
make: *** [main.o] Error 1 

我是C++新手。如果這個問題太簡單,我很抱歉。提前致謝。

回答

2

你有沒有包含在您的實現文件的相關標題,例如:

#include <CompilerInstance.h> 
2

您需要#include相應的頭文件以便引用您嘗試工作的API的成員。

相關問題