[R-pkg-devel] C++ library USING_R

Dirk Eddelbuettel edd @end|ng |rom deb|@n@org
Thu Oct 15 16:55:42 CEST 2020


On 15 October 2020 at 10:36, Joseph Park wrote:
| Apologies for the mis-directed reply, and, imprecise syntax/context re 
| environment variable.
| 
| Just to close the loop, it seems that using a -D macro assignment inside 
| the make command (macro?) is interpreted as a make option:

(I think 'make target' is a common expression.)
 
| $(LIBEDM):
|      @(cd $(CPPEDM_SRC_PATH) && $(MAKE) clean && $(MAKE) \
|      CXX="$(CXX11) $(CXX11STD)" CXXFLAGS="$(CXX11FLAGS) $(CXX11PICFLAGS)" \
|      AR="$(AR)" RANLIB="$(RANLIB)" -DUSING_R=1)
| 
| ...
| 
|     make[1]: Entering directory 
| '/tmp/RtmpxVmJkT/Rbuild1553f22c7484d/rEDM/src/cppEDM/src'
|     rm -f API.o CCM.o Common.o DateTime.o EDM.o EDM_Formatting.o 
| EDM_Neighbors.o Eval.o Multiview.o Parameter.o Simplex.o SMap.o libEDM.a
|     make[1]: Leaving directory 
| '/tmp/RtmpxVmJkT/Rbuild1553f22c7484d/rEDM/src/cppEDM/src'
|     make: invalid option -- 'D'
| 
| Hmm... since I use an R-specific makefile inside the C++ library 
| directory anyway, adding -DUSING_R there seems like a decent solution.

Yes, exactly, that was my initial suggestion. It's been a while since I
needed this but when do I tend to just go back to simple examples to check I
am on the right track:

   edd using rob:/tmp$ g++ -o io io.cpp; ./io         # default: empty
   Hello boring world
   edd using rob:/tmp$ g++ -DFOO -o io io.cpp; ./io   # with a defined 
   We see foo
   edd using rob:/tmp$ cat io.cpp
   #include <iostream>
   
   int main() {
   #if defined(FOO)
     std::cout << "We see foo\n";
   #else
     std::cout << "Hello boring world\n";
   #endif
   }
   edd using rob:/tmp$ 
   
So setting -Dsomething is really equivalent to doing in the code via #define
but allows you to have it done conditional on when/where/how you compile.
   
| Thanks a-million for the support & guidance to get me to the obvious 
| solution!

No worries. Is your repo public?  Good _portable_ cross-language solutions
are rare, it would be good to have another one to look at / refer people to.

Dirk

-- 
https://dirk.eddelbuettel.com | @eddelbuettel | edd using debian.org



More information about the R-package-devel mailing list