2017-01-20 8 views
-1

私はC++コード用のCMAKE-Fileを提供されていて、その機能をPythonにラップしたいと思っています。私はそれが私のインポートエラーを与えている機能をインポートしようとするたびに:SWIG + CMAKE:init関数がありません

動的モジュールは、init関数(init_main)

を定義していない。これは、C++のコードです:

#include <iostream> 
#include <string> 
#include <boost/bind.hpp> 
#include <Memory/Interface.hpp> 
#include <Memory/Subscription.hpp> 

using namespace memory::interface; 
using namespace memory; 

MemoryPtr mMem; 
Subscription *s; 

std::string write_document; 







void subscriber_callback(const Event &event) { 

    std::cout << "Received an event:" << std::endl; 
    switch (event.getType()) { 

     case Event::INSERT: 
       std::cout << "Insert event with document ID = " << event.getID() << std::endl; 
       break; 

     case Event::REMOVE: 
       std::cout << "Remove event with document ID = " << event.getID() << std::endl; 
       break; 

     case Event::QUERY: 
       std::cout << "Query event with document ID = " << event.getID() << std::endl; 
       break; 

     case Event::REPLACE: 
       std::cout << "Replace event with document ID = " << event.getID() << std::endl; 
       break; 

     case Event::ALL: 
       std::cout << "Generic event with document ID = " << event.getID() << std::endl; 
       break; 

     default: std::cout << "Unknown event type." << std::endl; 
    } 
    std::cout << "Contained document is: " << event.getDocument() << std::endl; 

} 



extern "C" int main(int argc, char *argv[]) { 
    // validate app arguments 
    if(argc < 3) { 
    std::cerr << "Usage : " << argv[0] << " <xcf:ShortTerm> <XPATH-trigger>" << std::endl; 
    return 1; 
    } 
    try { 
    // instantiate the memory interface 
    mMem = MemoryInterface::getInstance(argv[1]); 
    std::cout << "Memory interface initialized" << std::endl; 

    // create the trigger 
    std::string xpath(argv[2]); 

    // create a subscriber to a specific xpath event 
    s = new Subscription(
     Condition(Event::INSERT, xpath), 
     TriggeredAction(boost::bind(&subscriber_callback, _1)) 
    ); 
    mMem->subscribe (*s); 
    std::cout << "Memory interface subscriber initialized" << std::endl; 

    // insert command/text to memory 
    write_document = "<command><stop /></command>"; 
    mMem->insert(write_document); 
    std::cout << "Written to Memory interface" << std::endl; 

    // wait for key press 
    std::string end; 
    std::cin >> end; 
    } 
    catch (const MemoryInterfaceException& e) { 
    std::cerr << "MemoryInterfaceException: " << e.what() << std::endl; 
    return 1; 
    } 
    catch(std::exception &e) { 
    std::cerr << std::endl << "Could not initialize memory::interface. Reason:" 
    << std::endl << e.what() << std::endl; 
    return 1; 
    } 
    return 0; 
} 

これはCMAKEファイルでありますこれまで私は、最後のSWIGの一部を追加しました:

cmake_minimum_required(VERSION 3.0.2) 
project(xcf_minimal_readwrite) 

find_package(PkgConfig) 

IF(PKG_CONFIG_FOUND) 
    message(STATUS "found pkgconfig") 

    SET(MODULE "Memory") 

    IF(Memory_FIND_REQUIRED) 
    SET(Memory_REQUIRED "REQUIRED") 
    ENDIF() 
    PKG_CHECK_MODULES(Memory ${Memory_REQUIRED} ${MODULE}) 

    FIND_LIBRARY(Memory_LIBRARY 
    NAMES ${Memory_LIBRARIES} 
    HINTS ${Memory_LIBRARY_DIRS} 
) 
    SET(Memory_LIBRARIES ${Memory_LIBRARY}) 


    SET(MODULE "xmltio") 

    IF(xmltio_FIND_REQUIRED) 
    SET(xmltio_REQUIRED "REQUIRED") 
    ENDIF() 
    PKG_CHECK_MODULES(xmltio ${xmltio_REQUIRED} ${MODULE}) 

    FIND_LIBRARY(xmltio_LIBRARY 
    NAMES ${xmltio_LIBRARIES} 
    HINTS ${xmltio_LIBRARY_DIRS} 
) 
    SET(xmltio_LIBRARIES ${xmltio_LIBRARY}) 


ENDIF() 

IF(Memory_FOUND) 
    message(STATUS "found Memory") 
    include_directories(${Memory_INCLUDE_DIRS} ${xmltio_INCLUDE_DIRS}) 

    add_executable(${PROJECT_NAME} main.cc) 
    target_link_libraries(${PROJECT_NAME} ${Memory_LIBRARIES} ${xmltio_LIBRARIES}) 


    install(TARGETS ${PROJECT_NAME} 
    RUNTIME DESTINATION bin) 


ENDIF() 




# This is the part for Python SWIG: 

FIND_PACKAGE(SWIG REQUIRED) 
INCLUDE(${SWIG_USE_FILE}) 

FIND_PACKAGE(PythonLibs) 
INCLUDE_DIRECTORIES(${PYTHON_INCLUDE_PATH}) 

INCLUDE_DIRECTORIES(${CMAKE_CURRENT_SOURCE_DIR}) 

SET(CMAKE_SWIG_FLAGS "") 

SET_SOURCE_FILES_PROPERTIES(main.i PROPERTIES CPLUSPLUS ON) 
SET_SOURCE_FILES_PROPERTIES(main.i PROPERTIES SWIG_FLAGS "-includeall") 
SWIG_ADD_MODULE(main python main.i main.cc) 
SWIG_LINK_LIBRARIES(main ${Memory_LIBRARIES} ${xmltio_LIBRARIES} ${PYTHON_LIBRARIES}) 

をそして、これは私のmain.iがどのように見えるかです:

/* File : main.i */ 
%module main 
%{ 
/* Put headers and other declarations here */ 
extern int main(int argc, char *argv[]); 
%} 


extern int main(int argc, char *argv[]); 

ここで何が問題になりましたか?

「メイン」はこれに対して悪い名前かもしれませんか?

+0

メインはおそらく作成するのに最適な機能ではありませんが、あなたのモジュールはあなたのヘッダ(#%{/*...*/%} 'の中)に#includeを定義し、その後のヘッダー。 – AndyG

+0

ヘッダファイルはありません。 – Reyny

+0

SWIGはヘッダを解析するか、すべてのコードをインラインで書くことができます。これらの方法の1つを選択する必要があります。 – AndyG

答えて

0

swig呼び出しは、init_main関数を定義するソースファイルを生成します。このファイルはコンパイルされていないか、オブジェクトファイルがPython拡張機能を構成する共有オブジェクトにリンクされていません。

はい、メインは悪い名前ですが、ここではこれが問題になる可能性があるよりも早い段階で立ち往生しています。

関連する問題