2

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.

1 Answers1

2

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.

Dirk Eddelbuettel
  • 360,940
  • 56
  • 644
  • 725
  • I've seen a lot of your answers, so thanks for teaching me as much as you have. I tried the same hack with `PKG_LIBS` I get a ``` make: *** No rule to make target '-fstack-protector', needed by ...Stop. ``` I've taken a look at the Makevars.win packages that make it onto bioconductor, but I haven't found a package that sets the linker flags directly. – Joseph O'Brien Oct 29 '21 at 19:34
  • 1
    I meant: Use `OBJECTS` where you currently use `PKG_LIBS`. Full stop. Then add `-fstack_protector` (as one or several) args for `PKG_LIBS`. They will not be targets. – Dirk Eddelbuettel Oct 29 '21 at 19:57
  • Ok. Thank you. I don't know why I thought you were using `OBJECTS` in the abstract. Setting `OBJECTS` as its own variable and using it as the dependency for `SHLIB` seems to have been the missing step. I'm not getting compiler errors. I'll mark the answer as accepted once I get a chance to validate the code on windows. – Joseph O'Brien Oct 29 '21 at 20:20
  • @JosephO'Brien So how did the validation go? – Dirk Eddelbuettel Nov 02 '21 at 15:32
  • I'm tracking down a `Error: basic_string::_M_construct null not valid` error that is probably related to sqlite that may or may not be related to the compilation – Joseph O'Brien Nov 03 '21 at 16:07
  • So we moved from a _linking_ error to a _compilation_ error. Maybe a smaller reproducible example will help narrow it down. – Dirk Eddelbuettel Nov 03 '21 at 17:05
  • You're right. This is a separate error – Joseph O'Brien Nov 03 '21 at 20:19
  • Yep. I take it you got to it? – Dirk Eddelbuettel Nov 03 '21 at 20:21
  • I was working on it. I'm still not able to reproduce the results from the build on linux. – Joseph O'Brien Nov 04 '21 at 16:42
  • 1
    @eddelbuettel I should have updated this a couple weeks ago, but we have the functionality on windows now. Thanks again for the help. – Joseph O'Brien Nov 18 '21 at 20:44