How to run LLVM interpreter with a shared library? - llvm

I have mylib.c file which has some functions. I want to use those functions from my .c file as external ones in compiled llvm code. I'm playing with LLVM interpreter (lli-4.0) and I wonder how can I tell lli to use functions from my .c file?

lli has a -load argument so you compile your C file to a dynamic library and then just do
lli -load path-to-your-dynamic-library ....

lli supports the following args,
-extra-module for loading bitcode modules
-extra-object for loading object files
-extra-archive for loading static libs.

Related

How to link multiple LLVM IRs into one .so file?

I have three files as follows:
Counter.cpp
Counter.h
native-lib.cpp
These three files are jni code.
I want to convert the cpp file into an LLVM IR file first, and then make some modifications on the LLVM IR file.
Finally, the modified IR file is generated into a .so file.
What LLVM command can do the above mentioned?

issue with linking c/c++

I have a bit of an issue in static linking. I have a static library (libkells.lib) which is made up of a header file containing function declarations and a .cpp file containing the function implementations. I successfully compiled the two into an .o file and then built the static library out of them.
Then I have a file mcmd.cpp which calls the functions in the .lib file. I have included the header file involved in the static library into this mcmd.cpp file. This file (mcmd.cpp) successfully compiles into an .o file but when I try to build it into an executable file, my compiler returns a message like this:
libkells.lib(libkells.o): In function ZNKSt13move_iteratorIPSsE4baseEv
. Undefined reference to __cxa_end_catch, Undefined reference to
__cxa_begin_catch
and so many other errors. When I look keenly at these error messages, these errors seem to originate from some header file called include/c++/bits/stl_iterator.h. I'm using mingw 4.7.1 on Windows. What is it that I'm not doing right?
You are not linking against libstdc++ or not using g++, in short the 'gcc' driver, being the C driver, not the C++ driver, doesn't link the C++ runtime at the end. If you want, you can explicitly add -lstdc++ to the command line, or, definitely the first choice in general, just use 'g++'

c++ /ubuntu about .a and .so

HI,
I would like to ask what is better to use: the .a or the .so file? If i have for example an .so(or .a) and the header.h file can i use these 2 in a test.cpp code without needing the header.cpp? Can i go on a computer just with these 2 and create my own test program?
Secondly I would like to ask if .a is better to use what is the command in ubuntu for creating the .a file? And one more question. Do I necessarly need to use extern "C" class* object() {return new class} in the header.cpp. Or can i use extern"C" in the test.cpp code after i include the .a or .so and the .h file? THX!
The compile command is: g++ test.cpp -o test -ldl (//i dont want to include header.cpp here in the command line when i compile the test.cpp code
.a and .so serve different purposes. One is for statically linking the other one is for shared linking to your library. Search for these terms and you should get a fair amount of answers.

how use dll files in C++?

I need generate Dll file from matlab cods and use this in C++. I can not couple dll file in c++. Please help me.
This looks promising: How do I create a C - shared library with MATLAB Compiler 3.0 which can be used in other projects?
In short:
Compile your MATLAB files into a DLL (on Windows): mcc -t -L C -W lib:mylib -T link:lib -h <MATLAB files> libmmfile.mlib
Add mylib.lib to your MSVC (or your own IDE) project
Make sure to call the initialization and termination routines from your
code before calling any of the MATLAB files that are compiled. You need to call: mylibInitialize();
Afterwards, you should call the termination routine: mylibTerminate();
All of the symbols in mylib.dll will also appear in mylib.h.
You can call the functions compiled from the MATLAB code by invoking mlfFoo(...), from your C code.

Is it possible to read an LLVM bitcode file into an llvm::Module?

I'm writing a compiler with LLVM. Each source file is compiled into an LLVM bitcode file. Eventually the linker links and optimizes all the bitcode files into one final binary.
I need a way to read the bitcode files in the compiler in order to access the type information. The LLVM documentation shows a class called BitcodeReader, but that appears to be internal to LLVM.
Is there any publicly accessible way to read a bitcode file into an llvm::Module?
I looked through the source to the llvm-dis tool and found the function I was looking for:
Module *ParseBitcodeFile(MemoryBuffer *Buffer, LLVMContext& Context,
std::string *ErrMsg = 0);
from llvm/Bitcode/ReaderWriter.h.