2

I am trying to write a c++ extension for Ruby. In addition to the main file, I have a file extconf.rb:

require "mkmf"
$libs += " -lstdc++ "
create_makefile("file_name")

and, after doing ruby extconf.rb, when I try to compile it with g++ by typing make, I get the warning:

cc1plus: warning: command line option "-Wdeclaration-after-statement" is valid for C/ObjC but not for C++

I read that it is not harmful, but is there a way to avoid this warning? There is a person with the same problem here, but the solution cannot be found.

sawa
  • 165,429
  • 45
  • 277
  • 381

2 Answers2

9

Try this in your extconf.rb:

$warnflags.gsub!('-Wdeclaration-after-statement', '') if $warnflags

the if $warnflags is needed because mkmf changed in Ruby 1.9.3; without it you'll get undefined method `gsub!' for nil:NilClass if you try to build on Ruby 1.9.2. You shouldn’t get the c++ warning in 1.9.2 though: the default warnings Ruby uses changed in 1.9.3 and these have been added.

Update:

This is probably better:

CONFIG['warnflags'].gsub!('-Wdeclaration-after-statement', '')

$warnflags gets populated from this, but this doesn't need the if $warnflags for < 1.9.3.

matt
  • 78,533
  • 8
  • 163
  • 197
  • I did that, and the warning message reduced from giving two of them to one, but I still get one. – sawa Apr 15 '12 at 19:34
  • @sawa the same warning (`declaration-after-statement`), or a different one? – matt Apr 15 '12 at 19:44
  • Even with `CONFIG['warnflags']`..., I still get one warning. – sawa Apr 16 '12 at 00:05
  • @sawa Where in the generated makefile do you delete `-Wdeclaration-after-statement`? Does it appear twice in there? – matt Apr 16 '12 at 00:15
  • I didn't delete it int the Makefile. I put the line that you suggest in extconf.rb. – sawa Apr 16 '12 at 01:24
  • @sawa I mean in the other solution above, where in the Makefile does the line appear. On my machine '-Wdeclaration-after-statement' appears once in `warnflags` in the Makefile without my fix. I'm wondering if your system has that flag set somewhere else that `mkmf` is picking up. – matt Apr 16 '12 at 01:42
  • I was wrong. They were two different warning messages. I did `sub!` to remove both of them, and they disappeared. Thanks for the help. – sawa Apr 16 '12 at 19:29
4

Edit the resulting Makefile created after running create_makefile and remove the -Wdeclaration-after-statement from there.

Mahmoud Al-Qudsi
  • 28,357
  • 12
  • 85
  • 125
  • Is there a way to minimally modify some related code and do that automatically? After reading your answer, I am looking at `mkmf.rb`, but can't find anything that says `Wdeclaration-after-statement`. – sawa Apr 15 '12 at 18:57
  • I tried changing ` config["CPPFLAGS"]`, but it still gives warning. But, thanks. – sawa Apr 16 '12 at 00:07