Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Include gsl_type.h. File not found

It seems to be a common issue but I can't wrap solve this problem.

I have some .c code that I compile using a makefile. The goal is to create a shared object (.so) so that I can run the C code from R.

Here is my makefile:

obs = R_wrapper.o G.o develop.o utilities.o
CFLAGS = -arch x86_64 -std=gnu99 -I/Library/Frameworks/R.framework/Resources/include -I/Library/Frameworks/R.framework/Resources/include/x86_64 -DNDEBUG  -I/usr/local/include    -fPIC  -g -O3  -c
LFLAGS = -arch x86_64 -std=gnu99 -dynamiclib -Wl,-headerpad_max_install_names -undefined dynamic_lookup -single_module -multiply_defined suppress -L/usr/local/lib -O3 -lgsl -lm -lgslcblas

R_wrapper : $(obs)
    $gcc $(LFLAGS) $(obs) -o R_wrapper.so

R_wrapper.o : R_wrapper.c constants.h develop.h G.h
    $gcc $(CFLAGS) R_wrapper.c

G.o : G.c G.h constants.h utilities.h develop.h 
    $gcc $(CFLAGS) G.c develop.c

develop.o : develop.c develop.h G.h constants.h
    $gcc $(CFLAGS) develop.c

utilities.o : utilities.c develop.h
    $gcc $(CFLAGS) utilities.c

It works fine on the computer in my lab but it doesn't work on my personal computer. What causes this issue is these two lines at the beginning of my R_wrapper.c.

#include </opt/local/include/gsl/gsl_randist.h>
#include </opt/local/include/gsl/gsl_rng.h>

I have tried to move these files around and give different paths, put the files in the gal_type.h files in the same directory as my R_wrapper file, I have tried to rename my directories so that the path is more conventional but the OS did not give me the right to rename opt into usr (which probably makes obvious sense). I haven't created the makefile and don't fully get it. I would suppose I'd need to modify the path after the -I argument somewhere in CFLAGS OR LFLAGS.

EDIT 1

I changed R_wrapper.c on my lab computer to get rid of the whole path in #include <...>. Compilation failed as @Beta predicted. Then, I changed my makefile to add -I/opt/local/include/gsl to CFLAGS. I don't know what you mean by Verify that the makefile still works. I tried to compile with my edited makefile on my lab computer and it failed. I then re-edited my makefile changing -I/opt/local/include/gsl to -I/usr/local/include/gsl because on my lab computer the gsl folder is at /usr/local/include/gsl. -I/opt/local/include/gsl is the location of the gsl folder on my computer. So I am stuck here in your procedure.

Edit 2

I moved my gsl folder around my computer trying to include from different paths. And some interesting stuff happen. For example when I put my gsl folder in Users/remi/Documents/Biologie/programing/C/ and write (in CFLAGS)

-I/Users/remi/Documents/Biologie/programing/C/ 

I get this error:

R_wrapper.c:43:10: fatal error: 'gsl_randist.h' file not found
#include <gsl_randist.h> // goal.

When I write (in CFLAGS)

Users/remi/Documents/Biologie/programing/C/gsl

I get this error message:

"In file included from R_wrapper.c:43: /Users/remi/Documents/Biologie/programing/C/gsl/gsl_randist.h:22:10: fatal error: 
      'gsl/gsl_rng.h' file not found
#include <gsl/gsl_rng.h>" 
like image 419
Remi.b Avatar asked Dec 04 '14 04:12

Remi.b


2 Answers

Transferring comment to answer

Judging from the message about gsl/gsl_rng.h not being found that is mentioned in Edit 2, you should be writing

#include <gsl/gsl_randist.h>

(with the path prefix of gsl/ before the header name) in your source code. This is a common convention. You then specify in the -I option the name of the directory containing the gsl subdirectory that contains the gsl_*.h headers. In your Edit 2, you say you put the gsl directory into /Users/remi/Documents/Biologie/programing/C/, so you are then correct to use:

-I/Users/remi/Documents/Biologie/programing/C/

on the command line as you tried.

You should read the documentation, and if it says write either of these:

#include <gsl/gsl_randist.h>
#include "gsl/gsl_randist.h"

then that is what you should write in your code, because (as you've discovered the hard way) if you don't, it won't work.

Beta's answer also states

In general it's a bad idea to write paths into the #include statements unless you really have to; it causes just this kind of problem.

I agree, but would state it more strongly:

  • Do not write complete paths into the #include statements ever.

If you do write them, it radically limits the portability of your code. You can't rely on other people's machines having software installed in the same place as it is installed on your system. If you tried it in open source software, you'd be laughed out of court.

Be wary of people who get cute with ../somedir/header.h too — see What are the benefits of a relative path such as "../include/header.h" for a header?.


I observe that the GNU Scientific Library manual has an example program which starts:

#include <stdio.h>
#include <gsl/gsl_sf_bessel.h>

and the section on Compiling and Linking says:

The library header files are installed in their own gsl directory. You should write any preprocessor include statements with a gsl/ directory prefix thus,

#include <gsl/gsl_math.h>

If the directory is not installed on the standard search path of your compiler you will also need to provide its location to the preprocessor as a command line flag. The default location of the gsl directory is /usr/local/include/gsl.

like image 118
Jonathan Leffler Avatar answered Oct 19 '22 13:10

Jonathan Leffler


In general it's a bad idea to write paths into the #include statements unless you really have to; it causes just this kind of problem.

On your lab computer, edit R_wrapper.c:

#include <gsl_randist.h>
#include <gsl_rng.h>

The build should now fail. If so then this verifies that you don't have other versions of these headers floating around, or links to them, or whatever. Confirm that it fails, then back out the change, and confirm that it works again.

Then add -I/opt/local/include/gsl to CFLAGS. Verify that the makefile still works.

Then edit R_wrapper.c again; now the build should succeed.

Decide where you would like to keep these files (gsl_randist.h and gsl_rnd.h) on your personal computer, and modify CFLAGS accordingly on your home version of the makefile.

Once all of that is working perfectly, we can show you how to write one makefile that will work on both machines.

Also, you can improve your makefile rules in other ways, but I must ask one question first:

G.o : G.c G.h constants.h utilities.h develop.h 
    $gcc $(CFLAGS) G.c develop.c

Does G.o really require develop.c? If so then you should probably reexamine your source files, because this is really unhygienic.

like image 44
Beta Avatar answered Oct 19 '22 12:10

Beta