How would I instruct extconf.rb to use additional g++ optimization flags, and which are advisable?
I'm using Rice to write a C++ extension for a Ruby gem. The extension is in the form of a shared object (.so) file.
This requires 'mkmf-rice' instead of 'mkmf', but the two (AFAIK) are pretty similar.
By default, the compiler uses the flags -g -O2
. Personally, I find this kind of silly, since it's hard to debug with any optimization enabled. I've resorted to editin开发者_运维百科g the Makefile to take out the flags I don't like (e.g., removing -fPIC -shared
when I need to debug using main()
instead of Ruby's hooks).
But I figure there's got to be a better way. I know I can just do
$CPPFLAGS += " -DRICE"
to add additional flags. But how do I remove things without editing the Makefile directly?
A secondary question: what optimizations are safe for shared objects loaded by Ruby? Can I do things like -funroll-loops
? What do you all recommend?
It's a scientific computing project, so the faster the better. Memory is not much of an issue.
To define you use
-D name=definition
like you do in your example:
$CPPFLAGS += " -DRICE"
-U name
Cancel any previous definition of name, either built in or provided with a -D option.
$CPPFLAGS += " -URICE"
Although I'm not sure if it'll help with undefining -O2 like you want it.
The quick and dirty way is to append -O0 to CXXFLAGS, which will turn off optimization. Later options will override earlier ones.
As far as safety for a plugin, you should be able to do anything that doesn't affect the ABI. Without testing, I don't see why -funroll would. Of course, safe does not imply better. As noted by the man page, "-funroll-loops makes code larger, and may or may not make it run faster."
精彩评论