[cfe-dev] problem building clang tutorials

Kevin Steiner c934w-kev7gn4lc at yahoo.de
Wed Nov 28 08:10:43 PST 2012


Hi,

I'd like to use clang to parse c++ and so my first step would be to try the tutorial.
The most up-to-date tutorial i found is that on https://github.com/loarabia/Clang-tutorial
Unfortunately, i ran into some problems trying to build it. I could solve some of them but the one i'm currently stuck at is 
undefined reference to `clang::edit::rewriteObjCRedundantCallWithLiteral(clang::ObjCMessageExpr const*, clang::NSAPI const&, clang::edit::Commit&)'
You can find a description of my build attempt here:

http://www.dinkypage.com/161365I'll also paste it below (though without the html formatting that makes it much more readable)

Could you please tell me what i'm doing wrong here and how i can get the tutorial to build?
Best Regards,
Kevin

-------------------------------------
Problem description (pasted from html page at above url without the formatting; the html page is much more readable; search for "Current problem" to skip the solved problems and go straight to the current one)

Clang tutorial build problem
Objective: get the Clang tutorial examples to build and do some C++ parsing.
we'll assume that LLVM and Clang 3.1 are installed in $LLVMDIR 
in our case LLVMDIR=/zapps/clang+llvm-3.1-x86_64-linux-ubuntu-11.10
i.e. we went with a pre-built binary here, but the same problem arises with a Clang+LLVM built from source.

export PATH=$LLVMDIR/bin:$PATH
export LD_LIBRARY_PATH=$LLVMDIR/lib

The most up-to-date CLang tutorial found is at https://github.com/loarabia/Clang-tutorial so we'll get that:

git clone https://github.com/loarabia/Clang-tutorial
cd Clang-tutorial
git checkout 3.1

Since the tutorial makefile expects LLVM and clang to be installed to ../cl31 we will add a symlink to make the makefile happy:

ln -s $LLVMDIR ../cl31

we run make
this fails with the following error:

clang++ -fno-rtti -c -o tutorial1.o tutorial1.cpp
tutorial1.cpp:3:10: fatal error: 'iostream' file not found
#include <iostream>
 ^
1 error generated.

So we need to add the library containing iostream to the include path
In our case that directory is /usr/include/c++/4.6.3
you can find yours as the dirname of the entry you find with with find /usr/include -name 'iostream'
We change the include path by inserting the following line after the CXXFLAGS line in the makefile:
CXXFLAGS += -I/usr/include/c++/4.6.3 so we try make again:

clang++ -I/zapps/clang+llvm-3.1-x86_64-linux-ubuntu-11.10/include -DNDEBUG -D_GNU_SOURCE -D__STDC_CONSTANT_MACROS -D__STDC_FORMAT_MACROS -D__STDC_LIMIT_MACROS -O3 -fomit-frame-pointer -fvisibility-inlines-hidden -fno-exceptions -fno-rtti -fPIC -Woverloaded-virtual -Wcast-qual -fno-rtti -I/usr/include/c++/4.6.3 -c -o tutorial1.o tutorial1.cpp
In file included from tutorial1.cpp:3:
/usr/include/c++/4.6.3/iostream:38:10: fatal error: 'bits/c++config.h' file not found
#include <bits/c++config.h>

proceeding as before, we find /usr/include -name 'c++config.h' and insert the following line in the makefile:
CXXFLAGS += -I/usr/include/c++/4.6.3/x86_64-mageia-linux-gnu
we run make again. This time, all compiling steps succeed and the errors come with the linking:

clang++ -o tutorial1 tutorial1.o -lclangRewrite -lclangFrontend -lclangEdit -lclangParse -lclangSema -lclangAnalysis -lclangAST -lclangLex -lclangBasic -lclangDriver -lclangSerialization -lLLVMMC -lLLVMSupport -L/zapps/clang+llvm-3.1-x86_64-linux-ubuntu-11.10/lib -lpthread -ldl -lm -lLLVMCppBackendCodeGen -lLLVMCppBackendInfo -lLLVMTarget -lLLVMCore -lLLVMMC -lLLVMObject -lLLVMSupport
/usr/bin/ld: cannot find crtbegin.o: No such file or directory
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make: *** [tutorial1] Error 1

ok we need crtbegin.o which is nowhere in $LLVMDIR. We find one with find /usr/lib -name 'crtbegin.o'
in our case, at /usr/lib/gcc/x86_64-mageia-linux-gnu/4.6.3/crtbegin.o
So we'll add a softlink to that one:

ln -s /usr/lib/gcc/x86_64-mageia-linux-gnu/4.6.3/crtbegin.o

and re-run make:

clang++ -o tutorial1 tutorial1.o -lclangRewrite -lclangFrontend -lclangEdit -lclangParse -lclangSema -lclangAnalysis -lclangAST -lclangLex -lclangBasic -lclangDriver -lclangSerialization -lLLVMMC -lLLVMSupport -L/zapps/clang+llvm-3.1-x86_64-linux-ubuntu-11.10/lib -lpthread -ldl -lm -lLLVMCppBackendCodeGen -lLLVMCppBackendInfo -lLLVMTarget -lLLVMCore -lLLVMMC -lLLVMObject -lLLVMSupport
/usr/bin/ld: cannot find -lstdc++
/usr/bin/ld: cannot find -lgcc
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make: *** [tutorial1] Error 1

So now we're looking for library files named like libstdc++.* and libgcc.*
With find /usr/lib -iname 'libstdc++*' and find /usr/lib -iname 'libgcc*'
we find a libstdc++.so and a libgcc.a both in directory
/usr/lib/gcc/x86_64-mageia-linux-gnu/4.6.3
so we insert the following line after the LLVMLDFLAGS line in the makefile:
LLVMLDFLAGS +=-L/usr/lib/gcc/x86_64-mageia-linux-gnu/4.6.3
we try make again:

clang++ -o tutorial1 tutorial1.o -lclangRewrite -lclangFrontend -lclangEdit -lclangParse -lclangSema -lclangAnalysis -lclangAST -lclangLex -lclangBasic -lclangDriver -lclangSerialization -lLLVMMC -lLLVMSupport -L/zapps/clang+llvm-3.1-x86_64-linux-ubuntu-11.10/lib -lpthread -ldl -lm -lLLVMCppBackendCodeGen -lLLVMCppBackendInfo -lLLVMTarget -lLLVMCore -lLLVMMC -lLLVMObject -lLLVMSupport -L/usr/lib/gcc/x86_64-mageia-linux-gnu/4.6.3
/usr/bin/ld: cannot find crtend.o: No such file or directory
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make: *** [tutorial1] Error 1

So we proceed with crtend.o just as above with crtbegin.o and add a softlink:
ln -s /usr/lib/gcc/x86_64-mageia-linux-gnu/4.6.3/crtend.o
Current problem:
And we run make once more:

/zapps/clang+llvm-3.1-x86_64-linux-ubuntu-11.10/lib/libclangSema.a(SemaExprObjC.o): In function `checkCocoaAPI(clang::Sema&, clang::ObjCMessageExpr const*)':
/home/nikola/final/llvm.src/tools/clang/lib/Sema/SemaExprObjC.cpp:(.text+0x9daa): undefined reference to `clang::edit::rewriteObjCRedundantCallWithLiteral(clang::ObjCMessageExpr const*, clang::NSAPI const&, clang::edit::Commit&)'
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make: *** [tutorial1] Error 1

This time we're in some deeper shit:
grep rewriteObjCRedundantCallWithLiteral $LLVMDIR/lib/* gives us only the names of 3 matching files:
libclangEdit.a , libclangSema.a and libclang.so
Since the first two of those are already linked in via the CLANGLIBS variable in the makefile (and the second is the one that asks for that symbol), our only hope is for that symbol to be defined in libclang.so . So we insert another line after the CLANGLIBS in the makefile:
CLANGLIBS += -lclang
and try make again.
Unfortunately, that doesn't resolve the missing symbol.
for reference, the final makefile with modifications:

CXX := clang++
LLVMCOMPONENTS := cppbackend
RTTIFLAG := -fno-rtti
LLVMCONFIG := ../cl31/bin/llvm-config

CXXFLAGS := $(shell $(LLVMCONFIG) --cxxflags) $(RTTIFLAG)
CXXFLAGS += -I/usr/include/c++/4.6.3
CXXFLAGS += -I/usr/include/c++/4.6.3/x86_64-mageia-linux-gnu
LLVMLDFLAGS := $(shell $(LLVMCONFIG) --ldflags --libs $(LLVMCOMPONENTS))
LLVMLDFLAGS += -L/usr/lib/gcc/x86_64-mageia-linux-gnu/4.6.3
DDD := $(shell echo $(LLVMLDFLAGS))
SOURCES = tutorial1.cpp \
 tutorial2.cpp \
 tutorial3.cpp \
 tutorial4.cpp \
 tutorial6.cpp \
 CItutorial1.cpp \
 CItutorial2.cpp \
 CItutorial3.cpp \
 CItutorial4.cpp \
 CItutorial6.cpp \
 CIBasicRecursiveASTVisitor.cpp \
 CIrewriter.cpp

OBJECTS = $(SOURCES:.cpp=.o)
EXES = $(OBJECTS:.o=)
CLANGLIBS = \
 -lclangRewrite \
 -lclangFrontend \
 -lclangEdit \
 -lclangParse \
 -lclangSema \
 -lclangAnalysis \
 -lclangAST \
 -lclangLex \
 -lclangBasic \
 -lclangDriver \
 -lclangSerialization \
 -lLLVMMC \
 -lLLVMSupport \
 
CLANGLIBS += -lclang
all: $(OBJECTS) $(EXES)

%: %.o
 $(CXX) -o $@ $< $(CLANGLIBS) $(LLVMLDFLAGS)

clean:
 -rm -f $(EXES) $(OBJECTS) *~


how we installed LLVM+Clang:
For information, the system on which it was installed is a x86_64 Linux box (Distribution is Mageia 2)
Try 1: from source (installation works, but building tutorials doesn't work)
We basically follow http://clang.llvm.org/get_started.html except that we use the 3.1 release tarballs instead of svn

export LLVMDIR=/home/LLVMCustomPrefix
cd $LLVMDIR
wget http://llvm.org/releases/3.1/llvm-3.1.src.tar.gz
wget http://llvm.org/releases/3.1/clang-3.1.src.tar.gz
# wget http://llvm.org/releases/3.1/compiler-rt-3.1.src.tar.gz
tar xzf llvm-3.1.src.tar.gz
tar xzf clang-3.1.src.tar.gz
mv clang-3.1.src llvm-3.1.src/tools/clang
mkdir build
cd build
../llvm-3.1.src/configure --prefix=$LLVMDIR
make -j2
make install

Try 2: use precompiled LLVM+Clang instead of building it (same result):
we downloaded a binary tarball, extracted it and then adjusted the LLVMDIR variable like this:

export LLVMDIR=/zapps/clang+llvm-3.1-x86_64-linux-ubuntu-11.10
# that's where we extracted clang+llvm-3.1-x86_64-linux-ubuntu-11.10.tar.bz2

# we don't take this one because it needs a glibc newer than what's installed on our system:
# export LLVMDIR=/zapps/clang+llvm-3.1-x86_64-linux-ubuntu_12.04




More information about the cfe-dev mailing list