cmacosgccgslgcc6

Unable to compile with gsl on Mac


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.


Solution

  • 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.)