我为c ++代码提供了一个CMAKE文件,并希望将其函数包装到python中。每当我尝试导入该函数时,它都会给我一个导入错误:
动态模块没有定义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;
}
这是我添加了最后一个SWIG部分的CMAKE文件:
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[]);
有任何疑问,这里出了什么问题?
&#34; main&#34;对此可能是一个坏名字?
答案 0 :(得分:0)
swig调用生成一个定义init_main函数的源文件。此文件未编译,或其目标文件未链接到构成python扩展的共享对象。
是的,主要是一个坏名字,但在这里你被困在一个比这可能成为问题的早期阶段。