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

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?

Related

compile a cpp file with multiple different header files

I have a single cpp file and multiple hpp files. For each hpp file, I want to create an executable using the same cpp file. The header files have different names. Can I do this in CMake?
So I have
source_1.cpp
header_1.hpp
header_2.hpp
...
and I want to create
executable_1
executable_2
...
The C++ preprocessor can use macros for #include.
That is, you can have something like:
#include HEADER_FILE_TO_INCLUDE
Then when building the source file you could define the macro on the command-line:
g++ -DHEADER_FILE_TO_INCLUDE="\"header_1.hpp\"" source_1.cpp
To do this with CMake you first of all need multiple executable targets, where you specify the same source file for each target.
Then you can use target_compile_definitions to specify the macro and the header file to use.
Something like
add_executable(executable_1 source_1.cpp)
target_compile_definitions(executable_1 HEADER_FILE_TO_INCLUDE="header_1.hpp")
add_executable(executable_2 source_1.cpp)
target_compile_definitions(executable_2 HEADER_FILE_TO_INCLUDE="header_2.hpp")
If all header files are named header_X.hpp, with X just being a sequence number, then you could easily create a loop from 1 to the max value of header file numbers.

How to run LLVM interpreter with a shared library?

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.

Matlab C++ S-Function with multiple source files

I have a simulink model with a c++ s-function.
This s-function needs access to a lot of (>50) classes. Each class is consists of a header (.h) and a source (.cpp) file.
I have also divided my code into multiple directories:
root
-sfun.cpp
-folder1
--file1.h
--file1.cpp
--file2.h
--file2.cpp
-folder2
--file3.h
--file3.cpp
...
For the compilation of the s-function I am using the mex-function:
mex -Ifolder1 -Ifolder2 -outdir bin sfun.cpp folder1/file1.cpp folder1/file2.cpp folder1/file3.cpp
(http://de.mathworks.com/help/matlab/ref/mex.html)
But this gets very long and ugly with more and more files because I need to specify each header folder and earch source file separately.
Is there a better way to create a mex file that needs access to lots of source files?
I have the following ideas, but I am not sure what could be the correct and easiest way:
Add all header and source files (fileX.h/ fileX.cpp) to an visual studio project and compile them to a *.lib file. Then compile only the sfun.cpp with the mex tool and provide access to the *.lib file
Move all header and source files into one directory. This would shorten the command line as follows:
mex -outdir bin sfun.cpp file1.cpp file2.cpp file3.cpp
Making everything inline so that there is no need for a source file. (very ugly solution)
Is there some kind of a makefile for the mex compiler?
Include not only the header files but also the source files via a #include directive.
At the moment I am not convinced of any of these ideas and I would appreciate any help.
Thanks
Edit1:
One annotation: This project should be ported to a dspace pc in a later stage. Do I need to consider something special in this case?

Library file placement in C++

I am trying to use the dynamic bitset provided by boost libraries. Downloaded the file boost_1_55_0.tar.bz2 and extracted it into a folder named boost. In this folder I have put my source code file with the #include directive #include <boost/dynamic_bitset.hpp>, when I compile the source code, the compiler returns "No such file or directory". Where to place the source code?
I managed to compile the program by putting the source code file in the the same directory that boost is in, and compiling using the command formula:
g++ -I /your/source/root /your/source/root/A/code.cpp
As mentioned in the How to make g++ search for header files in a specific directory?

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.