Linking against clang-llvm - c++

I've been working on a small tool with clang/llvm but I haven't been able to successfully get g++ and gnu's linker to properly link my code against clang.
my linker is generating the following errors:
undefined reference to `clang::FileManager::~FileManager()'
undefined reference to `clang::FileManager::FileManager()'
undefined reference to `llvm::sys::getHostTriple()'
undefined reference to `clang::Diagnostic::Diagnostic(clang::DiagnosticClient*)'
undefined reference to `llvm::outs()'
undefined reference to `clang::TargetInfo::CreateTargetInfo(clang::Diagnostic&, clang::TargetOptions&)'
undefined reference to `clang::SourceManager::getOrCreateContentCache(clang::FileEntry const*)'
undefined reference to `clang::SourceManager::createFileID(clang::SrcMgr::ContentCache const*, clang::SourceLocation, clang::SrcMgr::CharacteristicKind, unsigned int, unsigned int)'
my compile commands looks like this:
g++ -g -fno-rtti -I~/llvm-2.8/tools/clang-2.8/include \
-I~/llvm-2.8/llvm/include \
`~/bin/llvm-config --cxxflags` \
-c Frontend.cpp
g++ -g -fno-rtti -I~/llvm-2.8/tools/clang-2.8/include \
-I~/llvm-2.8/llvm/include \
`~/bin/llvm-config --cxxflags` \
-c exec.cpp
g++ -I~/llvm-2.8/tools/clang-2.8/include \
-I~/llvm-2.8/llvm/include -L~/opt/lib/ \
-g -fno-rtti -lclangDriver -lclangAnalysis \
-lclangFrontend -lclangSema -lclangAST -lclangParse \
-lclangLex -lclangBasic \
`~/bin/llvm-config --cxxflags --ldflags --libs` \
Frontend.o exec.o -o run
any tips or advice would be welcomed.
cheers,
ct
PS: I've been exploring some of the information on this page:
http://ubuntuforums.org/showthread.php?t=532693
and it might do the trick, will post a comment on that tip when I can.
Solution
using clang code from this tutorial (which had to be modified to remove the references to FileSystemOptions b/c clang/Basic/FileSystemOptions.h doesn't exist in clang-2.8): http://clangtutorial.codeplex.com/
g++ tutorial1.cpp -g -fno-rtti -lclangFrontend -lclangDriver \
-lclangCodeGen -lclangSema -lclangChecker -lclangAnalysis \
-lclangRewrite -lclangAST -lclangParse -lclangLex -lclangBasic \
-lLLVMSupport -lLLVMSystem -I~/opt/include/ \
`llvm-config --cxxflags --ldflags --libs all`
seemed to do the trick!

When I've built some stuff against llvm / clang, this is what I've used to build it. Perhaps you can compare the two build lines.
Also, the llvm-config command I've used has been: llvm-config --cxxflags --ldflags --libs backend.
Finally, this is likely partially related to an ordering issue. You probably want to include the libraries for llvm before you include the clang libraries.
/usr/bin/g++ \
-fno-exceptions -fno-rtti -fno-common \
-I/Users/wlynch/Homebrew/include \
-DNDEBUG -D_GNU_SOURCE -D__STDC_LIMIT_MACROS -D__STDC_CONSTANT_MACROS \
../src/main.cpp -c -o src/main.cpp.0.o
/usr/bin/g++
src/main.cpp.0.o -o /Users/wlynch/Dropbox/Clang/Indexer/build/main \
-L/Users/wlynch/Homebrew/lib -L/Users/wlynch/Homebrew/lib \
-lpthread -lm \
-lLLVMX86Disassembler -lLLVMX86AsmParser -lLLVMX86CodeGen -lLLVMSelectionDAG \
-lLLVMAsmPrinter -lLLVMMCParser -lLLVMCodeGen -lLLVMScalarOpts -lLLVMInstCombine \
-lLLVMTransformUtils -lLLVMipa -lLLVMAnalysis -lLLVMTarget -lLLVMCore \
-lLLVMX86AsmPrinter -lLLVMMC -lLLVMX86Info -lLLVMSupport -lLLVMSystem \
-lclangAST -lclangAnalysis -lclangBasic -lclangChecker -lclangCodeGen \
-lclangDriver -lclangFrontend -lclangFrontendTool -lclangIndex -lclangLex \
-lclangParse -lclangRewrite -lclangSema -lclangSerialization

I assume you have back quotes around ~/bin/llvm-config, right?
That being said, move the -l options and the
`~/bin/llvm-config --cxxflags --ldflags --libs`
after the .o files on the command line. Stuff won't be taken out of the libraries unless referenced by a preceding object file.

Related

Is it possible to use other C++ standard library implemention, Libc++ on MQX

I am new to MQX. The MQX I am using has its own standard library implementation. It does not support std::shared_ptr nor std::unique_ptr.
Is anyone successfully using another version (e.g the gnu version libc++) of STL instead of embedded warrior library on MQX 4.11?
Edit 1
Added more details about what I am trying to do
Currently, the project is using GNU Arm Embedded Toolchain (gcc-arm-none-eabi-5_4-2016-q2) but instead of using gnu standard library, the project is using EWL(embedded warrior library) which seems to stop updating for a very long time. So I am trying to use the gnu library to replace the EWL.
In the makefile, it has something like:
CPP_FLAGS += -mcpu=cortex-m4 -mthumb -mfloat-abi=hard -mfpu=fpv4-sp-d16 -fsingle-precision-constant -foptimize-sibling-calls\
-fno-strict-aliasing \
-g2 -gdwarf-2 -gstrict-dwarf -std=gnu++14 -Wall -Wextra -Woverloaded-virtual -Werror -Wcast-align -Wfloat-equal \
-Wformat-extra-args -Wformat -Wno-error=deprecated-declarations \
-specs=../ewl_c++.specs -fdiagnostics-show-option \
-Wno-missing-field-initializers \
-Wno-unused-function \
-Wno-long-long \
-isystem$(MQX_lib)/bsp/Generated_Code \
-isystem$(MQX_lib)/bsp \
-isystem$(MQX_lib)/psp \
-isystem$(MQX_lib) \
-isystem$(MQX_lib)/shell \
-isystem$(MQX_lib)/mfs \
-isystem$(MQX_lib)/rtcs \
-isystem$(MQX_lib)/usb \
-isystem$(CLARINOX_dir) \
-isystem$(CLARINOX_dir)/Source \
-isystem$(MQX_lib)/EWL/ARM_GCC_Support/ewl/EWL_C/include \
-isystem$(MQX_lib)/EWL/ARM_GCC_Support/ewl/EWL_C++/include \
-isystem$(MQX_lib)/EWL/ARM_GCC_Support/ewl/EWL_Runtime/include \
$(INCLUDES) \
$(BUILD_DEFINES) \
-ffunction-sections -fdata-sections -fconstexpr-depth=4096 -mlong-calls -fno-exceptions \
DHAVE_MQX\
-D__VFPV4__=1 -D_DEBUG=1 -c -fmessage-length=0 -D__CC_ARM
ARFLAGS = rc
LINK_FLAGS = \
-mcpu=cortex-m4 -mthumb -mfloat-abi=hard -mfpu=fpv4-sp-d16 \
-g2 -gdwarf-2 -gstrict-dwarf -mlong-calls -fno-exceptions \
-Wl,-T../intflash_sramdata.ld \
-Xlinker --gc-sections \
-L$(MQX_lib)/EWL/ARM_GCC_Support/ewl/lib/armv7e-m/fpu
-L$(MQX_lib)/EWL/ARM_GCC_Support/ewl/lib/armv7e-m
-L$(MQX_lib)/bsp/Generated_Code \
-L$(MQX_lib)/bsp \
-L$(MQX_lib)/psp \
-L$(MQX_lib)/shell \
-L$(MQX_lib)/mfs \
-L$(MQX_lib)/rtcs \
-L$(MQX_lib)/usb \
Since I am trying to use the gnu standard library, I added
GCC_541=/opt/gcc-arm-none-eabi-5_4-2016q2/lib/gcc/arm-none-eabi/5.4.1
at the beginning of the makefile and replace the EWL includes in the CPP_FLAGS block
-isystem$(MQX_lib)/EWL/ARM_GCC_Support/ewl/EWL_C/include \
-isystem$(MQX_lib)/EWL/ARM_GCC_Support/ewl/EWL_C++/include \
-isystem$(MQX_lib)/EWL/ARM_GCC_Support/ewl/EWL_Runtime/include \
by
-isystem(GCC_541)/include \
-isystem(GCC_541)/include-fixed \
In the LINK_FLAGS block, I replaced the
-L$(MQX_lib)/EWL/ARM_GCC_Support/ewl/lib/armv7e-m/fpu
-L$(MQX_lib)/EWL/ARM_GCC_Support/ewl/lib/armv7e-m
by
-L$(GCC_541)/armv7e-m/fpu \
-L$(GCC_541)/armv7e-m \
After rebuilding the project I got a compile error
Compiling [../../source/TestApps/SomeSrcFile.cpp to obj/SomeSrcFile.obj]
/bin/sh: 1: Syntax error: "(" unexpected
../common.mk:1804: recipe for target 'obj/SomeSrcFile.obj' failed
make[1]: *** [obj/SomeSrcFile.obj] Error 2
I am thinking I might miss something, any suggestions? Thanks

What is the proper way of building a static binary with openssl?

I have the following make file:
CC=g++
CFLAGS=-c -Wall
REST_LIBS = -lssl -lcrypto -lboost_system -lcpprest
all: main
main: static_pack
g++ -std=c++14 -D DEBUG -Wfatal-errors -static -pthread -I$(basepath)/vendors/cpp-jwt/include -I$(basepath)/vendors/json/include \
-DTS=\"/ctts.json\" \
-DCS_PATH=\"/bin\" \
-DCTFS_ENC=\"/ctfs.enc\" \
-DUNTAR_PATH=\"/\" \
-DCLUSTER_PATH=\"/.clusters\" \
-o run main.cpp \
libmain.a && \
rm -rf debpkg/cs/usr/bin/cs debpkg/cs.deb && \
cp run debpkg/cs/usr/bin/cs && \
dpkg-deb -b debpkg/cs && \
mv debpkg/cs.deb .
static_pack: rest.o aes.o random.o
ar rcs libmain.a random.o aes/aes.o rest/rest.o
rest.o:
g++ -std=c++14 -Wfatal-errors -c $(REST_LIBS) -o rest/rest.o rest/rest.cpp
aes.o: random.o
g++ -std=c++14 -D DEBUG -Wfatal-errors -c -lcrypto -o aes/aes.o random.o aes/aes.cpp
random.o:
g++ -std=c++14 -Wfatal-errors -c -o random.o random.cpp
If I compile this to be dynamically linked I have no problems. However, when I try static compilation I get tons of errors such as:
aes.cpp:(.text+0x706): undefined reference to `EVP_DecryptInit_ex'
aes.cpp:(.text+0x732): undefined reference to `EVP_DecryptUpdate'
aes.cpp:(.text+0x763): undefined reference to `EVP_CIPHER_CTX_ctrl'
aes.cpp:(.text+0x792): undefined reference to `EVP_DecryptFinal_ex'
aes.cpp:(.text+0x7a1): undefined reference to `EVP_CIPHER_CTX_free'
Essentially none of the symbols are being found. I'm not sure what I need to do now. I've tried build my object files as static to but that fails. I've looked into linking order, but that seems right.
My question boils down to two things:
When static linking other objects, do those objects need to be statically compiled as well + archived?
What is wrong with my setup?
You don't need REST_LIBS for your rest.o rule, as it only compiles a source file. You need to pass those libraries to g++ in main rule - as part of it, g++ will call linker.
Ok so apparently linking happens in the opposite order than I thought... and I think it's possible I might have not been linking either originally as a fufu mistake.
REST_LIBS = -lboost_filesystem -lboost_system -lcpprest -lssl -lcrypto -ldl
# /usr/local/lib/libyaml-cpp.a
all: main
main: static_pack
g++ -std=c++14 -D DEBUG -Wfatal-errors -I$(basepath)/vendors/cpp-jwt/include -I$(basepath)/vendors/json/include \
-DTS=\"/ctts.json\" \
-DCS_PATH=\"/bin\" \
-DCTFS_ENC=\"/ctfs.enc\" \
-DUNTAR_PATH=\"/\" \
-DCLUSTER_PATH=\"/.clusters\" \
-o run main.cpp \
libmain.a $(REST_LIBS) -pthread && \
rm -rf debpkg/cs/usr/bin/cs debpkg/cs.deb && \
cp run debpkg/cs/usr/bin/cs && \
dpkg-deb -b debpkg/cs && \
mv debpkg/cs.deb .

Undefined symbols for architecture x86_64: "std::terminate()", when building kaleidoscope llvm

I'm doing the kaleidoscope tutorial. I'm on step two.
https://github.com/westymatt/creole
But I get this error when building with clang++
clang++ -Wno-c++11-extensions -g -std=c++11 -I/usr/local/Cellar/llvm/3.6.1/include -D__STDC_CONSTANT_MACROS -D__STDC_FORMAT_MACROS -D__STDC_LIMIT_MACROS src/lexer.cc src/parser.cc -L/usr/local/Cellar/llvm/3.6.1/lib/ -lLLVMX86Disassembler -lLLVMX86AsmParser -lLLVMX86CodeGen -lLLVMSelectionDAG -lLLVMAsmPrinter -lLLVMCodeGen -lLLVMScalarOpts -lLLVMProfileData -lLLVMInstCombine -lLLVMTransformUtils -lLLVMipa -lLLVMAnalysis -lLLVMTarget -lLLVMX86Desc -lLLVMObject -lLLVMMCParser -lLLVMBitReader -lLLVMMCDisassembler -lLLVMX86Info -lLLVMX86AsmPrinter -lLLVMMC -lLLVMX86Utils -lLLVMCore -lLLVMSupport -lc++ -O0 -o creole
Undefined symbols for architecture x86_64:
"std::terminate()", referenced from:
___clang_call_terminate in lexer-608bbc.o
___clang_call_terminate in parser-09b617.o
ld: symbol(s) not found for architecture x86_64
std::terminate is located inside libc++ on OSX (which I assume you're using because of the "Cellar" in your path there). You appear to be explicitly linking against libc++ which means that the ordering is likely going wrong on your link line.
I can't duplicate this using the actual tutorial sources from top of tree (I don't have 3.6.1 checked out), but I'd suggest you follow the example Makefiles in there. The link line for a given part of the tutorial looks like:
clang++ -Wl,-dead_strip -rdynamic -Wl,-rpath -Wl,#executable_path/../lib -L/Users/echristo/builds/build-llvm/Debug+Asserts/lib -L/Users/echristo/builds/build-llvm/Debug+Asserts/lib -m64 -o /Users/echristo/builds/build-llvm/Debug+Asserts/examples/Kaleidoscope-Ch4 /Users/echristo/builds/build-llvm/examples/Kaleidoscope/Chapter4/Debug+Asserts/toy.o \
-lLLVMX86Disassembler -lLLVMX86AsmParser -lLLVMX86CodeGen -lLLVMSelectionDAG -lLLVMAsmPrinter -lLLVMCodeGen -lLLVMScalarOpts -lLLVMProfileData -lLLVMInstCombine -lLLVMInstrumentation -lLLVMTransformUtils -lLLVMipa -lLLVMX86Desc -lLLVMMCDisassembler -lLLVMX86Info -lLLVMX86AsmPrinter -lLLVMX86Utils -lLLVMMCJIT -lLLVMExecutionEngine -lLLVMTarget -lLLVMAnalysis -lLLVMRuntimeDyld -lLLVMObject -lLLVMMCParser -lLLVMBitReader -lLLVMMC -lLLVMCore -lLLVMSupport -lz -lpthread -ledit -lcurses -lm
which should give you an idea of what it'll look like.
From looking at your sources on github it looks like you've gone to a "include the output of llvm-config on the command line" which isn't very reliable as components may change, etc.
clang++ -g -O3 toy.cpp `llvm-config --cxxflags --ldflags --system-libs --libs core` -o toy
from the tutorial should be enough to compile your simple example. Just replace toy.cpp with both of your example files since you split it up.
I got this error, while I was linking my code with my static lib built with ARC but there were some Objective C file with .mm extension. When I rename them with .m, worked fine.
According to this you need to make sure your implementations for your functions all have corresponding declarations.

linking clang lib allways undefined symbol

I have created an some classes I want to use with swig in order to generate ruby binding.
Everything is ok when I generate the code or compile the ruby module. But when a script load this module, there is an error:
undefined symbol: _ZTVN5clang5LexerE
I understand that this means that the problem is for the clang::Lexer. But I know I have
set the lclangLex lib for this.
here is the command I use in order to link the objects files:
clang++ -shared -o parser.so parser.o Declarations.o -L. -L/usr/lib -L. -Wl,-O1,\
--sort-common,--as-needed,-z,relro -fstack-protector -rdynamic -Wl,-export-dynamic \
-L/usr/lib -lz -lpthread -lffi -lcurses -ldl -lm -lruby -lclangLex -lclangAST \
-lpthread -lgmp -ldl -lcrypt -lm -lc -lLLVMCppBackendCodeGen -lLLVMCppBackendInfo \
-lLLVMTarget -lLLVMCore -lLLVMMC -lLLVMObject -lLLVMSupport
any idea ?
The problem was the order of the libs given to the linker:
-lclangAST -lclangLex -lclangBasic
lclangAST must be given before lclangLex then I just had to add lclangBasic and everything works.

Proper Way to Link with clang++ and scons

I'm trying to adapt an existing scons build system to use clang++ instead of g++. When using -O0 or -O2 -- neither of which output llvm IR -- things work swimmingly, that is to say, the build complete without errors. When using -O4, which compiles to llvm bytecode to allow link-time-optimization, the build fails on the final link.
I've searched various places, but I am unclear on exactly how to fix this, either directly on the command line or, knowing that in my SConstruct file.
Note: Setting env['LINK'] = "/path/to/llvm-link" causes all of my dependency checks to fail.
Note: Adding -Xlinker "-plugin" -Xlinker "/usr/lib/LLVMgold.so" to the command works well. So, the question is how to add this conditioned upon the choice of clang in SConstruct.
$ /usr/sbin/distcc clang++ -o build/release/filex.o -c -std=c++11 -march=core-avx-i -W -Wall -O4 \
-DHAVE_LLVM=0x0303 -D_GNU_SOURCE -D__STDC_CONSTANT_MACROS -D__STDC_FORMAT_MACROS \
-D__STDC_LIMIT_MACROS -D_GNU_SOURCE=1 -D_REENTRANT -DHAVE_CONFIG_H -D_X11 \
-DFIFODIR='"/var/run/dir"' -I. -Isrc -I/usr/include -I/usr/include/SDL src/filex.cpp
...
$ ar rc build/release/libfile2.a build/release/filea.o build/release/tools/fileb.o
$ ranlib build/release/libfile2.a
...
$ /usr/sbin/distcc clang++ -o mybinary -pthread build/release/file1.o \
build/release/libfile2.a ... -L/usr/lib -lLLVMBitWriter -lLLVMX86Disassembler \
-lLLVMX86AsmParser -lLLVMX86CodeGen -lLLVMSelectionDAG -lLLVMAsmPrinter -lLLVMMCParser \
-lLLVMX86Desc -lLLVMX86Info -lLLVMX86AsmPrinter -lLLVMX86Utils -lLLVMJIT \
-lLLVMRuntimeDyld -lLLVMExecutionEngine -lLLVMCodeGen -lLLVMObjCARCOpts -lLLVMScalarOpts \
-lLLVMInstCombine -lLLVMTransformUtils -lLLVMipa -lLLVMAnalysis -lLLVMTarget -lLLVMMC \
-lLLVMObject -lLLVMCore -lLLVMSupport -lz -lffi -ldl -lm -lboost_iostreams -lSDL \
-lSDL_net -lpthread -lboost_system -lpangocairo-1.0 -lpango-1.0 -lgobject-2.0 -lglib-2.0 \
-lcairo -lfontconfig -lfreetype -lboost_program_options -lboost_regex -lSDL_ttf \
-lSDL_mixer -lvorbisfile -lSDL_image -lX11
/usr/sbin/ld.gold: error: build/release/file1.o:1:3: invalid character
/usr/sbin/ld.gold: error: build/release/file1.o:1:3: syntax error, unexpected $end
/usr/sbin/ld.gold: error: build/release/flie1.o: not an object or archive
The key is to append -Wl,-plugin,/path/to/LLVMgold.so to LINKFLAGS rather than the -Xlinker... syntax.