Using gcc to compile userspace application using linux kernel headers - c

I have a really simple c program that I want to compile using gcc, importing from linux kernel headers.
#include <stdio.h>
#include <stdlib.h>
#include <linux/random.h>
int main(){
int rand;
get_random_bytes(&rand,sizeof(rand));
printf("%d",rand);
return 0;
}
I have tried to compile this program using the following command:
gcc rand.c -D__KERNEL__ -isystem /lib/modules/`uname -r`/build/include
But I get a bunch of errors (below). What am I missing?:
/usr/src/kernels/4.9.8-201.fc25.x86_64/include/linux/linkage.h:7:25: fatal error: asm/linkage.h: No such file or directory
#include <asm/linkage.h>

From some quick Google searches, it seems like get_random_bytes might be a private function only usable from within the kernel.
How about you try using getrandom instead? Here is the documentation of getrandom:
http://man7.org/linux/man-pages/man2/getrandom.2.html

Related

Header not found error when adding stdint.h to my C code when compiling with Clang on Raspberry Pi

Here's the error that I am getting and none of the online solutions are effectively fixing the issues that I am having. Just adding #include <stdint.h> breaks the compilation of my code. I tried installing multilib but the library seems to have no support on Ubuntu. I also tried some of the compatibility libraries but to no avail.
clang -O2 -target bpf -c hello.c -o hello.o
In file included from hello.c:2:
In file included from /usr/lib/llvm-11/lib/clang/11.0.0/include/stdint.h:52:
/usr/include/stdint.h:26:10: fatal error: 'bits/libc-header-start.h' file not found
#include <bits/libc-header-start.h>
^~~~~~~~~~~~~~~~~~~~~~~~~~
1 error generated.
The code for reference. I am trying to run a compiled version of uBPF on an ARM device
#include <stdint.h>
static int idouble(int a)
{
return (a * 2);
}
int bpf_prog(void *ctx)
{
int a = 1;
a = idouble(a);
return (a);
}
This question is based on this Klyr's tutorial on how to setup user input with uBPF.
I was able to have my issue fixed actually. ARM does not have a gcc-multilib equivalent so you must install gcc-multilib-arm-linux-gnueabihf for this to work. When trying to compile and target with Clang, you must first
cd /usr/include/aarch64-linux-gnu
then
sudo cp -r . ..
It's a hacky solution but it will let you import libraries as you see fit

gmp strange behaviour that doesn't let me compile new project

I've installed on my ubuntu gmp using this command:
sudo apt-get install libgmp3-dev
and it worked fine.
Now I'm trying to create a new project put simply writing
#include "gmp.h"
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
int main(){
mpz_t num;
mpz_init(num);
printf("%s\n",mpz_get_str (NULL, 10, num));
mpz_clear(num);
return 0;
}
give me
> gcc -lgmp mil.c /tmp/ccHvV9kT.o: In function `main':
> mil.c:(.text+0x1f): undefined reference to `__gmpz_init'
> mil.c:(.text+0x35): undefined reference to `__gmpz_get_str'
> mil.c:(.text+0x49): undefined reference to `__gmpz_clear' collect2:
> error: ld returned 1 exit status
I just copy-pasted the code of my previous project and I get the same error(in all the function that I created), but compiling my old project I don't get any error.
What is my problem???
Order of arguments to gcc matters a big lot.
Try to use (you want warnings and debug info, so)
gcc -Wall -Wextra -g mil.c -lgmp -o milprog
Then run ./milprog. You may want to use the gdb debugger on it, with
gdb ./milprog
and you may want (for benchmarking purposes) to ask the compiler to optimize, by adding (before -g) something like -O2 -march=native
Learn to use GNU make (or some other build automation tool, like ninja), see this.
Be sure to use a version control system like git.
BTW, I find more logical and more elegant to include "gmp.h" after (not before, as you did) the inclusion of standard headers (like <stdio.h>).

gcc cannot compile C files using mpi.h

I just received a message where someone could not compile a C file.
When they try to compile, it's getting the following error.
$ gcc mpi01.c
mpi01.c:1:17: fatal error: mpi.h: No such file or directory
#include <mpi.h>
^
compilation terminated.
$
I'm sure the C code is present so it must be a problem with the installation, but mpi.h is there.
/opt/mpss/3.6/sysroots/k1om-mpss-linux/usr/src/kernel/drivers/message/fusion/lsi/mpi.h
/usr/include/openmpi-x86_64/mpi.h
/usr/src/kernels/3.10.0-229.20.1.el7.x86_64/include/linux/mpi.h
Does someone know what I could do?
The system is running Centos7.
Edit 1:
To respond to the answers. It is not my code that I am trying to compile. What I did to "ensure" everything is in place for C compiling:
yum install gcc openmpi kernel-devel kernel-headers openmpi-devel
I do not know if I am allowed to post the code, but the following headers are included in the code:
#include <mpi.h>
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
The compiler command line was the following:
vim mpi01.c
module load mpi
gcc -o mpi01 mpi01.c
Resulting in the error above.

"gsl/gsl_rng.h' file not found" after OSX El Capitan upgrade

I just updated to the newest OSX, El Capitan, and I am having problems with compiling a C program. It compiled fine just before the upgrade of the OS. After it I got a warning message already for my LaTeX text editor, Latexian:
Latexian message
But since I don't use preview or compilation inside the program and compile in the terminal with "latex file.tex" it works fine.
Now my problem is with my .c program which includes one of the GSL libraries, here is my header:
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <gsl/gsl_rng.h>
#include <gsl/gsl_randist.h>
When compiling I get the following:
performance.c:4:10: fatal error: 'gsl/gsl_rng.h' file not found
#include <gsl/gsl_rng.h>
^
1 error generated.
I am guessing something changed in the OSX because of these two situations but the latter is a huge problem for me since I'm finishing my thesis! Hope my question is clear, it's my first.
EDIT:
And I'm guessing this is the problem
El Capitan's System Integrity Protection will shift utilities' functions
When compiling with GCC you may have to manually specify the parent directory that contains the gsl subfolder. Similarly you will have to specify the directory to find the libraries in as well. The include directory can be added as a search path to gcc with the -I option, and the library search path with -L. In your case that is done by adding this to your GCC compilation:
-I/usr/local/include -L/usr/local/lib
In my case this line solved this linking error
gcc $(gsl-config --cflags) name_of_file.c $(gsl-config --libs) -o name_of_file
See Wiki
In my case I just needed do install/update gsl
brew install gsl

Not able to link dependent header files in cpp file for Rcpp

Based on this tutorial - http://www.r-bloggers.com/using-r-callling-c-code-with-rcpp/
I was trying to call a C function from R.
The C code have following dependencies and it works perfectly after compiling the C code
#include <json/json.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <curl/curl.h>
#include <time.h>
#include <math.h>
#include <Rcpp.h>
Now when i am trying to load the so file , i am seeing the below error.
dyn.load("storage.so")
Error in dyn.load("storage.so") :
unable to load shared object '/home/algotree/Rcode/storage.so':
/home/algotree/Rcode/storage.so: undefined symbol: json_object_array_length
Seems R is not able to link the rest of header files.
How can I fix it?
This has nothing to do with Rcpp (for which we also provide ample documentation regarding use on its own, in package, via inline, ...).
You seem to use JSON-parsing functionality, but apparently have not linked to a JSON-parser library corresponding to the header json/json.h you included.
Apart from this question being incomplete in its code example and hence not reproducible, I see two issues here:
learn the ropes about C/C++ program using libraries, and
apply this to the R context.
As you using JSON and Curl based on your headers, you could (and probably should) study the corresponding packages like RJSONIO and RCurl.
If you know what is going there and understand the mechanics, you can then use Rcpp to provide the new functionality you are seeking. But just by throwing Rcpp in the mix, these issues do not address themselves. You need to understand how in include headers and link libraries.
Here is how i solved the issue , for running the code i had to add -lcurl and -ljson as command link arguement. So the command R CMD SHLIB should have executed is the below commands
g++ -I/usr/share/R/include -DNDEBUG -I/usr/local/lib/R/site-library/Rcpp/include -I/usr/include/ -fpic -O3 -pipe -g -c storage.cpp -o storage.o
g++ -shared -o storage.so storage.o -L/usr/lib -L/usr/local/lib/R/site-library/Rcpp/lib -lRcpp -Wl,-rpath,/usr/local/lib/R/site-library/Rcpp/lib -L/usr/lib64/R/lib -lcurl -ljson -lR
This can be done by editing the PKG_LIBS flags.

Resources