swig生成的来自接口头文件的封装java文件为空

时间:2018-06-12 18:09:47

标签: cmake swig

我生成的example.java没有来自头文件中指定的接口的代码。 example.i:

%module example
%include "std_string.i"
%{
  #include "storagepoc/storagepoc.h"
%};

using namespace std;
typedef std::string String;
%include "storagepoc.h";

storagepoc.h:

#ifndef STORAGEPOC_H
#define STORAGEPOC_H

#include <string>

class Aws::S3::S3Client;

namespace storagepoc {

class StoragePOC {
  public:
     /* Constructor */
     StoragePOC(const std::string &options);

    /* Destructor */
    ~StoragePOC();

    /* Copy image */
    std::string CopyImage(const std::string &input_url, const std::string 
    &storage_url, const std::string &options);

  private:
      std::shared_ptr<Aws::S3::S3Client> m_client;
    };

    }
    #endif

的CMakeLists.txt:

SET(CMAKE_SWIG_FLAGS -package com.intel.proxy)
swig_add_library(example LANGUAGE java TYPE USE_BUILD_SHARED_LIBS SOURCES 
example.i)
SWIG_LINK_LIBRARIES(example storagepoc)

# For convenience we copy the dynamic library to the current build folder
add_custom_command(
TARGET example 
POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy_if_different $<TARGET_FILE:example> 
${CMAKE_CURRENT_BINARY_DIR}
)

# Build the Java code into ExampleJNI.jar
add_jar(
ExampleJNI
SOURCES
VERSION 0.0.1
${CMAKE_SWIG_OUTDIR}/example.java
${CMAKE_SWIG_OUTDIR}/exampleJNI.java
)
  add_dependencies( ExampleJNI example )
  #link ExampleJNI with its dependencies
  #target_link_libraries(ExampleJNI example)

# Finally build the demonstration code into Main.jar

  set(CMAKE_JAVA_JAR_ENTRY_POINT main) 
  add_jar(
Main 
SOURCES main.java
VERSION 0.0.1
ENTRY_POINT main
)
add_dependencies( Main ExampleJNI )

生成的example.java是空的??? exampleJNI.java具有生成的接口方法--StoragePOC_CopyImage ...

我之前的example.java正在使用

 %inline %{
     namespace jni {
       extern int foo();
     }
 %} 

这会生成一个example.java包装文件??

TIA

1 个答案:

答案 0 :(得分:0)

不得不将以下内容添加到我的example.i中:

%pragma(java) modulecode=%{

 public static String CopyImage(String input_url, String storage_url, String options) {
  return new StoragePOC(options).CopyImage(input_url, storage_url, options);
}

%}

第21.4.2.1节http://www.swig.org/Doc1.3/Java.htm