Commit e212b973 by songxinkai

python::boost, prue virtual class

parent 410ed6c4
cmake_minimum_required(VERSION 3.14)
project(hello)
# headers
set(PYTHON_INCLUDE_DIRS /home/songxinkai/anaconda3/include/python3.7m)
set(PYTHON_LIBRARIES /home/songxinkai/anaconda3/lib/libpython3.7m.so)
# libraries
set(Boost_INCLUDE_DIRS /home/songxinkai/Boost_1.72.0/include)
set(Boost_LIBRARIES /home/songxinkai/Boost_1.72.0/lib/libboost_python37.so)
# cpp codes
file(GLOB_RECURSE python_srcs ${PROJECT_SOURCE_DIR}/*.cpp)
set(class_example_src class_example.cpp)
set(function_example_src function_example.cpp)
set(pure_virtual_inherit_example_src pure_virtual_inherit_example.cpp)
# compile
add_library(pyhello SHARED ${python_srcs})
set_target_properties(pyhello PROPERTIES PREFIX "" OUTPUT_NAME "_hello")
add_library(_function_example SHARED ${function_example_src})
add_library(_class_example SHARED ${class_example_src})
add_library(_pure_virtual_inherit_example SHARED ${pure_virtual_inherit_example_src})
set_target_properties(_function_example PROPERTIES PREFIX "" OUTPUT_NAME "_function_example")
set_target_properties(_class_example PROPERTIES PREFIX "" OUTPUT_NAME "_class_example")
set_target_properties(_pure_virtual_inherit_example PROPERTIES PREFIX "" OUTPUT_NAME "_pure_virtual_inherit_example")
include_directories(${PYTHON_INCLUDE_DIRS} ${Boost_INCLUDE_DIRS})
target_link_libraries(pyhello ${PYTHON_LIBRARIES} ${Boost_LIBRARIES})
target_link_libraries(_class_example ${PYTHON_LIBRARIES} ${Boost_LIBRARIES})
target_link_libraries(_function_example ${PYTHON_LIBRARIES} ${Boost_LIBRARIES})
target_link_libraries(_pure_virtual_inherit_example ${PYTHON_LIBRARIES} ${Boost_LIBRARIES})
......@@ -14,9 +14,6 @@
# EXTERNAL cache entries
########################
//No help, variable specified on the command line.
Boost_NO_BOOST_CMAKE:BOOL=ON
//Path to a program.
CMAKE_AR:FILEPATH=/usr/bin/ar
......@@ -203,9 +200,6 @@ CMAKE_STRIP:FILEPATH=/usr/bin/strip
// Studio IDE projects all commands are done without /nologo.
CMAKE_VERBOSE_MAKEFILE:BOOL=FALSE
//No help, variable specified on the command line.
PYTHON_EXECUTABLE:UNINITIALIZED=/home/songxinkai/anaconda3/bin/python3
//Value Computed by CMake
hello_BINARY_DIR:STATIC=/home/songxinkai/mytests/boost/python/build
......
......@@ -118,5 +118,7 @@ set(CMAKE_MAKEFILE_PRODUCTS
# Dependency information for all targets:
set(CMAKE_DEPEND_INFO_FILES
"CMakeFiles/pyhello.dir/DependInfo.cmake"
"CMakeFiles/_pure_virtual_inherit_example.dir/DependInfo.cmake"
"CMakeFiles/_class_example.dir/DependInfo.cmake"
"CMakeFiles/_function_example.dir/DependInfo.cmake"
)
......@@ -65,39 +65,113 @@ CMAKE_SOURCE_DIR = /home/songxinkai/mytests/boost/python
CMAKE_BINARY_DIR = /home/songxinkai/mytests/boost/python/build
#=============================================================================
# Target rules for target CMakeFiles/pyhello.dir
# Target rules for target CMakeFiles/_pure_virtual_inherit_example.dir
# All Build rule for target.
CMakeFiles/pyhello.dir/all:
$(MAKE) -f CMakeFiles/pyhello.dir/build.make CMakeFiles/pyhello.dir/depend
$(MAKE) -f CMakeFiles/pyhello.dir/build.make CMakeFiles/pyhello.dir/build
@$(CMAKE_COMMAND) -E cmake_echo_color --switch=$(COLOR) --progress-dir=/home/songxinkai/mytests/boost/python/build/CMakeFiles --progress-num=1,2,3 "Built target pyhello"
.PHONY : CMakeFiles/pyhello.dir/all
CMakeFiles/_pure_virtual_inherit_example.dir/all:
$(MAKE) -f CMakeFiles/_pure_virtual_inherit_example.dir/build.make CMakeFiles/_pure_virtual_inherit_example.dir/depend
$(MAKE) -f CMakeFiles/_pure_virtual_inherit_example.dir/build.make CMakeFiles/_pure_virtual_inherit_example.dir/build
@$(CMAKE_COMMAND) -E cmake_echo_color --switch=$(COLOR) --progress-dir=/home/songxinkai/mytests/boost/python/build/CMakeFiles --progress-num=5,6 "Built target _pure_virtual_inherit_example"
.PHONY : CMakeFiles/_pure_virtual_inherit_example.dir/all
# Include target in all.
all: CMakeFiles/pyhello.dir/all
all: CMakeFiles/_pure_virtual_inherit_example.dir/all
.PHONY : all
# Build rule for subdir invocation for target.
CMakeFiles/pyhello.dir/rule: cmake_check_build_system
$(CMAKE_COMMAND) -E cmake_progress_start /home/songxinkai/mytests/boost/python/build/CMakeFiles 3
$(MAKE) -f CMakeFiles/Makefile2 CMakeFiles/pyhello.dir/all
CMakeFiles/_pure_virtual_inherit_example.dir/rule: cmake_check_build_system
$(CMAKE_COMMAND) -E cmake_progress_start /home/songxinkai/mytests/boost/python/build/CMakeFiles 2
$(MAKE) -f CMakeFiles/Makefile2 CMakeFiles/_pure_virtual_inherit_example.dir/all
$(CMAKE_COMMAND) -E cmake_progress_start /home/songxinkai/mytests/boost/python/build/CMakeFiles 0
.PHONY : CMakeFiles/pyhello.dir/rule
.PHONY : CMakeFiles/_pure_virtual_inherit_example.dir/rule
# Convenience name for target.
pyhello: CMakeFiles/pyhello.dir/rule
_pure_virtual_inherit_example: CMakeFiles/_pure_virtual_inherit_example.dir/rule
.PHONY : pyhello
.PHONY : _pure_virtual_inherit_example
# clean rule for target.
CMakeFiles/pyhello.dir/clean:
$(MAKE) -f CMakeFiles/pyhello.dir/build.make CMakeFiles/pyhello.dir/clean
.PHONY : CMakeFiles/pyhello.dir/clean
CMakeFiles/_pure_virtual_inherit_example.dir/clean:
$(MAKE) -f CMakeFiles/_pure_virtual_inherit_example.dir/build.make CMakeFiles/_pure_virtual_inherit_example.dir/clean
.PHONY : CMakeFiles/_pure_virtual_inherit_example.dir/clean
# clean rule for target.
clean: CMakeFiles/pyhello.dir/clean
clean: CMakeFiles/_pure_virtual_inherit_example.dir/clean
.PHONY : clean
#=============================================================================
# Target rules for target CMakeFiles/_class_example.dir
# All Build rule for target.
CMakeFiles/_class_example.dir/all:
$(MAKE) -f CMakeFiles/_class_example.dir/build.make CMakeFiles/_class_example.dir/depend
$(MAKE) -f CMakeFiles/_class_example.dir/build.make CMakeFiles/_class_example.dir/build
@$(CMAKE_COMMAND) -E cmake_echo_color --switch=$(COLOR) --progress-dir=/home/songxinkai/mytests/boost/python/build/CMakeFiles --progress-num=1,2 "Built target _class_example"
.PHONY : CMakeFiles/_class_example.dir/all
# Include target in all.
all: CMakeFiles/_class_example.dir/all
.PHONY : all
# Build rule for subdir invocation for target.
CMakeFiles/_class_example.dir/rule: cmake_check_build_system
$(CMAKE_COMMAND) -E cmake_progress_start /home/songxinkai/mytests/boost/python/build/CMakeFiles 2
$(MAKE) -f CMakeFiles/Makefile2 CMakeFiles/_class_example.dir/all
$(CMAKE_COMMAND) -E cmake_progress_start /home/songxinkai/mytests/boost/python/build/CMakeFiles 0
.PHONY : CMakeFiles/_class_example.dir/rule
# Convenience name for target.
_class_example: CMakeFiles/_class_example.dir/rule
.PHONY : _class_example
# clean rule for target.
CMakeFiles/_class_example.dir/clean:
$(MAKE) -f CMakeFiles/_class_example.dir/build.make CMakeFiles/_class_example.dir/clean
.PHONY : CMakeFiles/_class_example.dir/clean
# clean rule for target.
clean: CMakeFiles/_class_example.dir/clean
.PHONY : clean
#=============================================================================
# Target rules for target CMakeFiles/_function_example.dir
# All Build rule for target.
CMakeFiles/_function_example.dir/all:
$(MAKE) -f CMakeFiles/_function_example.dir/build.make CMakeFiles/_function_example.dir/depend
$(MAKE) -f CMakeFiles/_function_example.dir/build.make CMakeFiles/_function_example.dir/build
@$(CMAKE_COMMAND) -E cmake_echo_color --switch=$(COLOR) --progress-dir=/home/songxinkai/mytests/boost/python/build/CMakeFiles --progress-num=3,4 "Built target _function_example"
.PHONY : CMakeFiles/_function_example.dir/all
# Include target in all.
all: CMakeFiles/_function_example.dir/all
.PHONY : all
# Build rule for subdir invocation for target.
CMakeFiles/_function_example.dir/rule: cmake_check_build_system
$(CMAKE_COMMAND) -E cmake_progress_start /home/songxinkai/mytests/boost/python/build/CMakeFiles 2
$(MAKE) -f CMakeFiles/Makefile2 CMakeFiles/_function_example.dir/all
$(CMAKE_COMMAND) -E cmake_progress_start /home/songxinkai/mytests/boost/python/build/CMakeFiles 0
.PHONY : CMakeFiles/_function_example.dir/rule
# Convenience name for target.
_function_example: CMakeFiles/_function_example.dir/rule
.PHONY : _function_example
# clean rule for target.
CMakeFiles/_function_example.dir/clean:
$(MAKE) -f CMakeFiles/_function_example.dir/build.make CMakeFiles/_function_example.dir/clean
.PHONY : CMakeFiles/_function_example.dir/clean
# clean rule for target.
clean: CMakeFiles/_function_example.dir/clean
.PHONY : clean
......
/home/songxinkai/mytests/boost/python/build/CMakeFiles/rebuild_cache.dir
/home/songxinkai/mytests/boost/python/build/CMakeFiles/edit_cache.dir
/home/songxinkai/mytests/boost/python/build/CMakeFiles/pyhello.dir
/home/songxinkai/mytests/boost/python/build/CMakeFiles/_pure_virtual_inherit_example.dir
/home/songxinkai/mytests/boost/python/build/CMakeFiles/_class_example.dir
/home/songxinkai/mytests/boost/python/build/CMakeFiles/_function_example.dir
......@@ -111,71 +111,124 @@ depend:
.PHONY : depend
#=============================================================================
# Target rules for targets named pyhello
# Target rules for targets named _pure_virtual_inherit_example
# Build rule for target.
pyhello: cmake_check_build_system
$(MAKE) -f CMakeFiles/Makefile2 pyhello
.PHONY : pyhello
_pure_virtual_inherit_example: cmake_check_build_system
$(MAKE) -f CMakeFiles/Makefile2 _pure_virtual_inherit_example
.PHONY : _pure_virtual_inherit_example
# fast build rule for target.
pyhello/fast:
$(MAKE) -f CMakeFiles/pyhello.dir/build.make CMakeFiles/pyhello.dir/build
.PHONY : pyhello/fast
_pure_virtual_inherit_example/fast:
$(MAKE) -f CMakeFiles/_pure_virtual_inherit_example.dir/build.make CMakeFiles/_pure_virtual_inherit_example.dir/build
.PHONY : _pure_virtual_inherit_example/fast
CMakeFiles/3.14.6/CompilerIdCXX/CMakeCXXCompilerId.o: CMakeFiles/3.14.6/CompilerIdCXX/CMakeCXXCompilerId.cpp.o
#=============================================================================
# Target rules for targets named _class_example
# Build rule for target.
_class_example: cmake_check_build_system
$(MAKE) -f CMakeFiles/Makefile2 _class_example
.PHONY : _class_example
# fast build rule for target.
_class_example/fast:
$(MAKE) -f CMakeFiles/_class_example.dir/build.make CMakeFiles/_class_example.dir/build
.PHONY : _class_example/fast
#=============================================================================
# Target rules for targets named _function_example
# Build rule for target.
_function_example: cmake_check_build_system
$(MAKE) -f CMakeFiles/Makefile2 _function_example
.PHONY : _function_example
# fast build rule for target.
_function_example/fast:
$(MAKE) -f CMakeFiles/_function_example.dir/build.make CMakeFiles/_function_example.dir/build
.PHONY : _function_example/fast
class_example.o: class_example.cpp.o
.PHONY : class_example.o
# target to build an object file
class_example.cpp.o:
$(MAKE) -f CMakeFiles/_class_example.dir/build.make CMakeFiles/_class_example.dir/class_example.cpp.o
.PHONY : class_example.cpp.o
class_example.i: class_example.cpp.i
.PHONY : class_example.i
# target to preprocess a source file
class_example.cpp.i:
$(MAKE) -f CMakeFiles/_class_example.dir/build.make CMakeFiles/_class_example.dir/class_example.cpp.i
.PHONY : class_example.cpp.i
class_example.s: class_example.cpp.s
.PHONY : class_example.s
# target to generate assembly for a file
class_example.cpp.s:
$(MAKE) -f CMakeFiles/_class_example.dir/build.make CMakeFiles/_class_example.dir/class_example.cpp.s
.PHONY : class_example.cpp.s
function_example.o: function_example.cpp.o
.PHONY : CMakeFiles/3.14.6/CompilerIdCXX/CMakeCXXCompilerId.o
.PHONY : function_example.o
# target to build an object file
CMakeFiles/3.14.6/CompilerIdCXX/CMakeCXXCompilerId.cpp.o:
$(MAKE) -f CMakeFiles/pyhello.dir/build.make CMakeFiles/pyhello.dir/CMakeFiles/3.14.6/CompilerIdCXX/CMakeCXXCompilerId.cpp.o
.PHONY : CMakeFiles/3.14.6/CompilerIdCXX/CMakeCXXCompilerId.cpp.o
function_example.cpp.o:
$(MAKE) -f CMakeFiles/_function_example.dir/build.make CMakeFiles/_function_example.dir/function_example.cpp.o
.PHONY : function_example.cpp.o
CMakeFiles/3.14.6/CompilerIdCXX/CMakeCXXCompilerId.i: CMakeFiles/3.14.6/CompilerIdCXX/CMakeCXXCompilerId.cpp.i
function_example.i: function_example.cpp.i
.PHONY : CMakeFiles/3.14.6/CompilerIdCXX/CMakeCXXCompilerId.i
.PHONY : function_example.i
# target to preprocess a source file
CMakeFiles/3.14.6/CompilerIdCXX/CMakeCXXCompilerId.cpp.i:
$(MAKE) -f CMakeFiles/pyhello.dir/build.make CMakeFiles/pyhello.dir/CMakeFiles/3.14.6/CompilerIdCXX/CMakeCXXCompilerId.cpp.i
.PHONY : CMakeFiles/3.14.6/CompilerIdCXX/CMakeCXXCompilerId.cpp.i
function_example.cpp.i:
$(MAKE) -f CMakeFiles/_function_example.dir/build.make CMakeFiles/_function_example.dir/function_example.cpp.i
.PHONY : function_example.cpp.i
CMakeFiles/3.14.6/CompilerIdCXX/CMakeCXXCompilerId.s: CMakeFiles/3.14.6/CompilerIdCXX/CMakeCXXCompilerId.cpp.s
function_example.s: function_example.cpp.s
.PHONY : CMakeFiles/3.14.6/CompilerIdCXX/CMakeCXXCompilerId.s
.PHONY : function_example.s
# target to generate assembly for a file
CMakeFiles/3.14.6/CompilerIdCXX/CMakeCXXCompilerId.cpp.s:
$(MAKE) -f CMakeFiles/pyhello.dir/build.make CMakeFiles/pyhello.dir/CMakeFiles/3.14.6/CompilerIdCXX/CMakeCXXCompilerId.cpp.s
.PHONY : CMakeFiles/3.14.6/CompilerIdCXX/CMakeCXXCompilerId.cpp.s
function_example.cpp.s:
$(MAKE) -f CMakeFiles/_function_example.dir/build.make CMakeFiles/_function_example.dir/function_example.cpp.s
.PHONY : function_example.cpp.s
hello.o: hello.cpp.o
pure_virtual_inherit_example.o: pure_virtual_inherit_example.cpp.o
.PHONY : hello.o
.PHONY : pure_virtual_inherit_example.o
# target to build an object file
hello.cpp.o:
$(MAKE) -f CMakeFiles/pyhello.dir/build.make CMakeFiles/pyhello.dir/hello.cpp.o
.PHONY : hello.cpp.o
pure_virtual_inherit_example.cpp.o:
$(MAKE) -f CMakeFiles/_pure_virtual_inherit_example.dir/build.make CMakeFiles/_pure_virtual_inherit_example.dir/pure_virtual_inherit_example.cpp.o
.PHONY : pure_virtual_inherit_example.cpp.o
hello.i: hello.cpp.i
pure_virtual_inherit_example.i: pure_virtual_inherit_example.cpp.i
.PHONY : hello.i
.PHONY : pure_virtual_inherit_example.i
# target to preprocess a source file
hello.cpp.i:
$(MAKE) -f CMakeFiles/pyhello.dir/build.make CMakeFiles/pyhello.dir/hello.cpp.i
.PHONY : hello.cpp.i
pure_virtual_inherit_example.cpp.i:
$(MAKE) -f CMakeFiles/_pure_virtual_inherit_example.dir/build.make CMakeFiles/_pure_virtual_inherit_example.dir/pure_virtual_inherit_example.cpp.i
.PHONY : pure_virtual_inherit_example.cpp.i
hello.s: hello.cpp.s
pure_virtual_inherit_example.s: pure_virtual_inherit_example.cpp.s
.PHONY : hello.s
.PHONY : pure_virtual_inherit_example.s
# target to generate assembly for a file
hello.cpp.s:
$(MAKE) -f CMakeFiles/pyhello.dir/build.make CMakeFiles/pyhello.dir/hello.cpp.s
.PHONY : hello.cpp.s
pure_virtual_inherit_example.cpp.s:
$(MAKE) -f CMakeFiles/_pure_virtual_inherit_example.dir/build.make CMakeFiles/_pure_virtual_inherit_example.dir/pure_virtual_inherit_example.cpp.s
.PHONY : pure_virtual_inherit_example.cpp.s
# Help Target
help:
......@@ -185,13 +238,18 @@ help:
@echo "... depend"
@echo "... rebuild_cache"
@echo "... edit_cache"
@echo "... pyhello"
@echo "... CMakeFiles/3.14.6/CompilerIdCXX/CMakeCXXCompilerId.o"
@echo "... CMakeFiles/3.14.6/CompilerIdCXX/CMakeCXXCompilerId.i"
@echo "... CMakeFiles/3.14.6/CompilerIdCXX/CMakeCXXCompilerId.s"
@echo "... hello.o"
@echo "... hello.i"
@echo "... hello.s"
@echo "... _pure_virtual_inherit_example"
@echo "... _class_example"
@echo "... _function_example"
@echo "... class_example.o"
@echo "... class_example.i"
@echo "... class_example.s"
@echo "... function_example.o"
@echo "... function_example.i"
@echo "... function_example.s"
@echo "... pure_virtual_inherit_example.o"
@echo "... pure_virtual_inherit_example.i"
@echo "... pure_virtual_inherit_example.s"
.PHONY : help
......
#include <boost/python.hpp>
#include <iostream>
using namespace boost::python;
using namespace std;
class Base {
public:
Base() : m_first(0) {
cout << "Base constructor 0, m_first: "
<< m_first << endl;
}
Base(int first) : m_first(first) {
cout << "Base constructor 1, m_first: "
<< m_first << endl;
}
~Base(){}
virtual void print(){
cout << "This is Base." << endl;
}
int get_first() const{return m_first;}
void set_first(int num){m_first = num;}
protected:
int m_first;
};
class Derived : public Base{
public:
Derived() : Base(), m_second(0) {}
Derived(int second) : Base(), m_second(second) {}
Derived(int first, int second) : Base(first), m_second(second) {}
~Derived(){}
void print(){
cout << "This is Derived (" << m_first << ", " << m_second << ")." << endl;
}
int get_second() const {return m_second;}
void set_second(int num) {m_second = num;}
protected:
int m_second;
};
class PrueVirtualClass {
public:
PrueVirtualClass(){}
virtual ~PrueVirtualClass () = default;
virtual int f() = 0;
};
struct PrueVirtualClassWrap : PrueVirtualClass, wrapper<PrueVirtualClass> {
PrueVirtualClassWrap(): PrueVirtualClass(){}
int f() {
return this->get_override("f")();
}
};
BOOST_PYTHON_MODULE(_class_example) {
class_<Base>("Base")
.def(init<int>())
.def("print", &Base::print)
.add_property("m_first", &Base::get_first) //, &Base::set_first)
;
// class_<Derived, bases<Base> >("Derived")
class_<Derived >("Derived")
.def(init<int>())
.def(init<int, int>())
.def("print", &Derived::print)
.add_property("m_second", &Derived::get_second, &Derived::set_second)
;
class_<PrueVirtualClassWrap, boost::noncopyable>("prueVirtualClass", init<>())
.def("f", pure_virtual(&PrueVirtualClass::f))
;
}
#include <boost/python.hpp>
using namespace boost::python;
const char* Greet() {
return "hello, world";
}
int Add(int i, int j) {
return i+j;
}
BOOST_PYTHON_MODULE(_function_example) {
def("greet", Greet);
def("add", &Add, "A function which adds two numbers");
}
#include <boost/python.hpp>
#include <iostream>
using namespace boost::python;
using std::cout;
using std::endl;
// Mockup abstract type.
class polygon {
public:
polygon() {}
virtual int area () = 0;
void print (){
cout << "in polygon::print()" << endl;
}
virtual ~polygon() = default;
};
// Wrapper to allow calling Python overrides.
struct polygon_wrap : polygon, wrapper<polygon> {
polygon_wrap(): polygon() {}
int area() { return this->get_override("area")(); }
};
BOOST_PYTHON_MODULE(_pure_virtual_inherit_example) {
// Expose models.
class_<polygon_wrap, boost::noncopyable>("Polygon", init<>())
.def("area", pure_virtual(&polygon::area))
.def("print", &polygon::print)
;
}
#!/home/songxinkai/anaconda3/bin/python
import sys
sys.path.insert(-1, "build")
import _pure_virtual_inherit_example as pv
class Square (pv.Polygon):
def area(self):
print("in sq::area()")
if __name__ == "__main__":
sq = Square()
sq.area()
sq.print()
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment