Skip to content

Conditional attributes don't work (e.g. [[Rcpp::depends]] inside #ifdef) #1479

@tom93

Description

@tom93

Hi, when I put // [[Rcpp::depends(...)]] inside an #ifdef, Rcpp "sees" this attribute and attempts to load the package even when the condition is false. Is there a way to make the Rcpp::depends attribute conditional?

I tried putting the attribute in a separate .h file and conditionally #include-ing that file, but that has exactly the same issue -- Rcpp "sees" the #include and always follows it regardless of the enclosing #ifdef.

My use case is that I have a .cpp file (for use with sourceCpp) with an optional dependency on some package "foo". I'd like to let users enable the use of foo by setting PKG_CPPFLAGS=-DFOO. I can wrap the C++ code that uses foo in an #ifdef FOO, but that doesn't work for the Rcpp::depends attribute (which is always "seen" by Rcpp and causes an error if foo is not installed).

The best workaround I can think of is to read the .cpp file, conditionally delete the #ifdef FOO ... #endif, and pass the result to sourceCpp(code=...). I'd appreciate any better ideas.

I think the proper solution is for Rcpp to run the real C preprocessor instead of trying to emulate it, but I'm not sure how difficult that would be. At the very least it would be nice if the current behaviour was documented, because the inconsistency with the real C preprocessor is surprising (#include is respected, but #ifdef etc. are not).

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions