日記はScrapboxに移動しました。

XSでC++を使おうとしたけど挫折したので教えてクン><

追記

コメントでご教示いただいた通り、とりあえず#include を、ファイルの一番最初にもっていけば、エラーは起こらなくなるようです。id:tokuhiromさん、通りすがりさん、ありがとうございました!!1

C++の勉強がてら、XSでC++ライブラリのバインディングを作ってみようと思いました。しかし、超初歩的なところでつまづいてしまいました……。どうすればいいかさっぱりわからない><。。。そこで偉いひとの降臨を待つことにしました。誰か教えてくれくれクンだお><。。。

概要

MacOSX/Debian双方において、UncoというモジュールのためにUnco.xsを書いてるとして、下記のようにC++ライブラリのヘッダファイルを

#include <iostream>

とかしてincludeした途端、いろいろうまくいかなくなる。環境については、このエントリの下部に記載。

やったこと

準備する。

$ h2xs -An Unco

Makefile.PLを以下のように編集。

use 5.008008;
use ExtUtils::MakeMaker;
# See lib/ExtUtils/MakeMaker.pm for details of how to influence
# the contents of the Makefile that is written.
WriteMakefile(
NAME              => 'Unco',
VERSION_FROM      => 'lib/Unco.pm', # finds $VERSION
PREREQ_PM         => {}, # e.g., Module::Name => 1.1
($] >= 5.005 ?     ## Add these new keywords supported since 5.005
(ABSTRACT_FROM  => 'lib/Unco.pm', # retrieve abstract from module
AUTHOR         => 'antipop <antipop@local>') : ()),
LIBS              => [''], # e.g., '-lm'
DEFINE            => '', # e.g., '-DHAVE_SOMETHING'
INC               => '-I.', # e.g., '-I. -I/usr/include/other'
# Un-comment this if you add C files to link with later:
# OBJECT            => '$(O_FILES)', # link all the C files too
CC    => 'g++',   # ここから3つが新たにつけくわえたもの
LD    => 'g++',
XSOPT => '-C++',
);

Unco.xsを編集する。

#ifdef __cplusplus
extern "C" {
#endif
#include "EXTERN.h"
#include "perl.h"
#include "XSUB.h"
#ifdef __cplusplus
}
#endif
MODULE = Unco  PACKAGE = Unco
void
hello()
CODE:
printf("Hello, Unco!!1\n");

ビルドして使ってみる。

$ perl Makefile.PL
Writing Makefile for Unco
$ make
/opt/local/bin/perl /opt/local/lib/perl5/5.8.8/ExtUtils/xsubpp  -C++ -typemap /opt/local/lib/perl5/5.8.8/ExtUtils/typemap  Unco.xs > Unco.xsc && mv Unco.xsc Unco.c
Please specify prototyping behavior for Unco.xs (see perlxs manual)
g++ -c  -I. -I/opt/local/include -fno-common -DPERL_DARWIN -no-cpp-precomp -fno-strict-aliasing -pipe -Wdeclaration-after-statement -I/usr/local/include -I/opt/local/include -O3   -DVERSION=\"0.01\" -DXS_VERSION=\"0.01\"  "-I/opt/local/lib/perl5/5.8.8/darwin-thread-multi-2level/CORE"   Unco.c
cc1plus: warning: command line option "-Wdeclaration-after-statement" is valid for C/ObjC but not for C++
Running Mkbootstrap for Unco ()
chmod 644 Unco.bs
rm -f blib/arch/auto/Unco/Unco.bundle
g++  -L/opt/local/lib -bundle -undefined dynamic_lookup -L/usr/local/lib Unco.o  -o blib/arch/auto/Unco/Unco.bundle     \
\
chmod 755 blib/arch/auto/Unco/Unco.bundle
cp Unco.bs blib/arch/auto/Unco/Unco.bs
chmod 644 blib/arch/auto/Unco/Unco.bs
Manifying blib/man3/Unco.3
$ perl -Ilib -MExtUtils::testlib -MUnco -e 'Unco::hello()'
Hello, Unco!!1

ここまではうまく行くのだけど、Unco.xsで以下のようにC++ライブラリをincludeした途端、ダメになる。

#ifdef __cplusplus
extern "C" {
#endif
#include "EXTERN.h"
#include "perl.h"
#include "XSUB.h"
#ifdef __cplusplus
}
#endif
#include <iostream>
MODULE = Unco  PACKAGE = Unco
void
hello()
CODE:
printf("Hello, Unco!!1\n");

MacOSXの場合。

$ perl Makefile.PL
Writing Makefile for Unco
$ make
/opt/local/bin/perl /opt/local/lib/perl5/5.8.8/ExtUtils/xsubpp  -C++ -typemap /opt/local/lib/perl5/5.8.8/ExtUtils/typemap  Unco.xs > Unco.xsc && mv Unco.xsc Unco.c
Please specify prototyping behavior for Unco.xs (see perlxs manual)
g++ -c  -I. -I/opt/local/include -fno-common -DPERL_DARWIN -no-cpp-precomp -fno-strict-aliasing -pipe -Wdeclaration-after-statement -I/usr/local/include -I/opt/local/include -O3   -DVERSION=\"0.01\" -DXS_VERSION=\"0.01\"  "-I/opt/local/lib/perl5/5.8.8/darwin-thread-multi-2level/CORE"   Unco.c
cc1plus: warning: command line option "-Wdeclaration-after-statement" is valid for C/ObjC but not for C++
In file included from /usr/include/c++/4.0.0/bits/basic_ios.h:44,
from /usr/include/c++/4.0.0/ios:50,
from /usr/include/c++/4.0.0/ostream:44,
from /usr/include/c++/4.0.0/iostream:44,
from Unco.xs:13:
/usr/include/c++/4.0.0/bits/locale_facets.h:4314:40: error: macro "do_open" requires 7 arguments, but only 2 given
/usr/include/c++/4.0.0/bits/locale_facets.h:4361:34: error: macro "do_close" requires 2 arguments, but only 1 given
/usr/include/c++/4.0.0/bits/locale_facets.h:4380:55: error: macro "do_open" requires 7 arguments, but only 2 given
/usr/include/c++/4.0.0/bits/locale_facets.h:4407:23: error: macro "do_close" requires 2 arguments, but only 1 given
In file included from /usr/include/c++/4.0.0/bits/locale_facets.h:4491,
from /usr/include/c++/4.0.0/bits/basic_ios.h:44,
from /usr/include/c++/4.0.0/ios:50,
from /usr/include/c++/4.0.0/ostream:44,
from /usr/include/c++/4.0.0/iostream:44,
from Unco.xs:13:
/usr/include/c++/4.0.0/i686-apple-darwin9/bits/messages_members.h:51:38: error: macro "do_open" requires 7 arguments, but only 2 given
/usr/include/c++/4.0.0/i686-apple-darwin9/bits/messages_members.h:60:71: error: macro "do_open" requires 7 arguments, but only 2 given
/usr/include/c++/4.0.0/i686-apple-darwin9/bits/messages_members.h:71:39: error: macro "do_close" requires 2 arguments, but only 1 given
/usr/include/c++/4.0.0/bits/locale_facets.h:4380: error: ‘do_open’ declared as a ‘virtual’ field
/usr/include/c++/4.0.0/bits/locale_facets.h:4380: error: expected ‘;’ before ‘const’
/usr/include/c++/4.0.0/bits/locale_facets.h:4407: error: variable or field ‘do_close’ declared void
/usr/include/c++/4.0.0/bits/locale_facets.h:4407: error: ‘do_close’ declared as a ‘virtual’ field
/usr/include/c++/4.0.0/bits/locale_facets.h:4407: error: expected ‘;’ before ‘const’
/usr/include/c++/4.0.0/i686-apple-darwin9/bits/messages_members.h:60: error: expected initializer before ‘const’
/usr/include/c++/4.0.0/i686-apple-darwin9/bits/messages_members.h:71: error: expected initializer before ‘const’
make: *** [Unco.o] Error 1

Debianの場合。

$ perl Makefile.PL
Checking if your kit is complete...
Looks good
Writing Makefile for Unco
~/tmp/Unco
$ make
cp lib/Unco.pm blib/lib/Unco.pm
/usr/bin/perl /home/kentaro/local/lib/perl5/5.8.8/ExtUtils/xsubpp  -C++ -typemap /usr/share/perl/5.8/ExtUtils/typemap  Unco.xs > Unco.xsc && mv Unco.xsc Unco.c
Please specify prototyping behavior for Unco.xs (see perlxs manual)
g++ -c  -I. -D_REENTRANT -D_GNU_SOURCE -DTHREADS_HAVE_PIDS -DDEBIAN -fno-strict-aliasing -pipe -I/usr/local/include -D_LARGEFILE_SOURCE -D_FILE_OFFSET_BITS=64 -O2   -DVERSION=\"0.01\" -DXS_VERSION=\"0.01\" -fPIC "-I/usr/lib/perl/5.8/CORE"   Unco.c
In file included from /usr/include/c++/4.2/bits/basic_ios.h:44,
from /usr/include/c++/4.2/ios:50,
from /usr/include/c++/4.2/ostream:45,
from /usr/include/c++/4.2/iostream:45,
from Unco.xs:13:
/usr/include/c++/4.2/bits/locale_facets.h:4420:40: error: macro "do_open" requires 7 arguments, but only 2 given
/usr/include/c++/4.2/bits/locale_facets.h:4467:34: error: macro "do_close" requires 2 arguments, but only 1 given
/usr/include/c++/4.2/bits/locale_facets.h:4486:55: error: macro "do_open" requires 7 arguments, but only 2 given
/usr/include/c++/4.2/bits/locale_facets.h:4513:23: error: macro "do_close" requires 2 arguments, but only 1 given
In file included from /usr/include/c++/4.2/bits/locale_facets.h:4599,
from /usr/include/c++/4.2/bits/basic_ios.h:44,
from /usr/include/c++/4.2/ios:50,
from /usr/include/c++/4.2/ostream:45,
from /usr/include/c++/4.2/iostream:45,
from Unco.xs:13:
/usr/include/c++/4.2/i486-linux-gnu/bits/messages_members.h:70:38: error: macro "do_open" requires 7 arguments, but only 2 given
/usr/include/c++/4.2/i486-linux-gnu/bits/messages_members.h:85:23: error: macro "do_open" requires 7 arguments, but only 2 given
/usr/include/c++/4.2/i486-linux-gnu/bits/messages_members.h:95:39: error: macro "do_close" requires 2 arguments, but only 1 given
In file included from /usr/include/c++/4.2/bits/basic_ios.h:44,
from /usr/include/c++/4.2/ios:50,
from /usr/include/c++/4.2/ostream:45,
from /usr/include/c++/4.2/iostream:45,
from Unco.xs:13:
/usr/include/c++/4.2/bits/locale_facets.h:4486: error: ‘do_open’ declared as a ‘virtual’ field
/usr/include/c++/4.2/bits/locale_facets.h:4486: error: expected ‘;’ before ‘const’
/usr/include/c++/4.2/bits/locale_facets.h:4513: error: variable or field ‘do_close’ declared void
/usr/include/c++/4.2/bits/locale_facets.h:4513: error: expected ‘;’ before ‘const’
In file included from /usr/include/c++/4.2/bits/locale_facets.h:4599,
from /usr/include/c++/4.2/bits/basic_ios.h:44,
from /usr/include/c++/4.2/ios:50,
from /usr/include/c++/4.2/ostream:45,
from /usr/include/c++/4.2/iostream:45,
from Unco.xs:13:
/usr/include/c++/4.2/i486-linux-gnu/bits/messages_members.h:84: error: expected initializer before ‘const’
/usr/include/c++/4.2/i486-linux-gnu/bits/messages_members.h:95: error: expected initializer before ‘const’
Unco.c: In function ‘void boot_Unco(PerlInterpreter*, CV*)’:
Unco.c:60: warning: deprecated conversion from string constant to ‘char*’
Unco.c:66: warning: deprecated conversion from string constant to ‘char*’
make: *** [Unco.o] エラー 1

MacOSXの環境

$ perl -V
Summary of my perl5 (revision 5 version 8 subversion 8) configuration:
Platform:
osname=darwin, osvers=9.3.0, archname=darwin-thread-multi-2level
uname='darwin kaho.local 9.3.0 darwin kernel version 9.3.0: fri may 23 00:49:16 pdt 2008; root:xnu-1228.5.18~1release_i386 i386 '
config_args='-Dusethreads -Duseshrplib -des -Dprefix=/opt/local -Dccflags=-I'/opt/local/include' -Dldflags=-L/opt/local/lib -Dvendorprefix=/opt/local'
hint=recommended, useposix=true, d_sigaction=define
usethreads=define use5005threads=undef useithreads=define usemultiplicity=define
useperlio=define d_sfio=undef uselargefiles=define usesocks=undef
use64bitint=undef use64bitall=undef uselongdouble=undef
usemymalloc=n, bincompat5005=undef
Compiler:
cc='cc', ccflags ='-I/opt/local/include -fno-common -DPERL_DARWIN -no-cpp-precomp -fno-strict-aliasing -pipe -Wdeclaration-after-statement -I/usr/local/include -I/opt/local/include',
optimize='-O3',
cppflags='-no-cpp-precomp -I/opt/local/include -fno-common -DPERL_DARWIN -no-cpp-precomp -fno-strict-aliasing -pipe -Wdeclaration-after-statement -I/usr/local/include -I/opt/local/include'
ccversion='', gccversion='4.0.1 (Apple Inc. build 5465)', gccosandvers=''
intsize=4, longsize=4, ptrsize=4, doublesize=8, byteorder=1234
d_longlong=define, longlongsize=8, d_longdbl=define, longdblsize=16
ivtype='long', ivsize=4, nvtype='double', nvsize=8, Off_t='off_t', lseeksize=8
alignbytes=8, prototype=define
Linker and Libraries:
ld='env MACOSX_DEPLOYMENT_TARGET=10.3 cc', ldflags ='-L/opt/local/lib -L/usr/local/lib'
libpth=/usr/local/lib /opt/local/lib /usr/lib
libs=-ldbm -ldl -lm -lutil -lc
perllibs=-ldl -lm -lutil -lc
libc=/usr/lib/libc.dylib, so=dylib, useshrplib=true, libperl=libperl.dylib
gnulibc_version=''
Dynamic Linking:
dlsrc=dl_dlopen.xs, dlext=bundle, d_dlsymun=undef, ccdlflags=' '
cccdlflags=' ', lddlflags='-L/opt/local/lib -bundle -undefined dynamic_lookup -L/usr/local/lib'
Characteristics of this binary (from libperl):
Compile-time options: MULTIPLICITY PERL_IMPLICIT_CONTEXT
PERL_MALLOC_WRAP USE_ITHREADS USE_LARGE_FILES
USE_PERLIO USE_REENTRANT_API
Built under darwin
Compiled at Jun 16 2008 14:32:33
%ENV:
PERL5LIB="/Users/antipop/local/lib/perl5/5.8.8:/Users/antipop/local/lib/perl/5.8.8:/Users/antipop/local/share/perl/5.8.8:/Users/antipop/local/lib/perl5:/Users/antipop/dev/mylib/perl"
PERL5_CPANPLUS_CONFIG="/Users/antipop/.cpanplus/config"
@INC:
/Users/antipop/local/lib/perl5/5.8.8
/Users/antipop/local/lib/perl/5.8.8
/Users/antipop/local/share/perl/5.8.8
/Users/antipop/local/lib/perl5
/Users/antipop/dev/mylib/perl
/opt/local/lib/perl5/5.8.8/darwin-thread-multi-2level
/opt/local/lib/perl5/5.8.8
/opt/local/lib/perl5/site_perl/5.8.8/darwin-thread-multi-2level
/opt/local/lib/perl5/site_perl/5.8.8
/opt/local/lib/perl5/site_perl
/opt/local/lib/perl5/vendor_perl/5.8.8/darwin-thread-multi-2level
/opt/local/lib/perl5/vendor_perl/5.8.8
/opt/local/lib/perl5/vendor_perl
.
$ g++ --version
i686-apple-darwin9-g++-4.0.1 (GCC) 4.0.1 (Apple Inc. build 5484)
Copyright (C) 2005 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

Debianの環境

$ perl -V
Summary of my perl5 (revision 5 version 8 subversion 8) configuration:
Platform:
osname=linux, osvers=2.6.22-3-amd64, archname=i486-linux-gnu-thread-multi
uname='linux deneb 2.6.22-3-amd64 #1 smp thu oct 11 15:23:23 utc 2007 i686 gnulinux '
config_args='-Dusethreads -Duselargefiles -Dccflags=-DDEBIAN -Dcccdlflags=-fPIC -Darchname=i486-linux-gnu -Dprefix=/usr -Dprivlib=/usr/share/perl/5.8 -Darchlib=/usr/lib/perl/5.8 -Dvendorprefix=/usr -Dvendorlib=/usr/share/perl5 -Dvendorarch=/usr/lib/perl5 -Dsiteprefix=/usr/local -Dsitelib=/usr/local/share/perl/5.8.8 -Dsitearch=/usr/local/lib/perl/5.8.8 -Dman1dir=/usr/share/man/man1 -Dman3dir=/usr/share/man/man3 -Dsiteman1dir=/usr/local/man/man1 -Dsiteman3dir=/usr/local/man/man3 -Dman1ext=1 -Dman3ext=3perl -Dpager=/usr/bin/sensible-pager -Uafs -Ud_csh -Uusesfio -Uusenm -Duseshrplib -Dlibperl=libperl.so.5.8.8 -Dd_dosuid -des'
hint=recommended, useposix=true, d_sigaction=define
usethreads=define use5005threads=undef useithreads=define usemultiplicity=define
useperlio=define d_sfio=undef uselargefiles=define usesocks=undef
use64bitint=undef use64bitall=undef uselongdouble=undef
usemymalloc=n, bincompat5005=undef
Compiler:
cc='cc', ccflags ='-D_REENTRANT -D_GNU_SOURCE -DTHREADS_HAVE_PIDS -DDEBIAN -fno-strict-aliasing -pipe -I/usr/local/include -D_LARGEFILE_SOURCE -D_FILE_OFFSET_BITS=64',
optimize='-O2',
cppflags='-D_REENTRANT -D_GNU_SOURCE -DTHREADS_HAVE_PIDS -DDEBIAN -fno-strict-aliasing -pipe -I/usr/local/include'
ccversion='', gccversion='4.1.2 20061115 (prerelease) (Debian 4.1.1-21)', gccosandvers=''
intsize=4, longsize=4, ptrsize=4, doublesize=8, byteorder=1234
d_longlong=define, longlongsize=8, d_longdbl=define, longdblsize=12
ivtype='long', ivsize=4, nvtype='double', nvsize=8, Off_t='off_t', lseeksize=8
alignbytes=4, prototype=define
Linker and Libraries:
ld='cc', ldflags =' -L/usr/local/lib'
libpth=/usr/local/lib /lib /usr/lib
libs=-lgdbm -lgdbm_compat -ldb -ldl -lm -lpthread -lc -lcrypt
perllibs=-ldl -lm -lpthread -lc -lcrypt
libc=/lib/libc-2.3.6.so, so=so, useshrplib=true, libperl=libperl.so.5.8.8
gnulibc_version='2.3.6'
Dynamic Linking:
dlsrc=dl_dlopen.xs, dlext=so, d_dlsymun=undef, ccdlflags='-Wl,-E'
cccdlflags='-fPIC', lddlflags='-shared -L/usr/local/lib'
Characteristics of this binary (from libperl):
Compile-time options: MULTIPLICITY PERL_IMPLICIT_CONTEXT
PERL_MALLOC_WRAP THREADS_HAVE_PIDS USE_ITHREADS
USE_LARGE_FILES USE_PERLIO USE_REENTRANT_API
Built under linux
Compiled at Nov  5 2007 06:11:48
%ENV:
PERL5LIB="/home/kentaro/local/lib/perl5/5.8.8:/home/kentaro/local/lib/perl/5.8.8:/home/kentaro/local/share/perl/5.8.8:/home/kentaro/local/lib/perl5:/home/kentaro/dev/mylib/perl"
PERL5_CPANPLUS_CONFIG="/home/kentaro/.cpanplus/config"
@INC:
/home/kentaro/local/lib/perl5/5.8.8/i486-linux-gnu-thread-multi
/home/kentaro/local/lib/perl5/5.8.8
/home/kentaro/local/lib/perl/5.8.8
/home/kentaro/local/share/perl/5.8.8
/home/kentaro/local/lib/perl5/5.8.8/i486-linux-gnu-thread-multi
/home/kentaro/local/lib/perl5/5.8.8
/home/kentaro/local/lib/perl5/i486-linux-gnu-thread-multi
/home/kentaro/local/lib/perl5
/home/kentaro/dev/mylib/perl
/etc/perl
/usr/local/lib/perl/5.8.8
/usr/local/share/perl/5.8.8
/usr/lib/perl5
/usr/share/perl5
/usr/lib/perl/5.8
/usr/share/perl/5.8
/usr/local/lib/site_perl
.
$ g++ --version
g++ (GCC) 4.2.3 (Debian 4.2.3-5)
Copyright (C) 2007 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

4 comments

  1. 時間なくてためしてみたわけじゃないので、勘ですが、#include <iostream> を一番先頭にもってきたらなおるかも。
    perl のマクロでぶっこわれてるかもしれません

  2. 一番先頭に持ってってみたところ、エラーなくmakeできました!!1ありがとうございます!!1
    しかし、他のC++なXSモジュールを見ると特にそのようなことをしているわけでもないし、うちの環境がなんかおかしいのかなあ……。

  3. 多分ですが、Perl の h ファイルの embed.h の中で do_open と do_close が define されてるのが原因のように感じます。
    何をやられてるのは寡聞ながらわかりませんが、おそらく問題のソースから最終的に embed.h も include されるようになっていて、そちらが iostream が要求している定義と合わずにエラーになっているように見えます。ですので iostream を先頭に持っていくと iostream からの定義の方が先に include されて直る、というのは納得が行きます。
    最終的にどうすればよいのかまでちょっとわかりませんが。。。あんまり役に立ちませんね。。。

  4. 既に解決しているようですが、一番確実な方法は .xs は C で記述し、C++ のソースファイルは別に作成することだと思います。.xs と C++ 間の連携については、C (not C++) なヘッダファイルを準備し、それを双方からインクルードするようにします。そうすることで、プリプロセッサやコンパイルオプションの影響を抑えることができます。

Leave a Reply

Your email address will not be published. Required fields are marked *