Search code examples
c++11makefileclangclang++mersenne-twister

clang ignores -std=c++11 flag for include files


I have some code that I use a (old) Mersenne Twister header file with to get a good pseudo-random number generator.

The problem is that that code uses the register keyword which (as far as I understand) was deprecated in c++11 and throws an error with c++17. I believe I am using the latter in the version of clang I'm using on Mac OSX (10.14.6; Mojave).

The Makefile I am using is as thus:

BUILDDIR=$(TOPDIR)/bin
INCDIROUT=$(BUILDDIR)/include
LIBDIROUT=$(BUILDDIR)/lib
INCDIR=inc/
SRCDIR=src/
INCFLAG= -IMersenne -Iinc
LIBFLAG= -L. -L$(LIBDIROUT)

#need to use an older version of gcc b/c of Mersenne Twister using the deprecated `register` keyword
#https://github.com/danini/graph-cut-ransac/issues/23
CXX=clang++
#CXXFLAGS=-g -std=c++11 -Wall -pedantic
CXXFLAGS=-fpermissive -std=c++98

#trick for getting the git version in the code
GIT_VERSION = $(shell sh -c 'git describe --abbrev=4 --always')

CFLAGS += -D__GIT_VERSION=\"$(GIT_VERSION)\"

RUN_SCRIPT := $(shell mkdir -p 'bin/lib')

all: $(BUILDDIR)/realizeCascades $(LIBDIROUT)/rootUtil.o $(LIBDIROUT)/edepmath.o $(LIBDIROUT)/cascadeProd.o $(LIBDIROUT)/isotope_info.o $(LIBDIROUT)/weisskopf.o $(LIBDIROUT)/lindhard.o $(LIBDIROUT)/libncap.so

$(LIBDIROUT)/isotope_info.o: $(SRCDIR)/isotope_info.c $(INCDIR)/isotope_info.h
        $(CXX) -std=c++98 -fPIC -c $(CFLAGS) $(INCFLAG) $(SRCDIR)/isotope_info.c `root-config --cflags --glibs` $(LIBFLAG) 
        mv isotope_info.o $(LIBDIROUT)/

$(LIBDIROUT)/rootUtil.o: $(SRCDIR)/rootUtil.c $(INCDIR)/rootUtil.h
        $(CXX) -std=c++98 -fPIC -c $(CFLAGS) $(INCFLAG) $(SRCDIR)/rootUtil.c `root-config --cflags --glibs` $(LIBFLAG) 
        mv rootUtil.o $(LIBDIROUT)/

$(LIBDIROUT)/edepmath.o: $(SRCDIR)/edepmath.c $(INCDIR)/edepmath.h
        $(CXX) -std=c++98 -fPIC -c $(CFLAGS) $(INCFLAG) $(SRCDIR)/edepmath.c `root-config --cflags --glibs` $(LIBFLAG) 
        mv edepmath.o $(LIBDIROUT)/

$(LIBDIROUT)/weisskopf.o: $(SRCDIR)/weisskopf.c $(INCDIR)/weisskopf.h
        $(CXX) -std=c++98 -fPIC -c $(CFLAGS) $(INCFLAG) $(SRCDIR)/weisskopf.c `root-config --cflags --glibs` $(LIBFLAG) 
        mv weisskopf.o $(LIBDIROUT)/

$(LIBDIROUT)/lindhard.o: $(SRCDIR)/lindhard.c $(INCDIR)/lindhard.h
        $(CXX) -std=c++98 -fPIC -c $(CFLAGS) $(INCFLAG) $(SRCDIR)/lindhard.c `root-config --cflags --glibs` $(LIBFLAG) 
        mv lindhard.o $(LIBDIROUT)/

$(LIBDIROUT)/cascadeProd.o: $(SRCDIR)/cascadeProd.c $(INCDIR)/cascadeProd.h
        $(CXX) -std=c++98 -fPIC -c $(CFLAGS) $(INCFLAG) $(SRCDIR)/cascadeProd.c `root-config --cflags --glibs` $(LIBFLAG) 
        mv cascadeProd.o $(LIBDIROUT)/

$(LIBDIROUT)/libncap.so: $(LIBDIROUT)/isotope_info.o $(LIBDIROUT)/weisskopf.o $(LIBDIROUT)/lindhard.o $(LIBDIROUT)/cascadeProd.o $(LIBDIROUT)/edepmath.o $(LIBDIROUT)/rootUtil.o
        $(CXX) -std=c++98 -fPIC -shared $(LIBDIROUT)/lindhard.o $(LIBDIROUT)/weisskopf.o $(LIBDIROUT)/isotope_info.o $(LIBDIROUT)/cascadeProd.o $(LIBDIROUT)/edepmath.o $(LIBDIROUT)/rootUtil.o `root-config --cflags --glibs` -o $(LIBDIROUT)/libncap.so 

$(BUILDDIR)/realizeCascades: $(LIBDIROUT)/libncap.so $(BUILDDIR)/realizeCascades.cpp
        $(CXX) -std=c++98 -fPIC -Wl,-rpath=$(LIBDIROUT) $(CFLAGS) $(INCFLAG) $(LIBFLAG) $(BUILDDIR)/realizeCascades.cpp `root-config --cflags --glibs` -lncap -o $(BUILDDIR)/realizeCascades 

clean:
        rm -f $(LIBDIROUT)/*.o
        rm -f $(LIBDIROUT)/*.so
        rm -f $(BUILDDIR)/realizeCascades
        rm -f *.o
        rm -f *.so
        rm -rf $(LIBDIROUT)

despite using the -std=c++11 or -std=c++98 flags once the makefile begins to compile lindhard.c as thus:

clang++ -std=c++98 -fPIC -c -D__GIT_VERSION=\"v1.0.6-73-g01bc\"
-IMersenne -Iinc src//lindhard.c `root-config --cflags --glibs` -L. -L/Users/villaa/nrCascadeSim/bin/lib

An error is thrown for each of the many times the register keyword is used:

Mersenne/MersenneTwister.hh:187:2: error: ISO C++17 does not allow 'register' storage class specifier [-Wregister]
        register uint32 s1;
        ^~~~~~~~~

I am struggling to find out the reason for this. Is the -std flag not being applied to the pre-processing of include files? Am I going about this the wrong way?


Solution

  • @MadScientist and @idz were correct above. The key was that the root libraries when using root-config --cflags --glibs added the following:

    -stdlib=libc++ -pthread -std=c++17 -m64 -I/usr/local/Cellar/root/6.24.04/include/root -L/usr/local/Cellar/root/6.24.04/lib/root -lGui -lCore -lImt -lRIO -lNet -lHist -lGraf -lGraf3d -lGpad -lROOTVecOps -lTree -lTreePlayer -lRint -lPostscript -lMatrix -lPhysics -lMathCore -lThread -lMultiProc -lROOTDataFrame -stdlib=libc++ -lpthread -lm -ldl
    

    This came after my flag -std=c++98 and therefore overrode it.

    The solution here was to move the -std=c++98 to after the root-config line.

    I suppose it was technically possible that some of the ROOT code that I was using required c++17 and then I'm not sure if this could have been resolved.

    But as it turns out I was using a portion of the ROOT code base that was older and likely (though I'm still not sure) didn't have any upgrades requiring c++17.

    If it did I suppose my only choice would have been to update my incompatible library, Mersenne, to be compatible with c++17

    PS: I know that Mersenne Twister now is implemented in C++ standard code, but for reasons I had preferred to use my old implementation.