[R-pkg-devel] How to use O4 flag in an RCpp project?

Peiyuan Zhu g@ryzhubc @end|ng |rom gm@||@com
Wed May 21 22:55:14 CEST 2025


Thanks Kasper, but I don’t know how exactly to accomplish this. As Dirk
says the system overwrites it every time.

On Wednesday, May 21, 2025, Kasper Daniel Hansen <
kasperdanielhansen using gmail.com> wrote:

> As a package writer, you should not in general (IMO) override the settings
> made by whoever compiled R on your system
>
> If you want to use -O4, you should really compile R from source using -O4
> and check that it works. Then your package will use it as well.
>
> Best,
> Kasper
>
> On Wed, May 21, 2025 at 3:30 PM Peiyuan Zhu <garyzhubc using gmail.com> wrote:
>
>> Ok now I replaced all -g -O2 with -O3 in Makeconf then it seems to have
>> worked. Not sure which line exactly that has led to it working tho.
>>
>> > load_all(".")
>> ℹ Loading dst
>> Exports from /Users/garyzhubc/Work/dst-1/src/belplauHFast.cpp:
>>    NumericMatrix belplauHFast(NumericVector MACC, const arma::sp_mat& W2,
>> NumericMatrix h, bool display_progress = false)
>>
>> Exports from /Users/garyzhubc/Work/dst-1/src/closure.cpp:
>>    LogicalMatrix closure(IntegerMatrix ttx, bool computeJoin = true, bool
>> display_progress = false)
>>
>> Exports from /Users/garyzhubc/Work/dst-1/src/closureSparse.cpp:
>>    arma::sp_mat closureSparse(arma::sp_mat ttx, bool computeJoin = true,
>> bool display_progress = false)
>>
>> Exports from /Users/garyzhubc/Work/dst-1/src/commSparse.cpp:
>>    NumericVector commSparse(arma::sp_mat x, arma::sp_mat x_c, double a,
>> bool display_progress = false)
>>
>> Exports from /Users/garyzhubc/Work/dst-1/src/iotaSparse.cpp:
>>    arma::sp_mat iotaSparse(arma::sp_mat tt, bool display_progress = false)
>>
>> Exports from /Users/garyzhubc/Work/dst-1/src/treeFast.cpp:
>>    SEXP buildTreeFast(const arma::sp_mat& tt, const Rcpp::NumericVector&
>> q,
>> bool display_progress = false, Rcpp::Nullable<Rcpp::IntegerVector>
>> indices
>> = R_NilValue)
>>    SEXP updateTreeFast(SEXP tree_ptr, LogicalVector xx_vec, LogicalVector
>> s_vec)
>>    SEXP supersetFast(SEXP node_ptr, LogicalVector z_vec)
>>    NumericVector unravelTreeFast(SEXP tree_ptr)
>>    List inspectNode(SEXP tree_ptr)
>>    Rcpp::List inspectNodes(Rcpp::List trees)
>>    Rcpp::List buildTreesFast(const arma::sp_mat& tt, const
>> Rcpp::NumericVector& q)
>>    Rcpp::NumericVector unravelTreesFast(Rcpp::List trees)
>>    Rcpp::List updateTreesFast(Rcpp::List trees, Rcpp::NumericVector
>> xx_vec,
>> Rcpp::NumericVector s_vec)
>>
>> /Users/garyzhubc/Work/dst-1/src/RcppExports.cpp updated.
>> /Users/garyzhubc/Work/dst-1/R/RcppExports.R updated.
>> ℹ Re-compiling dst
>> ── R CMD INSTALL
>> ────────────────────────────────────────────────────────────
>> ──────────────────────────
>> ─  installing *source* package ‘dst’ ...
>>    ** using staged installation
>>    ** libs
>>    using C++ compiler: ‘Apple clang version 17.0.0 (clang-1700.0.13.3)’
>>    using SDK: ‘’
>>    clang++ -arch x86_64 -std=gnu++17
>> -I"/Library/Frameworks/R.framework/Resources/include" -DNDEBUG
>>  -I'/Library/Frameworks/R.framework/Versions/4.4-x86_64/
>> Resources/library/Rcpp/include'
>> -I'/Library/Frameworks/R.framework/Versions/4.4-x86_64/
>> Resources/library/BH/include'
>> -I'/Library/Frameworks/R.framework/Versions/4.4-x86_64/Resources/library/
>> RcppProgress/include'
>> -I'/Library/Frameworks/R.framework/Versions/4.4-x86_64/Resources/library/
>> RcppArmadillo/include'
>> -I/opt/R/x86_64/include   -O3 -fPIC  -falign-functions=64 -Wall -O3   -c
>> RcppExports.cpp -o RcppExports.o
>>
>>
>> On Wed, May 21, 2025 at 1:08 PM Peiyuan Zhu <garyzhubc using gmail.com> wrote:
>>
>> > The closest I got is the below after putting:
>> >
>> > PKG_CXXFLAGS = -O3
>> > PKG_CFLAGS = -O3
>> >
>> > in Makevars. Still I'm seeing "-falign-functions=64 -Wall -g -O2" after
>> > -O3 so the last flag always wins.
>> >
>> >
>> > > load_all(".")
>> > ℹ Loading dst
>> > Exports from /Users/garyzhubc/Work/dst-1/src/belplauHFast.cpp:
>> >    NumericMatrix belplauHFast(NumericVector MACC, const arma::sp_mat&
>> W2,
>> > NumericMatrix h, bool display_progress = false)
>> >
>> > Exports from /Users/garyzhubc/Work/dst-1/src/closure.cpp:
>> >    LogicalMatrix closure(IntegerMatrix ttx, bool computeJoin = true,
>> bool
>> > display_progress = false)
>> >
>> > Exports from /Users/garyzhubc/Work/dst-1/src/closureSparse.cpp:
>> >    arma::sp_mat closureSparse(arma::sp_mat ttx, bool computeJoin = true,
>> > bool display_progress = false)
>> >
>> > Exports from /Users/garyzhubc/Work/dst-1/src/commSparse.cpp:
>> >    NumericVector commSparse(arma::sp_mat x, arma::sp_mat x_c, double a,
>> > bool display_progress = false)
>> >
>> > Exports from /Users/garyzhubc/Work/dst-1/src/iotaSparse.cpp:
>> >    arma::sp_mat iotaSparse(arma::sp_mat tt, bool display_progress =
>> false)
>> >
>> > Exports from /Users/garyzhubc/Work/dst-1/src/treeFast.cpp:
>> >    SEXP buildTreeFast(const arma::sp_mat& tt, const Rcpp::NumericVector&
>> > q, bool display_progress = false, Rcpp::Nullable<Rcpp::IntegerVector>
>> > indices = R_NilValue)
>> >    SEXP updateTreeFast(SEXP tree_ptr, LogicalVector xx_vec,
>> LogicalVector
>> > s_vec)
>> >    SEXP supersetFast(SEXP node_ptr, LogicalVector z_vec)
>> >    NumericVector unravelTreeFast(SEXP tree_ptr)
>> >    List inspectNode(SEXP tree_ptr)
>> >    Rcpp::List inspectNodes(Rcpp::List trees)
>> >    Rcpp::List buildTreesFast(const arma::sp_mat& tt, const
>> > Rcpp::NumericVector& q)
>> >    Rcpp::NumericVector unravelTreesFast(Rcpp::List trees)
>> >    Rcpp::List updateTreesFast(Rcpp::List trees, Rcpp::NumericVector
>> > xx_vec, Rcpp::NumericVector s_vec)
>> >
>> > /Users/garyzhubc/Work/dst-1/src/RcppExports.cpp updated.
>> > /Users/garyzhubc/Work/dst-1/R/RcppExports.R updated.
>> > ℹ Re-compiling dst
>> > ── R CMD INSTALL
>> > ────────────────────────────────────────────────────────────
>> ──────────────────────────
>> > ─  installing *source* package ‘dst’ ... (1.4s)
>> >    ** using staged installation
>> >    ** libs
>> >    using C++ compiler: ‘Apple clang version 17.0.0 (clang-1700.0.13.3)’
>> >    using SDK: ‘’
>> >    clang++ -arch x86_64 -std=gnu++17
>> > -I"/Library/Frameworks/R.framework/Resources/include" -DNDEBUG
>> >  -I'/Library/Frameworks/R.framework/Versions/4.4-x86_64/
>> Resources/library/Rcpp/include'
>> > -I'/Library/Frameworks/R.framework/Versions/4.4-x86_64/
>> Resources/library/BH/include'
>> > -I'/Library/Frameworks/R.framework/Versions/4.4-x86_64/
>> Resources/library/RcppProgress/include'
>> > -I'/Library/Frameworks/R.framework/Versions/4.4-x86_64/
>> Resources/library/RcppArmadillo/include'
>> > -I/opt/R/x86_64/include   -O3 -fPIC  -falign-functions=64 -Wall -g -O2
>>  -c
>> > RcppExports.cpp -o RcppExports.o
>> >
>> >
>> >
>> > On Wed, May 21, 2025 at 12:57 PM Dirk Eddelbuettel <edd using debian.org>
>> wrote:
>> >
>> >>
>> >> On 21 May 2025 at 09:34, Peiyuan Zhu wrote:
>> >> | I'm happy to just use O3 but it sounds like this isn't something
>> doable
>> >> in C++.
>> >>
>> >> It is called -O3, not 'O3'. (Also Rcpp, not RCpp.)
>> >>
>> >> We do not know what you did because you never posted anything even
>> >> remotely
>> >> close to reproducible.
>> >>
>> >> | Maybe I should just give up RCpp and move the program entirely to C++
>> >> from now
>> >> | on.
>> >>
>> >> Whatever works for you. You could also look at some existing projects
>> and
>> >> see
>> >> what they do; I just did a quick recursive search below my git
>> directory
>> >> on
>> >> the machine and saw at least two packages set -O3.  So it works for
>> some
>> >> of us.
>> >>
>> >> Good luck either way.
>> >>
>> >> Best, Dirk
>> >>
>> >> --
>> >> dirk.eddelbuettel.com | @eddelbuettel | edd using debian.org
>> >>
>> >
>>
>>         [[alternative HTML version deleted]]
>>
>> ______________________________________________
>> R-package-devel using r-project.org mailing list
>> https://stat.ethz.ch/mailman/listinfo/r-package-devel
>>
>
>
> --
> Best,
> Kasper
>

	[[alternative HTML version deleted]]



More information about the R-package-devel mailing list