1
votes

I need to build object files from C++ sources, then link them against the Boost Regex library to mimic the compilation of a package using Rcpp. I observe the same problem in the compilation of a small test case and of the complete package, hence the two versions of the problem provided here, for people unfamiliar with Rcpp.

Small test case

I am using Gcc 4.6.3 on Windows, as provided by Rtools. The source file for the test is very basic: I include regex.hpp, then I use boost::regex, boost::regex_match, boost::regex_search and boost::smatch. The problem is that if I compile my code using:

g++ -O0 -pipe -g -Wall -I"xxx\boost_1_56_0" "src/01 - regex.cpp" -o Debug/regex.o
g++ -O0 -pipe -g -Wall Debug/regex.o "xxx\libboost_regex-gcc-1_56.a" -o Debug/reg.exe

I get this kind of error message at the first command:

undefined reference to `boost::re_detail::cpp_regex_traits_char_layer<char>::init()

But if I use a single command to build everything:

g++ -O0 -pipe -g -Wall -I"xxx\boost_1_56_0" "src/01 - regex.cpp" "xxx\libboost_regex-gcc-1_56.a" -o Debug/reg.exe

I get no error messages. How can I compile my object files then link to Boost Regex ?

Problem with the complete Rcpp package

In the bigger context, when I compile my real Rcpp package, I am able to compile several other object files and link them later to (non Boost) libraries. Boost Regex is the first problematic library in that regard.

The complete package is built using:

R CMD INSTALL --no-multiarch --with-keep.source .

With:

STKPPLIB = ../../stkpp/lib/libSTKpp.a
RELIB = ../../boost_1_56_0/libs/regex/build/gcc/libboost_regex-gcc-1_56.a
MCLIB = ../../MixtComp/lib/libMixtComp.a

and:

PKG_LIBS = `$(R_HOME)/bin/Rscript -e "Rcpp:::LdFlags()"` $(MCLIB) $(STKPPLIB) $(RELIB)

And the build process give similar "undefined references" errors. The question is the same as the previous one: how can I compile my object files then link to Boost Regex ?

2
Turns out that for the small test case I just forgot the gcc -c option to compile but not link the object file. - vkubicki

2 Answers

2
votes

My personal advice is to put the source of regex part in your package and compile that when installing the package.

I have used regex in one of my package and no compiling error on Windows. You can find it by

https://github.com/thirdwing/mzR/

Hope it helps you.

1
votes

This amounts to the standard problem of 'how do I build package that needs to link to a library?'

First off, you should use R CMD COMPILE ... rather than manual g++ invocation. You then have all the options provided by R to declare libraries to link to -- notable PKG_LIBS.

All this gets easier once you use a package. Dozens of answers here on SO strongly suggest a package as the appropriate structure for more complex collections of R and C++ files.

This topic is discussed in a few places, starting with the Writing R Extensions manual and my Rcpp book. Many Rcpp packages on CRAN do it. For Windows and the CRAN auto-builder, you have the added problem of ensuring they have the library you need.

Edit: Even after your edits, your example is not complete or minimal, but if you want to see a simple use of Boost regex with Rcpp, the Rcpp Gallery has a post illustraing it as well. If you want to be self-contained, then @KQiang's advice is good: embed the source. You will have to leanrn how to build packages with subdirectories, so you trade one difficulty off for another. But there are examples for either approach.