我正在尝试使用SWIG链接C ++文件,但我遇到了绊脚石。
我有以下文件:
foo.cpp
foo.h
fooParser.cpp
fooParser.h
foo.swigcxx
foo_test.go
我可以正常运行go build
和go install
但是当我尝试运行go test
时,我收到了一堆类似于
/tmp/go-build806262998/github.com/foo/_test/_obj_test/foo.cpp.o: In function `foo::FooParser::parseX()':./FooParser.cpp:138: undefined reference to `foo::Foo<std::string>::Foo(std::string const&)'
collect2: error: ld returned 1 exit status
/usr/local/go/pkg/tool/linux_amd64/6l: running g++ failed: unsuccessful exit status 0x100
foo.swigcxx
%module foo
%{
#include "foo.h"
#include "fooParser.h"
%}
%include "std_string.i"
%include "foo.h"
%include "fooParser.h"
%template(FooString) Foo<std::string>;
foo.h中
#ifndef _H_Foo
#define _H_Foo
#include<string>
namespace foo {
template <class T>
class Foo
{
public:
Foo(const T &initValue = T());
...
};
}
fooParser.h
#ifndef _H_FooParser
#define _H_FooParser
#include "foo.h"
#include <string>
class FooParser
{
public:
...
Foo<std::string> *parse(const std::string &x) throw (Error);
...
private:
Foo<std::string> *parseX() throw(Error);
...
};
为什么我抱怨foo::Foo<std::string>::Foo(std::string const&)
%template(FooString) Foo<std::string>;