gcc cannot compile C files using mpi.h - c

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.

Related

gcc include source files in subdirectory relative to each other in c

I'm having issues compiling some c code.
I have a code structure like the following
main.c
src
line
line.h
line.c
Available here: https://github.com/Vafilor/c_includes
main.c includes #include "src/line/line.h"
src/line/line.c includes #include "src/line/line.h"
When I try to compile this with gcc main.c src/line/line.c on ubuntu 20.4
I get this error
src/line/line.c:1:10: fatal error: src/line/line.h: No such file or directory
1 | #include "src/line/line.h"
| ^~~~~~~~~~~~~~~~~
compilation terminated.
How can I get this to compile?
Additional info:
I've been looking at the bluez code at https://git.kernel.org/pub/scm/bluetooth/bluez.git/tree/
and they have something similar with
client/main.c - includes shell.h
src/shared/shell.c - includes shell.h
src/shared/shell.h
so I'm trying to figure out how it works for their code base.

Using gcc to compile userspace application using linux kernel headers

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

"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.

gcc compiler error: "cannot find -lcs50 collect2: Id returned 1 exit status"

I have a problem in 'c' language inside compiling with gcc.
I am using "Cygwin" with (gcc-core, gcc-g++, gdb, make & other supportive packages) inside windows xp.
I installed "Cygwin" on this path "C:\Cygwin\".
My home directory: "C:\Cygwin\home\Bhanu Pratap"
I copied "cs50.h" and "cs50.c" inside my working directory which is also under "C:\Cygwin\home\Bhanu Pratap".
This is code inside my hello.c file
#include "cs50.h"
#include <stdio.h>
int
main(void){
string name = "David";
printf("O hai, %s!\n", name);
}
This is command under bash (Cygwin)
gcc -o hello hello.c -lc50
I get this error:
/usr/lib/gcc/i686-pc-cygwin/3.4.4/../../../../i686-pc-cygwin/bin/id: cannot find -lcs50
collect2: Id returned 1 exit status
Please help me where i am wrong?
I'm also using the cs50 library file, and I've noticed in the code that you've used is :
#include "cs50.h"
#include <stdio.h>
and also this command :
gcc -o hello hello.c -lc50
just wondered why you used quotation marks, instead of '< >'
and the last part of the command -lc50
we normally use it this way:
#include <cs50.h>
#include <stdio.h>
and -lcs50
hope this helps \m/
To be able to use -lcs50, you'll first need to build that library (cs50) from its source code (cs50.c).
Alternatively, you could simply:
gcc -o hello hello.c cs50.c
assuming cs50.c doesn't have other dependencies.
I am using DJGPP (gcc) compiler in Windows XP for CS50 edX course.
I have tried different solutions from answers, but none of them helped me (though Mat gave me a clue).
Here is a solution:
1) copy cs50.h and cs50.c from library50-c-5.zip into a directory, where your .c source file, which you want to compile, is located.
2) type into your .c source file: #include "cs50.h"
3) compile your .c source file (at cmd.exe prompt, for example): gcc custom.c -o custom cs50.c
You may copy cmd.exe from "`C:\WINDOWS\system32" folder into your working folder (with your .c files). In this case you don't have to change directory for navigating to your working files, when you start up command prompt window.
See the link http://manual.cs50.net for relevant guidance about guidance about installing the cs50.h library. They have a precompiled version of the cs50 library that can be downloaded and installed. It is worth a try. They used gcc to compile the library, and they beginning to switch over to clang, which can also produce 64 bit compatible libraries, which is going to more useful in the future.

Resources