例如,我在python中有一个函数,我想将其转换为c++ (或从c++调用,但我不想依赖python解释器)简单的python函数
//test.py
def my_sum(x,y):
print "Hello World!"
return x*x+y我运行了shedskin并且有
//test.cpp
#include "builtin.hpp"
#include "test.hpp"
namespace __test__ {
str *__name__;
void __init() {
__name__ = new str("__main__");
}
} // module namespace
int main(int, char **) {
__shedskin__::__init();
__shedskin__::__start(__test__::__init);
}
//test.hpp
#ifndef __TEST_HPP
#define __TEST_HPP
using namespace __shedskin__;
namespace __test__ {
extern str *__name__;
} // module namespace
#endif丑陋的代码,没有我的函数my_sum,代码依赖于"builtin.hpp“。是否可以只转换函数?
或
我想从我的c++代码中调用类似于int sum= py.my_sum(3,5)的函数;我该怎么做呢?
或
也许我可以从python代码中做DLL或Lib,我可以在c++代码中使用它们?
发布于 2012-05-02 04:22:00
请注意shedskin为该程序提供的警告:
*WARNING* test.py:1: function my_sum not called!文档中还提到,为了使编译起作用,应该(直接或间接)调用函数,否则无法进行类型推断。如果没有对my_sum的调用,如何确定它的参数类型??:-)
添加以下内容,例如:
if __name__ == '__main__':
my_sum(1,1)使my_sum出现在生成的C++代码中,该代码可能会被其他C++程序调用。
https://stackoverflow.com/questions/10347741
复制相似问题