スポンサーサイト
新しい記事を書く事で広告が消せます。
調べたことを忘れないために。
/* point.h */
class Point{
public:
Point(int, int);
int getX();
int getY();
private:
int x, y;
};
/* point.cpp */
#include "point.h"
Point::Point(int x, int y){
this->x = x;
this->y = y;
}
int Point::getX(){
return this->x;
}
int Point::getY(){
return this->y;
}
/* wrapper.cpp */
#include "point.h"
Point* createPoint(int x, int y){
return new Point(x, y);
}
int getPointX(Point* p){
return p->getX();
}
int getPointY(Point* p){
return p->getY();
}
void destroyPoint(Point* p){
delete p;
}
-- main.hs
{-# LANGUAGE ForeignFunctionInterface #-}
module Main where
import Foreign
import Foreign.Ptr
import Foreign.C.Types
data Point = Point
main = do
p <- c_createPoint 1000 2000
x <- c_getPointX p
y <- c_getPointY p
c_destroyPoint p
print x
print y
foreign import ccall "createPoint" c_createPoint :: CInt -> CInt -> IO (Ptr Point)
foreign import ccall "getPointX" c_getPointX :: Ptr Point -> IO CInt
foreign import ccall "getPointY" c_getPointY :: Ptr Point -> IO CInt
foreign import ccall "destroyPoint" c_destroyPoint :: Ptr Point -> IO ()
$ g++ -c point.cpp wrapper.cpp
$ ghc --make main.hs point.o wrapper.o
[1 of 1] Compiling Main ( main.hs, main.o )
Linking main ...
ld: warning: in point.o, file was built for unsupported file format which is not the architecture being linked (i386)
ld: warning: in wrapper.o, file was built for unsupported file format which is not the architecture being linked (i386)
Undefined symbols:
"_destroyPoint", referenced from:
_rKi_info in main.o
_sTt_info in main.o
"_getPointX", referenced from:
_rKg_info in main.o
_sTx_info in main.o
"_createPoint", referenced from:
_rKe_info in main.o
_sWN_info in main.o
ld: symbol(s) not found
collect2: ld returned 1 exit status
file was built for unsupported file format
$ g++ -c point.cpp wrapper.cpp -m32
$ ghc --make main.hs point.o wrapper.o
[1 of 1] Compiling Main ( main.hs, main.o )
Linking main ...
Undefined symbols:
"operator delete(void*)", referenced from:
_destroyPoint in wrapper.o
_createPoint in wrapper.o
"operator new(unsigned long)", referenced from:
_createPoint in wrapper.o
"___gxx_personality_v0", referenced from:
___gxx_personality_v0$non_lazy_ptr in wrapper.o
(maybe you meant: ___gxx_personality_v0$non_lazy_ptr)
ld: symbol(s) not found
collect2: ld returned 1 exit status
ghc --make main.hs -o hybrid_program -lstdc++ nifty_code.o
$ g++ -c point.cpp wrapper.cpp -m32
$ ghc -lstdc++ --make main.hs point.o wrapper.o
-- main.hs
{-# LANGUAGE ForeignFunctionInterface #-}
module Main where
main = c_mecabTest
foreign import ccall "mecabTest" c_mecabTest :: IO ()
/* wrapper.cpp */
#include <mecab.h>
#include <iostream>
extern "C" {
void mecabTest(){
char input[1024] = "太郎は次郎が持っている本を花子に渡した。";
MeCab::Tagger *tagger = MeCab::createTagger (0, 0);
const MeCab::Node* node = tagger->parseToNode(input);
for (; node; node = node->next) {
std::cout << node->id << ' ';
if (node->stat == MECAB_BOS_NODE)
std::cout << "BOS";
else if (node->stat == MECAB_EOS_NODE)
std::cout << "EOS";
else
std::cout.write (node->surface, node->length);
std::cout << ' ' << node->feature
<< ' ' << (int)(node->surface - input)
<< ' ' << (int)(node->surface - input + node->length)
<< ' ' << node->rcAttr
<< ' ' << node->lcAttr
<< ' ' << node->posid
<< ' ' << (int)node->char_type
<< ' ' << (int)node->stat
<< ' ' << (int)node->isbest
<< ' ' << node->alpha
<< ' ' << node->beta
<< ' ' << node->prob
<< ' ' << node->cost << std::endl;
}
delete tagger;
}
}
$ ghc --make main.hs wrapper.cpp /usr/local/lib/libmecab.dylib -lstdc++
$ ./main
0 BOS BOS/EOS,*,*,*,*,*,*,*,* 0 0 0 0 0 0 2 1 0 0 0 0
5 太郎 名詞,固有名詞,人名,名,*,*,太郎,タロウ,タロー 0 6 1291 1291 44 2 0 1 0 0 0 8614
8 は 助詞,係助詞,*,*,*,*,は,ハ,ワ 6 9 261 261 16 6 0 1 0 0 0 9699
15 次郎 名詞,固有名詞,人名,名,*,*,次郎,ジロウ,ジロー 9 15 1291 1291 44 2 0 1 0 0 0 20566
18 が 助詞,格助詞,一般,*,*,*,が,ガ,ガ 15 18 148 148 13 6 0 1 0 0 0 21070
22 持っ 動詞,自立,*,*,五段・タ行,連用タ接続,持つ,モッ,モッ 18 24 742 742 31 2 0 1 0 0 0 25702
31 て 助詞,接続助詞,*,*,*,*,て,テ,テ 24 27 307 307 18 6 0 1 0 0 0 24174
40 いる 動詞,非自立,*,*,一段,基本形,いる,イル,イル 27 33 919 919 33 6 0 1 0 0 0 24723
46 本 名詞,一般,*,*,*,*,本,ホン,ホン 33 36 1285 1285 38 2 0 1 0 0 0 29905
53 を 助詞,格助詞,一般,*,*,*,を,ヲ,ヲ 36 39 156 156 13 6 0 1 0 0 0 29095
54 花 名詞,一般,*,*,*,*,花,ハナ,ハナ 39 42 1285 1285 38 2 0 1 0 0 0 33000
63 子 名詞,接尾,一般,*,*,*,子,コ,コ 42 45 1298 1298 51 2 0 1 0 0 0 39627
65 に 助詞,格助詞,一般,*,*,*,に,ニ,ニ 45 48 151 151 13 6 0 1 0 0 0 39654
77 渡し 動詞,自立,*,*,五段・サ行,連用形,渡す,ワタシ,ワタシ 48 54 735 735 31 2 0 1 0 0 0 42751
86 た 助動詞,*,*,*,特殊・タ,基本形,た,タ,タ 54 57 435 435 25 6 0 1 0 0 0 41874
89 。 記号,句点,*,*,*,*,。,。,。 57 60 8 8 7 3 0 1 0 0 0 38440
91 EOS BOS/EOS,*,*,*,*,*,*,*,* 60 60 0 0 0 0 3 1 0 0 0 36904