Thank you for the quick and kind reply, Kevin! You are right. I confirmed that the program option object is defined as a local variable in mylib::Simulation constructor, and is destructed correctly. But I found that the object is initialized with other objects from several source files, and they were defined as static variables. Changing them into local variables has solved the problem. What I didn't know was, therefore, that static variables do survive after Rcpp function calling. Now I will have to take a careful look at other variables too.
Thanks a lot!! Watal On Wed, Jul 6, 2016 at 3:13 PM, Kevin Ushey <kevinus...@gmail.com> wrote: > If I had to guess, maybe there is some initialization that occurs when > your mylib::Simulation object is constructed, and you need to ensure > that code is only run once? (Perhaps the code that defines the program > options needs to be hidden behind an initialization flag, or something > like that) > > Nonetheless, this isn't really an Rcpp question, and it's hard to know > for sure without seeing how your Simulation object is defined. > > On Tue, Jul 5, 2016 at 10:56 PM, Watal M. Iwasaki <heavy.wa...@gmail.com> > wrote: > > Hi all, > > > > I have been trying to make an R function to call my C++ simulation > program > > in this way: > > ```c++ > > // [[Rcpp::plugins(cpp14)]] > > #include <Rcpp.h> > > #include <mylib.hpp> > > // [[Rcpp::export]] > > std::string run_simulation(Rcpp::CharacterVector > > args=Rcpp::CharacterVector::create()) { > > auto vs_args = Rcpp::as<std::vector<std::string>>(args); > > mylib::Simulation sim(vs_args); > > sim.run(); > > return sim.results(); > > } > > ``` > > > > The class `Simulation` parses arguments with `boost::program_options` > and is > > implemented in `libmylib.dylib`, which can be linked with `PKG_LIBS = > > -lmylib`. Both `devtools::check()` and independent `Rcpp::sourceCpp()` > can > > successfully build the function `run_simulation()`. It runs perfectly as > I > > intended **only if it is the first call**. The second time I call > > `run_simulation('-v')`, I get the following error: > > ``` > > Error: option '-v' is ambiguous and matches different versions of > > '--verbose' > > ``` > > > > Other options like `-h` or `--mutation=0` raise the same error. It > indicates > > that every option is re-defined after the first call (maybe?). Calling > > without any argument is always OK, and an executable `a.out` built with > > `main.cpp -lmylib` has no problem. I guess the key is how C++ objects are > > cached in R environments. Is this a bug in Rcpp? Or am I missing > something? > > > > Thanks, > > > > -- > > Watal M. Iwasaki / 岩嵜 航 > > SOKENDAI, Graduate University for Advanced Studies, > > Hayama, Kanagawa 240-0193, Japan > > +81-46-858-1576 > > https://heavywatal.github.io/ > > > > _______________________________________________ > > Rcpp-devel mailing list > > Rcpp-devel@lists.r-forge.r-project.org > > https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel > -- Watal M. Iwasaki / 岩嵜 航 SOKENDAI, Graduate University for Advanced Studies, Hayama, Kanagawa 240-0193, Japan +81-46-858-1576 https://heavywatal.github.io/
_______________________________________________ Rcpp-devel mailing list Rcpp-devel@lists.r-forge.r-project.org https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel