【问题标题】:SWIG shows error when wrapping C++ for Java为 Java 包装 C++ 时 SWIG 显示错误
【发布时间】:2015-03-18 20:51:48
【问题描述】:

我想使用 SWIG for java 包装一个 c++ 类。我遵循了official tutorialdocumentation 并试图让它工作,但我得到了一些错误。 我使用的是 Windows 7 x64。

于是我打开cmd输入:swig -c++ -java myclass.i

该命令执行并生成了几个文件。(它没有产生错误)

之后,我输入:gcc -c myclass_wrap.cxx -I"C:\Program Files\Java\jdk1.7.0_60\include" -I"C:\Program Files\Java\jdk1.7.0_60\include\win32" 该命令也成功了。 (没有产生错误)

最后,我输入了ld -G myclass_wrap.o -o libmyclass.so

产生了一堆未定义的引用错误,例如:

myclass_wrap.o:myclass_wrap.cxx:<.text+0xa8>: undefined reference to '__cxa_allocate_exception'

这是我最初的 c++ 代码,以下 c++ 类: 头文件:

/*myclass.h*/
#define MYCLASS_H
#ifndef MYCLASS_H
#include <vector>
#include <iostream>

class myClass
{
private:
    int ID;
    std::vector<int> vector;

public:
    myClass();
    myClass(int id, std::vector<int> v);

    void setID(int id);
    int getID();

    void setVector(std::vector<int> v);
    std::vector<int> getVector();

    void insertIntoVector(int num);
    void printVector();
};

#endif // MYCLASS_H

和cpp文件:

/*myclass.cpp*/
#include "myclass.h"
myClass::myClass()
{
    ID=0;
    vector=std::vector<int>();
}

myClass::myClass(int id, std::vector<int> v)
{
    ID=id;
    vector=v;
}

void myClass::setID(int id)
{
    ID=id;
}

int myClass::getID()
{
    return ID;
}


void myClass::setVector(std::vector<int> v)
{
    vector=v;
}
std::vector<int> myClass::getVector()
{
    return vector;
}

void myClass::insertIntoVector(int num)
{
    std::vector<int>::iterator it;
    it=vector.end();
    vector.insert(it,num);
}

void myClass::printVector()
{
    std::vector<int>::iterator it;
    for (it=vector.begin(); it<vector.end(); it++)
        std::cout << ' ' << *it<< std::endl;
}

还有 swig 模块文件:

/*myclass.i*/    
%module test
%{
#include "myclass.h"
%}

%include "std_vector.i"
namespace std {
   %template(IntVector) vector<int>;
}

%include "myclass.h"

【问题讨论】:

    标签: java c++ cmd swig


    【解决方案1】:

    之后,我输入:gcc -c myclass_wrap.cxx -I"C:\Program Files\Java\jdk1.7.0_60\include" -I"C:\Program Files\Java\jdk1.7.0_60\include\win32"

    它是 C++ 代码,因此通常更喜欢使用 g++ 而不是 gcc 进行编译(尽管并非绝对必要):

    g++ -c myclass_wrap.cxx -I"C:\Program Files\Java\jdk1.7.0_60\include" -I"C:\Program Files\Java\jdk1.7.0_60\include\win32"

    最后,我输入了ld -G myclass_wrap.o -o libmyclass.so

    产生了一堆未定义的引用错误,例如:

    myclass_wrap.o:myclass_wrap.cxx:<.text>: 未定义的引用 '__cxa_allocate_exception'

    从 SWIG 文档构建共享库的示例不适用于 C++(因此链接器找不到特定于 C++ 的符号),也不适用于 Windows。

    假设您使用的是 Mingw 版本的 gcc for Windows,请改用以下代码编译 DLL myclass.dll

    g++ -shared -o myclass.dll myclass_wrap.o

    有关使用 Mingw 构建 DLL 的更多信息,see this example on the Mingw site

    另请参阅this SWIG FAQ page,其中包含有关为各种平台和编译器构建共享库或 DLL 的信息的链接(但要注意许多示例命令是针对 C 而不是 C++)。

    【讨论】:

    • 感谢您的回答。我一回到家(几个小时内)就会尝试一下,然后我会发布结果。再次感谢。
    • 你好@softwariness,我做了你所说的一切,但是应该产生共享库g++ -shared -o myclass.dll myclass_wrap.o的命令会为 myclass.cpp 中的所有函数产生未定义的引用错误。最后,最终错误是Id returned 1 exit status。我做错了什么?
    • 我做到了! :) 。我用谷歌搜索了一下,我发现我不仅应该编译 example_wrap.cxx,还应该使用 g++ 编译器编译 example.cpp。我做到了,我不得不将最后一个命令修改为:g++ -shared -o myclass.dll myclass_wrap.o myclass.o,然后创建了 dll 文件:)。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-02-10
    • 2021-08-29
    • 1970-01-01
    • 2020-10-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多