0

I am trying to implement gsl_rng.h on a Montecarlo simulation on my MacBook Pro (13-inch, Mid 2012). The simulation is all written in C. My problem is that gcc-6 complains it cannot find the gsl library despite the compilation flags which I think are fine.

The top of declare.h, which is included in all .c files I am working on:

/* __________________ LIBRARIES ___________________*/
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>
#include <gsl/gsl_rng.h>

The error:

fatal error: gsl/gsl_rng.h: No such file or directory

The compilation flags included in my makefile:

INCLUDE = -I/usr/local/Cellar/gsl/2.4/include
LINK = -L/usr/local/Cellar/gsl/2.4/lib -lgsl -lgslcblas

I installed both gcc-6 and gsl via Homebrew.

How can I make gcc-6 find gsl? Are my flags wrong?

The makefile:

CC = g++-6

CFLAGS = -lm -O3 -ansi -pedantic -Wall -Wextra\
        -Wconversion -Wredundant-decls -fmax-errors=7\
        -Wunsafe-loop-optimizations -Wmissing-braces\
        -Wparentheses
        # -Wdouble-promotion

INCLUDE = -I/usr/local/Cellar/gsl/2.4/include

LINK = -L/usr/local/Cellar/gsl/2.4/lib -lgsl -lgslcblas

../bin/bidimensional_MC: random.o functions.o subroutines.o\
             main.o 
$(CC) -o ../bin/bidimensional_MC random.o functions.o\
      subroutines.o main.o  $(CFLAGS) $(LINK) $(INLCUDE)

random.o: random.c
    $(CC) -c random.c -lm -O3  $(CFLAGS) $(INCLUDE)

functions.o: functions.c
    $(CC) -c functions.c  $(CFLAGS) $(INCLUDE)

main.o: main.c
    $(CC) -c main.c  $(CFLAGS) $(INCLUDE)

suboutines.o: subroutines.c
    $(CC) -c subroutines.c  $(CFLAGS) $(INCLUDE)

clean:
    rm *.o

The output of ls /usr/local/Cellar/gsl/2.4/include/gsl/ is:

/usr/local/Cellar/gsl/2.4/include/gsl/gsl_rng.h

The output of ls /usr/local/Cellar/gsl/2.4/include/ is:

gsl/ 

The output of ls /usr/local/Cellar/gsl/2.4/include/gsl/ is too long to post, but everything is there, as it should.

EXTRA INFORMATION: I am using g++-6 instead of gcc-6 because the cluster in which I'm going to finally execute the simulation requires code to be C++ compliant.

Maganna Dev
  • 189
  • 1
  • 11
  • 2
    is `/usr/local/Cellar/gsl/2.4` a valid directory? Are the include path and lib path valid? – Retired Ninja Sep 18 '17 at 04:54
  • 1
    What is the output of `ls /usr/local/Cellar/gsl/2.4/include/gsl/gsl_rng.h /usr/local/Cellar/gsl/2.4/include/gsl/ /usr/local/Cellar/gsl/2.4/include/`? – Antti Haapala -- Слава Україні Sep 18 '17 at 05:05
  • @RetiredNinja They are the absolute paths where everything is stored. Maybe you where expecting something like: `/usr/local/include` but that only contains symlinks to the paths I previously wrote. – Maganna Dev Sep 18 '17 at 05:16
  • 1
    You included some flags in your makefile; are they used in the rules? What was the command that was executed? If necessary, use `make -n` to find out — you need to see and report the invocation of GCC. – Jonathan Leffler Sep 18 '17 at 05:16
  • 1
    @MagannaDev good. Then I'd suspect that your makefile is not using the compilation flags. Thus post a MCVE of your makefile... / proof that the `-I` is actually given to gcc. (just like Jonathan said) – Antti Haapala -- Слава Україні Sep 18 '17 at 05:16
  • Please, add the information to the question where you can format it (split the lines for readability). On the face of it, there's no source code (C or C++) in that compilation line, but the 'header not found' should mean that you've still got to create the object files, or at least one object file. – Jonathan Leffler Sep 18 '17 at 05:20
  • Please — add the information to the question. We can't read it in the comments. You can format it in the question. – Jonathan Leffler Sep 18 '17 at 05:25
  • "The top of declare.h, which is included in all .c files"... yet not *all* compilation command lines use the `$(INCLUDE)`. I am voting to close as "simple typographical error / not really helpful to future readers". – Antti Haapala -- Слава Україні Sep 18 '17 at 05:36
  • I have added the information to the question, sorry I'm new here. @JonathanLeffler – Maganna Dev Sep 18 '17 at 05:37
  • @AnttiHaapala, I've included it and it still yields `fatal error: gsl/gsl_rng.h: No such file or directory #include ` – Maganna Dev Sep 18 '17 at 05:40
  • @MagannaDev so what's the **actual** GCC command that you're running – Antti Haapala -- Слава Україні Sep 18 '17 at 05:40
  • It's really hard to help you when you change things continuously. I diagnosed a problem — you fixed it. I'm not sure of the exact timing of your fix vs my answer, but it's hard to keep up with you! – Jonathan Leffler Sep 18 '17 at 05:48

1 Answers1

2

In the makefile, you have (or, more precisely, at one time claimed to have):

random.o: random.c
    $(CC) -c random.c -lm -O3  $(CFLAGS)

You shouldn't specify the library when compiling the object file. Your CFLAGS do not include the ${INCLUDE} (or $(INCLUDE)) macro. You need something like:

random.o: random.c
    $(CC) -c random.c -O3 $(CFLAGS) $(INCLUDE)

This is a minimal change; I'd add $(INCLUDE) to CFLAGS (and remove the -lm again — you don't even need that on a Mac though it does no specific harm). I'd also add -Werror -Wmissing-prototypes -Wstrict-prototypes, and using -ansi (aka -std=c90) isn't sensible — it is an archaic standard. You should be using -std=c11.

CFLAGS = -O3 -g -std=c11 -pedantic -Wall -Wextra \
        -Wconversion -Wredundant-decls -fmax-errors=7 \
        -Wunsafe-loop-optimizations -Wmissing-braces \
        -Wparentheses $(INCLUDE) \
        -Werror -Wmissing-prototypes -Wstrict-prototypes

Also, I added -g to get debugging code. Always include -g, even with optimization. It doesn't have a run-time cost; it does have a benefit if you need to debug the code. Granted, it isn't as easy to debug optimized code, but you can do more than if you don't have -g. Include it in both the 'compilation to object' and the 'linking' phases.

(And, in point of detail, I'd have a bunch of separate macros so that each flag can be turned on or off independently, rather than having to rewrite the whole of CFLAGS. However, that can await another day.)

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278