How compile a llvm code file using functions from C? - c

Is there a way for compile a file in llvm (*.ll) that uses functions in C?
I created a test using check C and I compile it as:
$clang counter_i.c counter_test_check.c -lcheck
This way, I am using the libraries from check, but I need produce the llvm code that uses the library from check. When I try this command:
$clang -S -emit-llvm counter_i.c counter_test_check.c
and try execute the code:
$lli-mp-3.5 counter_test_check.ll
I receive this answer:
LLVM ERROR: Program used external function 'srunner_create' which could not be resolved!
I think that a solution is do something as:
$clang -S -emit-llvm counter_i.c counter_test_check.c -lcheck
But it is not supported.
I am thinking that a similar answer is available at:LLVM JIT-compiled program cannot find external functions

Yes, LLVM has a C interface (although there may be some limitations compared to the C++ API):
http://llvm.org/docs/doxygen/html/group__LLVMC.html

I found a solution with:
clang -S -emit-llvm -c counter_test_check.c counter_i.c
clang -o executable counter_test_check.ll counter_i.ll -lcheck
./executable
It does the compilation in two steps and this way I can use other llvm source file.

Related

Compiling cmocka on windows

I'm trying to compile a simple unit test on my windows machine.
When I'm trying to compile my test I'm using the shared library flag.
gcc -c -L./bin/ -lcmocka .\Test.c .\src\some_module.c
gcc .\Test.o .\some_module.o -o main
But the second line throws this error:
undefined reference to `_cmocka_run_group_tests'
However, if I'm compiling using directly the cmocka.c file which I downloaded from their git it works fine:
gcc -c .\lib\cmocka.c .\Test.c .\src\some_module.c
gcc .\Test.o .\some_module.o .\cmocka.o
What am I doing wrong in the first compilation?
In addition, I would happy to understand the difference between the two compilations. Which one is the better practice?
Thank you
In order to compile your code, the compiler does not need to know where to look for the library. It's enough if the compiler "finds" the declarations of the functions which are usually in the header files provided by the library.
This step is done in the first line of your compilation procedure (maybe you need to specify the folder to the header files by adding -Ipath/to/headers/):
gcc -c .\Test.c .\src\some_module.c
The library itself is "combined" with your code during the linking step, which is done during your second compilation step. Here you need to specify the library (and its path via -Lpath/to/library, if the linker does not find the library on its own):
gcc .\Test.o .\some_module.o -o main -L./bin/ -lcmocka
You should definitely not use your second approach and compile the library by yourself.

Clang: compile IR, C files and apply opt in one line

I'm building an IR level Pass for LLVM which instrument the functions with calls to my runtime library.
So far I have used the following lines to compile any C file with my pass and link it with the runtime library and guaranteeing that the runtime library function calls are inlined.
Compiling source to IR...
clang -S -emit-llvm example.c -o example-codeIR.ll -I ../runtime
Running Pass with opt...
opt -load=../build/PSS/libPSSPass.so -PSSPass -overwrite -always-inline -S -o example-codeOpt.ll example-codeIR.ll
Linking IR with runtime library...
llvm-link -o example-linked.bc example-codeOpt.ll ../runtime/obj/PSSutils.ll
Compiling bitcode to binary...
clang -ldl -O3 -o example example-linked.bc ../initializer/so/shim.so
Now I would like to test my pass with the LLVM testsuite and the only thing I can do is pass flags to the test suite. I can't control the steps of of compilation and generate so many files for each test case.
Is there a way to do the same as above without having to save intermediate files and yet keep the order of the steps?
I have tried the following:
clang -ldl -Xclang -load -Xclang ../build/PSS/libPSSPass.so ../initializer/so/shim.so ../runtime/obj/PSSutils.ll $<
But I ran into the problem that I can't compile both IR and .c files.
If I compile the runtime library to be an object file the functions in it will not get inlined anymore which is the main goal of the above steps.
So to Answer my question:
first of all, call to shared objects are never inlined. hence, the above mentioned shared objects should be compiled to objects instead. The -flto=thin flag should be used when compiling the objects to build a summary of the functions so the linker can perform link time optimizations.
And in the final step of compiling the target you will need to also compile it with -flto=thin flag and the compiler will do the magic for you.

Get preprocessor output from C through MATLAB mex

I want to get the preprocessor output when compiling my c-code through mex of MATLAB using MinGW64 Compiler (C) so using gcc (right?).
From this post I got that you can do this with pure gcc passing the option -E to gcc.
However I installed gcc through MATLAB app and therefore cannot access it through command line (would also appreciate a command how to do that, without reinstalling MinGW64 and setting it up manually for use with MATLAB).
I tried to do the following assuming that compiler flags are the right way to pass the argument:
mex -c grampc_run.c -I../../include -I../include COMPFLAGS='$COMPFLAGS -E'
This just results in the creation of the object file.
COMPFLAGS is used by the MSCV compiler. The GCC compiler loos at CFLAGS and CXXFLAGS (for C and C++ compilation, respectively). See here. Thus, you should use the following syntax:
mex -c grampc_run.c -I../../include -I../include CFLAGS='$CFLAGS -E'
You might also want to add the -v option to mex. GCC puts the preprocessor output to the standard output, which mex might not show you. With -v it does show you all the output.

Parse single file from a GNU Unix utility program with clang and llvm

I want to do some program analysis project on C program. So far, I want to get one C program as the input for clang, and then I use LLVM to make some analysis.
I want to do some experiment on gnu unix utility. For example:
diff (https://www.gnu.org/software/diffutils/).
For example, when I want to do analysis on src/diff.c:
My command is: clang -emit-llvm -O0 -c diff.c -o test.bc
However I will get the below error:
./system.h:21:10: fatal error: 'config.h' file not found
which seems that clang cannot find config.h.
So my question is how to parse single program in a GNU utility?
Do I need to write a Makefile or where can I find something like config.h?

Linking after using f2c

I used f2c to translate a huge Fortran subroutine into C. The header says the following:
/* fourier.f -- translated by f2c (version 20090411).
You must link the resulting object file with libf2c:
on Microsoft Windows system, link with libf2c.lib;
on Linux or Unix systems, link with .../path/to/libf2c.a -lm
or, if you install libf2c.a in a standard place, with -lf2c -lm
-- in that order, at the end of the command line, as in
cc *.o -lf2c -lm
Source for libf2c is in /netlib/f2c/libf2c.zip, e.g.,
http://www.netlib.org/f2c/libf2c.zip
*/
I am using ubuntu 10.04. How can I link the object file with libf2c?
You would have to install the libf2c2-dev package -- but as the f2c package already depends on it, all you may need is to add -lf2c to your Makefile.
Are you compiling the resulting C file with gcc? Then add "-lf2c -lm" to the gcc compile command.
Why not compile with a Fortran compiler, such as gfortran? It's easily available for Ubuntu.
By passing -lf2c -lm to the line which will create the executable from the objects. Which compiler are you using on Ubuntu? GCC?
gcc -c fourier.c -lf2c -lm
Could be as simple as that.
Well - no direct answer to your linking problems, but:
Since you're working with Linux: Why don't you compile you fortran code as is and link it directly with the C-code? GCC can do that. Converting the code is of course doable but it is by no way required.
Nils

Resources