Search code examples
c++windowsrcpppackage-management

LDFLAGS not read in Makevars.win when building an Rcpp package


Short and sweet:

I'm writing an Rcpp package that uses zlib and sqlite.

In the following Makevars.win file, I set Compiler flags and try to set some targets.

PKG_CPPFLAGS=-I. -I./lib/sqlite/ -fopenmp -march=native -g -O2 -msse2 -fstack-protector -mfpmath=sse\
             -DRSQLITE_USE_BUNDLED_SQLITE \
             -DSQLITE_ENABLE_RTREE \
             -DSQLITE_ENABLE_FTS3 \
             -DSQLITE_ENABLE_FTS3_PARENTHESIS \
             -DSQLITE_ENABLE_FTS5 \
             -DSQLITE_ENABLE_JSON1 \
             -DSQLITE_ENABLE_STAT4 \
             -DSQLITE_SOUNDEX \
             -DRCPP_DEFAULT_INCLUDE_CALL=false \
             -DRCPP_USING_UTF8_ERROR_STRING \
             -DBOOST_NO_AUTO_PTR \
             -DSQLITE_MAX_LENGTH=2147483647 \
             -DHAVE_USLEEP=1

PKG_CXXFLAGS=$(CXX_VISIBILITY)
PKG_CFLAGS=$(C_VISIBILITY)

LDFLAGS=-fstack-protector

PKG_LIBS = lib/sqlite/sqlite3.o lib/zlib/adler32.o lib/zlib/compress.o lib/zlib/crc32.o lib/zlib/deflate.o lib/zlib/gzclose.o lib/zlib/gzlib.o lib/zlib/gzread.o lib/zlib/gzwrite.o lib/zlib/infback.o lib/zlib/inffast.o lib/zlib/inflate.o lib/zlib/inftrees.o lib/zlib/trees.o lib/zlib/uncompr.o lib/zlib/zutil.o  #-Llib/sqlite/ -lsqlite3

.PHONY: all

all: $(SHLIB)

$(SHLIB): $(PKG_LIBS)

(Emphasis on the -fstack-protector flag)

In spite of this, the linker line in the build window is:

C:/rtools40/mingw64/bin/g++ -std=gnu++11 -shared -s -static-libgcc -o OptiLCMSmzDB.dll tmp.def RcppExports.o base64.o mzDBReader.o mzDBWriter.o mzMLReader.o query_mzDB.o spectrum.o lib/sqlite/sqlite3.o lib/zlib/adler32.o lib/zlib/compress.o lib/zlib/crc32.o lib/zlib/deflate.o lib/zlib/gzclose.o lib/zlib/gzlib.o lib/zlib/gzread.o lib/zlib/gzwrite.o lib/zlib/infback.o lib/zlib/inffast.o lib/zlib/inflate.o lib/zlib/inftrees.o lib/zlib/trees.o lib/zlib/uncompr.o lib/zlib/zutil.o -LC:/PROGRA~1/R/R-40~1.4/bin/x64 -lR

A little long, but notice that our favorite flag is missing. As a result, the linker confronts me with several hundred instances of the following:

undefined reference to `__stack_chk_fail'`

I'm compiling on windows 10 using rtools40 with -std=gnu++11

I would greatly appreciate any suggestions.


Solution

  • There are a lot of things going on there we need to decompose.

    First off, you managed to have SHLIB use your enumerated list of object files. Good! I recently had to the same and I used a OBJECTS list. I think you may get lucky if you stick the -fstack-protector into PKG_LIBS because the PKG_* variables are there for your expand on the defaults use (in the hidden Makefile controlled by R). Whereas ... LDFLAGS may just get ignored.

    Otherwise, I would recommend to sample among the 4000+ CRAN packages with compiled code. Some will set similar things, the search with the 'CRAN' "org" at GitHub is crude but better than nuttin'. Good luck!

    Edit: You could look at my (more complicated still) Makevars.win for RInside. I just grep'ed among all the repos I have here and I don't have a current example of anybody setting -fSOMETHING on Windows.

    Edit 2: I do actually have a better example for your. Each and every RcppArmadillo package uses

    PKG_CXXFLAGS = -I../inst/include -I. $(SHLIB_OPENMP_CXXFLAGS) 
    PKG_LIBS = $(SHLIB_OPENMP_CXXFLAGS) $(LAPACK_LIBS) $(BLAS_LIBS) $(FLIBS)
    

    where the (R-system-level variable) SHLIB_OPENMP_CXXFLAGS expands to -fopenmp. So I really do think you want PKG_LIBS as stated above.