views:

106

answers:

2

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 editing 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.

Many thanks!

+1  A: 

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."

Matthew Flaschen
Append -O0 to what exactly? Just stick it on `$CPPFLAGS`?
mohawkjohn
@mohawk, it should actually be CXXFLAGS, which is the C++-specific variable. CPPFLAGS will be passed to the C++ compiler, but it's actually for pre-processor options in general. CXXFLAGS is also after CPPFLAGS in the [implicit rule](http://www.gnu.org/software/automake/manual/make/Catalogue-of-Rules.html#Catalogue-of-Rules).
Matthew Flaschen
Thanks very much. Learn something new everyday. =)
mohawkjohn
+1  A: 

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.

Dmitry Yudakov