Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/137.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
c++;库或dll DEF MyIySUM(x,y):打印“Hello World!”返回x*x+y如何调用C++的这个代码?类似代码C++ +代码> int和= pY.MySySand(x,y);代码>例如,谢谢!它可以工作,但如何访问类的方法呢codeclass class_cl:def myadd(a,b):print(“hell0 add”)返回a*a+b您的意思是第二个示例boost::python::object ret=main_module.attr(“class_cl”)().attr(“myadd”)(1,2)。它的工作顺序是:获取对类的引用,创建实例,获取myadd方法并用1,2调用它。也许你能给我一些简单的例子吗Helo.Py代码> DEF MyIySUM(x,y):打印“Hello World!”返回x*x+y如何调用C++的这个代码?类似代码C++ +代码> int和= pY.MySySand(x,y);代码>例如,谢谢!它可以工作,但如何访问类的方法呢codeclass class_cl:def myadd(a,b):print(“hell0 add”)返回a*a+b您的意思是第二个示例boost::python::object ret=main_module.attr(“class_cl”)().attr(“myadd”)(1,2)。它按顺序工作:获取对类的引用,创建实例,获取myadd方法并用1,2调用它。_C++_Python_Dll - Fatal编程技术网

c++;库或dll DEF MyIySUM(x,y):打印“Hello World!”返回x*x+y如何调用C++的这个代码?类似代码C++ +代码> int和= pY.MySySand(x,y);代码>例如,谢谢!它可以工作,但如何访问类的方法呢codeclass class_cl:def myadd(a,b):print(“hell0 add”)返回a*a+b您的意思是第二个示例boost::python::object ret=main_module.attr(“class_cl”)().attr(“myadd”)(1,2)。它的工作顺序是:获取对类的引用,创建实例,获取myadd方法并用1,2调用它。也许你能给我一些简单的例子吗Helo.Py代码> DEF MyIySUM(x,y):打印“Hello World!”返回x*x+y如何调用C++的这个代码?类似代码C++ +代码> int和= pY.MySySand(x,y);代码>例如,谢谢!它可以工作,但如何访问类的方法呢codeclass class_cl:def myadd(a,b):print(“hell0 add”)返回a*a+b您的意思是第二个示例boost::python::object ret=main_module.attr(“class_cl”)().attr(“myadd”)(1,2)。它按顺序工作:获取对类的引用,创建实例,获取myadd方法并用1,2调用它。

c++;库或dll DEF MyIySUM(x,y):打印“Hello World!”返回x*x+y如何调用C++的这个代码?类似代码C++ +代码> int和= pY.MySySand(x,y);代码>例如,谢谢!它可以工作,但如何访问类的方法呢codeclass class_cl:def myadd(a,b):print(“hell0 add”)返回a*a+b您的意思是第二个示例boost::python::object ret=main_module.attr(“class_cl”)().attr(“myadd”)(1,2)。它的工作顺序是:获取对类的引用,创建实例,获取myadd方法并用1,2调用它。也许你能给我一些简单的例子吗Helo.Py代码> DEF MyIySUM(x,y):打印“Hello World!”返回x*x+y如何调用C++的这个代码?类似代码C++ +代码> int和= pY.MySySand(x,y);代码>例如,谢谢!它可以工作,但如何访问类的方法呢codeclass class_cl:def myadd(a,b):print(“hell0 add”)返回a*a+b您的意思是第二个示例boost::python::object ret=main_module.attr(“class_cl”)().attr(“myadd”)(1,2)。它按顺序工作:获取对类的引用,创建实例,获取myadd方法并用1,2调用它。,c++,python,dll,C++,Python,Dll,也许您需要检查如何将python嵌入另一个应用程序() 您可能需要检查如何将python嵌入另一个应用程序() 你可以用cython和write Cython libhelloWorld.pyx: import sys sys.path.append(".") #yourlib is in current folder import yourlib #you can import any python module cdef public void helloWorld(): prin

也许您需要检查如何将python嵌入另一个应用程序()

您可能需要检查如何将python嵌入另一个应用程序()

你可以用cython和write Cython lib
helloWorld.pyx

import sys
sys.path.append(".") #yourlib is in current folder
import yourlib #you can import any python module

cdef public void helloWorld():
    print "Hello World Cython!"
    yourlib.helloWorld("Python")

cdef public int my_sum(int x, int y):
    return x*x+y

from libcpp.string cimport string

cdef public string testString( string sx, string sy ):
    x = int(sx.c_str())
    y = int(sy.c_str())
    ret= "%d*%d+%d=%d"%(x,x,y,my_sum(x,y))
    cdef char* ret2= ret
    return string( ret2 )
使用cython编译(创建
helloWorld.cpp
helloWorld.h
):

您的代码
程序.cpp

#include <string>
#include <iostream>
#include "Python.h"
#include "helloWorld.h" // it's cpp header so remove __PYX_EXTERN_C (bug)

int main(int argc, char *argv[]) {
    Py_Initialize(); //start python interpreter
    inithelloWorld(); //run module helloWorld
    helloWorld();
    std::cout << testString("6","6") << std::endl; #it's fast!
    Py_Finalize();
    return 0;
}
#include <string>
#include <iostream>
#include <boost/python.hpp>

int main(int argc, char *argv[]) {
    Py_Initialize();
    boost::python::object sys = boost::python::import("sys");
    sys.attr("path").attr("append")(".");
    boost::python::object main_module = boost::python::import("yourlib");
    main_module.attr("helloWorld")("boost_python");
    boost::python::object ret= main_module.attr( "my_sum" )( 10, 10 );
    std::cout << boost::python::extract<char const*>(ret) << std::endl;
    Py_Finalize();
    return 0;
}
另一种方法是使用 您的代码
程序.cpp

#include <string>
#include <iostream>
#include "Python.h"
#include "helloWorld.h" // it's cpp header so remove __PYX_EXTERN_C (bug)

int main(int argc, char *argv[]) {
    Py_Initialize(); //start python interpreter
    inithelloWorld(); //run module helloWorld
    helloWorld();
    std::cout << testString("6","6") << std::endl; #it's fast!
    Py_Finalize();
    return 0;
}
#include <string>
#include <iostream>
#include <boost/python.hpp>

int main(int argc, char *argv[]) {
    Py_Initialize();
    boost::python::object sys = boost::python::import("sys");
    sys.attr("path").attr("append")(".");
    boost::python::object main_module = boost::python::import("yourlib");
    main_module.attr("helloWorld")("boost_python");
    boost::python::object ret= main_module.attr( "my_sum" )( 10, 10 );
    std::cout << boost::python::extract<char const*>(ret) << std::endl;
    Py_Finalize();
    return 0;
}
你可以用cython和write Cython lib
helloWorld.pyx

import sys
sys.path.append(".") #yourlib is in current folder
import yourlib #you can import any python module

cdef public void helloWorld():
    print "Hello World Cython!"
    yourlib.helloWorld("Python")

cdef public int my_sum(int x, int y):
    return x*x+y

from libcpp.string cimport string

cdef public string testString( string sx, string sy ):
    x = int(sx.c_str())
    y = int(sy.c_str())
    ret= "%d*%d+%d=%d"%(x,x,y,my_sum(x,y))
    cdef char* ret2= ret
    return string( ret2 )
使用cython编译(创建
helloWorld.cpp
helloWorld.h
):

您的代码
程序.cpp

#include <string>
#include <iostream>
#include "Python.h"
#include "helloWorld.h" // it's cpp header so remove __PYX_EXTERN_C (bug)

int main(int argc, char *argv[]) {
    Py_Initialize(); //start python interpreter
    inithelloWorld(); //run module helloWorld
    helloWorld();
    std::cout << testString("6","6") << std::endl; #it's fast!
    Py_Finalize();
    return 0;
}
#include <string>
#include <iostream>
#include <boost/python.hpp>

int main(int argc, char *argv[]) {
    Py_Initialize();
    boost::python::object sys = boost::python::import("sys");
    sys.attr("path").attr("append")(".");
    boost::python::object main_module = boost::python::import("yourlib");
    main_module.attr("helloWorld")("boost_python");
    boost::python::object ret= main_module.attr( "my_sum" )( 10, 10 );
    std::cout << boost::python::extract<char const*>(ret) << std::endl;
    Py_Finalize();
    return 0;
}
另一种方法是使用 您的代码
程序.cpp

#include <string>
#include <iostream>
#include "Python.h"
#include "helloWorld.h" // it's cpp header so remove __PYX_EXTERN_C (bug)

int main(int argc, char *argv[]) {
    Py_Initialize(); //start python interpreter
    inithelloWorld(); //run module helloWorld
    helloWorld();
    std::cout << testString("6","6") << std::endl; #it's fast!
    Py_Finalize();
    return 0;
}
#include <string>
#include <iostream>
#include <boost/python.hpp>

int main(int argc, char *argv[]) {
    Py_Initialize();
    boost::python::object sys = boost::python::import("sys");
    sys.attr("path").attr("append")(".");
    boost::python::object main_module = boost::python::import("yourlib");
    main_module.attr("helloWorld")("boost_python");
    boost::python::object ret= main_module.attr( "my_sum" )( 10, 10 );
    std::cout << boost::python::extract<char const*>(ret) << std::endl;
    Py_Finalize();
    return 0;
}

如果我使用这种方法,并且我想在另一台计算机上运行我的程序,我必须安装python解释器?@mrglud是的,但这绝对适用于任何类型的cpython代码。从未尝试过(对于嵌入式代码),但我认为您可以使用freeze或类似的工具,只将必要的文件打包到应用程序中,我看不出有任何理由不这样做。你也必须为C程序包含正确的LIBS,这听起来很复杂。我只想把Python代码放到LIB或DLL中(只建一次,以后不使用解释程序),并且可以从C++代码(主程序)访问它。也许网络上有一些例子?@mrglud:没有,如果你将Python嵌入到你的应用程序中,你不需要在目标机器上安装Python。您只需要将Python DLL和所有需要的Python模块(这些模块可以放在一个.zip文件中)与应用程序一起分发,我通过在我的项目中放置pithon27.DLL和Lib with.py文件设法做到了这一点,我知道我可以用类似installshield的东西来分发它们,但是有没有办法在一个.exe文件中构建它?如果我使用这种方法,并且我想在另一台计算机上运行我的程序,我必须安装python解释器?@mrglud是的,但是对于任何类型的cpython代码来说都是这样。从未尝试过(对于嵌入式代码),但我认为您可以使用freeze或类似的工具,只将必要的文件打包到应用程序中,我看不出有任何理由不这样做。你也必须为C程序包含正确的LIBS,这听起来很复杂。我只想把Python代码放到LIB或DLL中(只建一次,以后不使用解释程序),并且可以从C++代码(主程序)访问它。也许网络上有一些例子?@mrglud:没有,如果你将Python嵌入到你的应用程序中,你不需要在目标机器上安装Python。您只需要将Python DLL和所有需要的Python模块(这些模块可以放在一个.zip文件中)与应用程序一起分发,我通过在我的项目中放置pithon27.DLL和Lib with.py文件设法做到了这一点,我知道我可以用类似installshield的东西来分发它们,但是有没有一种方法可以在一个.exe文件中构建它?也许你可以给我一些简单的例子Helo.Py代码> DEF MyIySUM(x,y):打印“Hello World!”返回x*x+y<代码>如何调用C++的这个代码?类似代码C++ +代码> int和= pY.MySySand(x,y);代码>例如,谢谢!它可以工作,但如何访问类的方法呢
code
class class_cl:def myadd(a,b):print(“hell0 add”)返回a*a+b您的意思是第二个示例<代码>boost::python::object ret=main_module.attr(“class_cl”)().attr(“myadd”)(1,2)。它的工作顺序是:获取对类的引用,创建实例,获取
myadd
方法并用1,2调用它。也许你能给我一些简单的例子吗Helo.Py代码> DEF MyIySUM(x,y):打印“Hello World!”返回x*x+y<代码>如何调用C++的这个代码?类似代码C++ +代码> int和= pY.MySySand(x,y);代码>例如,谢谢!它可以工作,但如何访问类的方法呢
code
class class_cl:def myadd(a,b):print(“hell0 add”)返回a*a+b您的意思是第二个示例<代码>boost::python::object ret=main_module.attr(“class_cl”)().attr(“myadd”)(1,2)。它按顺序工作:获取对类的引用,创建实例,获取
myadd
方法并用1,2调用它。