@@ -418,6 +418,8 @@ code. This has many benefits not the least of which is easy distribution of
418
418
shared code. More information on creating packages that contain C++ code
419
419
is included in the Package Development section below.
420
420
421
+ \subsubsection {Shared Code in Header Files }
422
+
421
423
If you need to share a small amount of C++ code between source files
422
424
compiled with \texttt {sourceCpp } and the option of creating a package
423
425
isn't practical, then you can also share code using local includes of C++
@@ -441,8 +443,7 @@ use an include guard and be sure to pick a unique name for the corresponding
441
443
\texttt {\# define }.
442
444
443
445
Also note the use of the \code {inline} keyword preceding the function. This
444
- isn't necessary for \code {sourceCpp} however if you are working on an R package
445
- then it's important to ensure that there are not multiple definitions of
446
+ is important to ensure that there are not multiple definitions of
446
447
functions included from header files. Classes fully defined in header files
447
448
automatically have inline semantics so don't require this treatment.
448
449
@@ -459,10 +460,49 @@ double transformValue(double x) {
459
460
}
460
461
@
461
462
462
- Note that the processing of Rcpp attributes (e.g. \texttt {export },
463
- \texttt {depends }, etc.) is limited to the main source file so all exported
464
- functions and dependencies should be defined there rather than in
465
- shared header files.
463
+ \subsubsection {Shared Code in C++ Files }
464
+
465
+ When scanning for locally included header files \code {sourceCpp} also checks
466
+ for a corresponding implementation file and automatically includes it in the
467
+ compilation if it exists.
468
+
469
+ This enables you to break the shared code entirely into it's own source file.
470
+ In terms of the above example, this would mean having only a function
471
+ declaration in the header:
472
+
473
+ <<lang =cpp >>=
474
+ # ifndef __UTILITIES__
475
+ # define __UTILITIES__
476
+
477
+ double timesTwo(double x );
478
+
479
+ # endif // __UTILITIES__
480
+ @
481
+
482
+ Then actually defining the function in a separate source file with the
483
+ same base name as the header file but with a .cpp extension (in the above
484
+ example this would be \code {utilities.cpp}):
485
+
486
+ <<lang =cpp >>=
487
+ # include "utilities.hpp"
488
+
489
+ double timesTwo(double x ) {
490
+ return x * 2 ;
491
+ }
492
+ @
493
+
494
+ It's also possible to use attributes to declare dependencies and exported
495
+ functions within shared header and source files. This enables you to take
496
+ a source file that is typically used standalone and include it when compiling
497
+ another source file.
498
+
499
+ Note that since additional source files are processed as separate translation
500
+ units the total compilation time will increase proportional to the number of
501
+ files processed. From this standpoint it's often preferable to use shared
502
+ header files with definitions fully inlined as demonstrated above.
503
+
504
+ Note also that embedded R code is only executed for the main source file not
505
+ those referenced by local includes.
466
506
467
507
\subsection {Including C++ Inline }
468
508
0 commit comments