在使用命名空间std的c ++头文件上运行SWIG时缺少分号错误

时间:2019-05-22 22:17:00

标签: java c++ swig

我想从使用命名空间std并包含一些库的c ++头文件生成Java的包装文件。当我尝试运行swig时,即使我的头文件来自大型代码库,并且可以正常运行,它也会给我带来语法错误,即说缺少分号。

我尝试在swig所需的.i文件中包括“使用命名空间std”行,还包括.h文件中包含的类似库,但是仍然存在相同的错误。

这是我的AdaptationPlanner.h文件

// some code ...
#include <string>
#include <vector>
#include <set>
using namespace std; //line 68
// more code ...

这是我的AdaptationPlanner.i文件

/* AdaptationPlanner.i */
%module AdaptationPlanner
%{
/* Includes the header in the wrapper code */
#include "AdaptationPlanner.h" 
%}

/* Parse the header file to generate wrappers */
%include "AdaptationPlanner.h"

这是swig给出的错误消息:

AdaptationPlanner.h:68: Error: Syntax error - possibly a missing semicolon.

3 个答案:

答案 0 :(得分:0)

import static org.hamcrest.CoreMatchers.hasItems;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.not;
import static org.hamcrest.collection.IsCollectionWithSize.hasSize;
import static org.hamcrest.collection.IsIterableContainingInOrder.contains;
import static org.junit.Assert.assertThat;    

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;    

import org.hamcrest.collection.IsEmptyCollection;
import org.junit.Test;    

public class AssertLists {    

     @Test
        public void testAssertList() {    

            List<Integer> actual = Arrays.asList(1,2,3,4,5,6);
            List<Integer> expected = Arrays.asList(1,2,3,4,5,6);


            //1. Test equal.
            assertThat(actual, is(expected));    


            //2. Check List Size
            assertThat(actual, hasSize(6));    

            assertThat(actual.size(), is(6));    

            //3.  List order    

            // Ensure Correct order
            assertThat(expected, contains(1,2,3,4,5,6));    

            // Can be any order
            assertThat(expected, hasItems(3,2,6));    

            //4. check empty list
            assertThat(actual, not(IsEmptyCollection.empty()));    

            assertThat(new ArrayList<>(), IsEmptyCollection.empty());    

        }    

}

我真的不能说出您为什么遇到这样的问题。但是,是否要尝试上述解决方案?只需在接口文件中明确提及名称空间即可。

答案 1 :(得分:0)

我有同样的错误。 getchar的建议无法解决。解决该问题的方法是将%module名称从“ AdaptationPlanner”重命名为“ AdaptationPlannerSwig”,以避免名称冲突。

答案 2 :(得分:0)

您必须在 swig 命令中使用 -c++。

示例: swig -java -package xxx.xxx.xxx.xxx -c++ AdaptationPlanner.i