Explicitly setting compiler to C++ in extconf.rb

I’m trying to setup a Ruby gem that bundles the Swig-generated bindings
for Qpid, along with native Ruby code on top of those bindings. In
extconf.rb I have directives to verify that the header files and
libraries are present. For example:

abort “Missing address header” unless have_header
“qpid/messaging/Address.h”

This line always fails, even though the header file is present. It
fails with this message:

have_header: checking for qpid/messaging/Address.h…
-------------------- no

“gcc -E -I. -I/usr/lib64/ruby/1.8/x86_64-linux -I.
-I/home/mcpierce/Programming/Qpid/qpid/cpp/include/ -DRICE -fPIC
-fno-inline conftest.c -o conftest.i”
In file included from
/home/mcpierce/Programming/Qpid/qpid/cpp/include/qpid/messaging/exceptions.h:26:0,
from
/home/mcpierce/Programming/Qpid/qpid/cpp/include/qpid/messaging/Address.h:26,
from conftest.c:1:
/home/mcpierce/Programming/Qpid/qpid/cpp/include/qpid/types/Exception.h:25:18:
fatal error: string: No such file or directory
compilation terminated.
checked program was:
/* begin /
1: #include <qpid/messaging/Address.h>
/
end */

Specifically, it’s failing since, at line 25 of Exception.h, is:

#include

which is a Standard C++ header. But the Makefile generated is always
using gcc (rather than g++) to check. If I dump the checks for the
headers then everything works, but that’s not the Right Way™ to me to
do this.

Any help on how to tell Ruby to use g++?

Darryl L. Pierce escreveu isso a:

Specifically, it’s failing since, at line 25 of Exception.h, is:

#include

which is a Standard C++ header. But the Makefile generated is always
using gcc (rather than g++) to check. If I dump the checks for the
headers then everything works, but that’s not the Right Way™ to me to
do this.

Any help on how to tell Ruby to use g++?

You can try something like this:

require ‘rbconfig’
RbConfig::CONFIG[‘CPP’] = RbConfig::CONFIG[‘CPP’].gsub(‘gcc’, ‘g++’)
require ‘mkmf’
have_header(‘string’) or raise('You need ')
create_makefile(‘test’)

On Jun 23, 2011, at 7:30 PM, Antonio Terceiro wrote:

require ‘rbconfig’
RbConfig::CONFIG[‘CPP’] = RbConfig::CONFIG[‘CPP’].gsub(‘gcc’, ‘g++’)
require ‘mkmf’
have_header(‘string’) or raise(‘You need ’)
create_makefile(‘test’)

Don’t do this. The following is sufficient:

require ‘mkmf’
have_library(‘stdc++’)
create_makefile(‘laser/BasicBlock’)

That’s a working extconf.rb for a gem I’m actively working on. Adding
stdc++
makes mkmf use g++ on my system.

Michael E.
[email protected]
http://carboni.ca/

On 06/23/2011 07:39 PM, Michael E. wrote:

require ‘mkmf’
have_library(‘stdc++’)
create_makefile(‘laser/BasicBlock’)

That’s a working extconf.rb for a gem I’m actively working on. Adding stdc++
makes mkmf use g++ on my system.

Hrm, no joy. It’s still failing with the same error in mkmf.log.

On Fri, Jun 24, 2011 at 11:22:22AM +0900, Michael E. wrote:

Hrm, no joy. It’s still failing with the same error in mkmf.log.

Just checked, and you’re right - same thing happens to me if I insert
a “have_header” for one of my header files. It’s failing because mkmf
is creating a file called “conftest.c” and not “conftest.cc” - changing the
name of the file makes it work. Ugh.

It looks like a bug in mkmf.rb:407, in “cpp_command,” it uses CONFTEST_C
which ends in .c .

Ugh. So, for now, I’ll have to ignore checking for the headers and just
file a bug against mkmf to support C++ header checks.

On Jun 23, 2011, at 10:07 PM, Darryl L. Pierce wrote:

On 06/23/2011 07:39 PM, Michael E. wrote:

require ‘mkmf’
have_library(‘stdc++’)
create_makefile(‘laser/BasicBlock’)

That’s a working extconf.rb for a gem I’m actively working on. Adding stdc++
makes mkmf use g++ on my system.

Hrm, no joy. It’s still failing with the same error in mkmf.log.

Just checked, and you’re right - same thing happens to me if I insert
a “have_header” for one of my header files. It’s failing because mkmf
is creating a file called “conftest.c” and not “conftest.cc” - changing
the
name of the file makes it work. Ugh.

It looks like a bug in mkmf.rb:407, in “cpp_command,” it uses CONFTEST_C
which ends in .c .

Michael E.
[email protected]
http://carboni.ca/

On Fri, Jun 24, 2011 at 11:22:22AM +0900, Michael E. wrote:

Hrm, no joy. It’s still failing with the same error in mkmf.log.

Just checked, and you’re right - same thing happens to me if I insert
a “have_header” for one of my header files. It’s failing because mkmf
is creating a file called “conftest.c” and not “conftest.cc” - changing the
name of the file makes it work. Ugh.

It looks like a bug in mkmf.rb:407, in “cpp_command,” it uses CONFTEST_C
which ends in .c .

I see you already reported this issue [1]. Thanks. :slight_smile:

[1] Feature #4924: mkmf have_header fails with C++ headers - Ruby master - Ruby Issue Tracking System

Michael E. escreveu isso a:

require ‘mkmf’
have_library(‘stdc++’)
create_makefile(‘laser/BasicBlock’)

That’s a working extconf.rb for a gem I’m actively working on. Adding stdc++
makes mkmf use g++ on my system.

Actualy mkmf uses g++ for C++ code (.cc,.cpp), and not because you
checked for ‘stdc++’

The original problem is not checking for a C++ standard header, is
checking for a header that is actually a C++ header that happens to
#include a C++ standard header. Doing that with gcc -E instead of g++ -E will just not work.

Of course, mkmf could provide a way to say “please check for everything
using C++ instead of C”.

On Jun 24, 2011, at 11:50 , Antonio Terceiro wrote:

The original problem is not checking for a C++ standard header, is
checking for a header that is actually a C++ header that happens to
#include a C++ standard header. Doing that with gcc -E instead of g++ -E will just not work.

Of course, mkmf could provide a way to say “please check for everything
using C++ instead of C”.

mkmf provides:

with_cflags(flags) # :yields:

so you should be able to do something like

with_cflags("-x c++") do

end

On Sat, Jun 25, 2011 at 05:22:12AM +0900, Ryan D. wrote:

mkmf provides:

with_cflags(flags) # :yields:

so you should be able to do something like

with_cflags("-x c++") do

end

That did the trick! I added “-x c++” to my $CFLAGS and the system looked
for the headers correctly.

Thank you.