user361697
user361697

Reputation: 1131

Getting started from LLVM Core libraries book cant compile code

I am going through the book "Getting started with LLVM Core libraries" but I am not able to compile the example in Chapter 3. I have installed LLVM 3.4 as the author suggested. I am using Mint Linux 64bit.

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.

Edit : Solved the above problem by renaming the file from .c to .cpp

Now I am facing another issue.

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 :

Check my answer below which has link to a working docker image.

Upvotes: 0

Views: 2134

Answers (4)

Stupid Dog
Stupid Dog

Reputation: 11

By including the header file "llvm/IR/LLVMContext.h", I can resolve "error: aggregate ‘llvm::LLVMContext context’ has incomplete type and cannot be defined".

Upvotes: 1

user361697
user361697

Reputation: 1131

Update :

I contacted Packt Publishing regarding this problem and the authors were glad to upload a docker image which works pretty well :)

https://registry.hub.docker.com/u/rafaelauler/llvmbook/

Check the /workspace folder in the image.

Thanks a lot to authors and Packt Publishing for this :)

Upvotes: 0

Joachim Isaksson
Joachim Isaksson

Reputation: 180917

The makefiles seem somewhat broken, the linking row in the Makefile;

$(QUIET)$(CXX) -o $@ $(CXXFLAGS) $(LDFLAGS) $^ `$(LLVM_CONFIG) --libs bitreader support`

...should have $(LDFLAGS) last in the line as;

$(QUIET)$(CXX) -o $@ $(CXXFLAGS) $^ `$(LLVM_CONFIG) --libs bitreader support` $(LDFLAGS)

...to get the libs to link in the correct order. That makes the first sample compile and run on my Ubuntu machine.

A bit off topic, but since there is so much that can go wrong in an environment I can't test; to get started, you could use this Dockerfile to build a working environment for your development;

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 $@ $(CXXFLAGS) $^ `$(LLVM_CONFIG) --libs bitreader support` $(LDFLAGS)/' >Makefile

This downloads the samples, renames the first sample to not contain any spaces in the path (seems to also break the Makefile) and patches the first Makefile to build things correctly if you just start the image and type "make".

Upvotes: 1

Eli Bendersky
Eli Bendersky

Reputation: 273456

The llvm-clang-samples project contains many samples using the LLVM and Clang APIs, with a Makefile to demonstrate how to build them vs. LLVM. It has branches for each of the recent releases, including 3.4; so you could pull it, check out the llvm3.4 branch and build the samples. Then, adapt the build steps from its Makefile to compile the samples from the book.

Upvotes: 1

Related Questions