2014-09-06 118 views
0

我正在閱讀「LLVM核心庫入門」一書,但我無法編譯第3章中的示例。我已按照作者的建議安裝了LLVM 3.4。我正在使用Mint Linux 64位。LLVM核心庫入門書無法編譯代碼

In file included from helloworld.c:1:0: 
/home/user/llvm3.4/llvm/include/llvm/Bitcode/ReaderWriter.h:17:18: fatal error: string: No such file or directory 
#include <string> 
       ^
compilation terminated. 

編輯:從.C重命名文件到.CPP

現在我面臨的另一個問題解決了上述問題。

helloworld.cpp: In function ‘int main(int, char**)’: 
helloworld.cpp:16:21: error: aggregate ‘llvm::LLVMContext context’ has incomplete type and cannot be defined 
     LLVMContext context; 
        ^
helloworld.cpp:22:21: error: ‘cerr’ was not declared in this scope 
       std:cerr << "Error reading bitcode: " << error << std::end; 
        ^
helloworld.cpp:22:21: note: suggested alternative: 
In file included from helloworld.cpp:8:0: 
/usr/include/c++/4.8/iostream:62:18: note: ‘std::cerr’ 
    extern ostream cerr; /// Linked to standard error (unbuffered) 
       ^
helloworld.cpp:22:17: warning: label ‘std’ defined but not used [-Wunused-label] 
       std:cerr << "Error reading bitcode: " << error << std::end; 
       ^
make: *** [helloworld] Error 1 

UPDATE:

檢查我的回答下面這有鏈接到一個碼頭工人的工作形象。

+1

它應該是C還是C++?看起來你在C文件中包含一個C++頭文件。 – Biffen 2014-09-06 09:33:36

+2

你應該爲你的新問題提出一個新問題。 – Biffen 2014-09-06 09:34:43

+0

有沒有人有一個碼頭安裝程序的所有示例代碼編譯?我錯誤後出現錯誤:(編輯:示例代碼在這裏http://examples.oreilly.com/9781782166924/ – user361697 2014-09-06 09:43:56

回答

1

llvm-clang-samples項目包含許多使用LLVM和Clang API的樣本,Makefile演示瞭如何構建它們與LLVM。它在每個最近的版本中都有分支,包括3.4;所以你可以拉它,看看llvm3.4分支並構建樣本。然後,修改Makefile的構建步驟以編寫書中的樣本。

1

makefile看起來有點壞了,Makefile中的鏈接行;

$(QUIET)$(CXX) -o [email protected] $(CXXFLAGS) $(LDFLAGS) $^ `$(LLVM_CONFIG) --libs bitreader support` 

......應該有$(LDFLAGS)最後一行爲;

$(QUIET)$(CXX) -o [email protected] $(CXXFLAGS) $^ `$(LLVM_CONFIG) --libs bitreader support` $(LDFLAGS) 

...讓libs以正確的順序鏈接。這使得我的第一個示例在我的Ubuntu機器上編譯和運行。

有點偏離主題,但由於在我無法測試的環境中可能會出現很多錯誤,要開始,您可以使用此Dockerfile爲您的開發構建一個工作環境;

FROM ubuntu:14.04 
RUN apt-get -qq update && apt-get install -qq llvm-3.4 build-essential wget unzip libclang-3.4-dev > /dev/null 
RUN update-alternatives --install /usr/bin/llvm-config llvm-config /usr/bin/llvm-config-3.4 34 

WORKDIR /tmp 
RUN wget http://examples.oreilly.com/9781782166924/9781782166924_Code.zip && unzip -x 9781782166924_Code.zip && mv 6924OS_Code/Chapter\ 3 6924OS_Code/Chapter3 
WORKDIR /tmp/6924OS_Code/Chapter3 
RUN mv Makefile Makefile.bak && cat Makefile.bak | sed 's/.*bitreader.*/\t$(QUIET)$(CXX) -o [email protected] $(CXXFLAGS) $^ `$(LLVM_CONFIG) --libs bitreader support` $(LDFLAGS)/' >Makefile 

此下載的樣本,重命名第一個樣本不包含路徑中的任何空間(似乎也打破了Makefile文件),並正確地修補第一Makefile文件建立的東西,如果你剛開始的形象和類型「使」。

+0

感謝第3章編譯。 – user361697 2014-09-06 19:05:28

+0

第4章也不編譯:(編譯project.cpp /tmp/6924OS_Code/Chapter4/project.cpp:6:27:致命錯誤:clang -c/Index.h:沒有這樣的文件或目錄 #include「clang- c/Index.h「 ^ 編譯已終止 make:*** [project.o]錯誤1 – user361697 2014-09-07 10:50:00

+0

@ user361697似乎需要libclang-3.4-dev用於該示例,更新了Dockerfile部分。在添加包後與上面相同,$(LDFLAGS)需要在鏈接線上最後移動。 – 2014-09-07 15:11:04

1

通過包含頭文件「LLVM/IR/LLVMContext.h」,我可以解決「的錯誤:總‘LLVM :: LLVMContext語境’有不完整的類型,不能被定義「。