[英]Python code in C++
I want to use some Python code in my C++ framework to plot some statistics. 我想在我的C ++框架中使用一些Python代码来绘制一些统计信息。 I have already found the following posting (about how to embedd python in c++) but following the instructions did not lead to success: Embed python code in C++ (Windows + minGW + Python 2.7.2 + Eclipse)
我已经找到了以下帖子(关于如何在c ++中嵌入python ),但是遵循说明并没有带来成功: 在C ++中嵌入python代码(Windows + minGW + Python 2.7.2 + Eclipse)
#include "Python.h"
int main(int f_argc, const char* f_argv [])
{
Py_Initialize();
const char* pythonScript = "print 'Hello, world!'\n";
int result = PyRun_SimpleString(pythonScript);
Py_Finalize();
return 0;
}
I am sorry, but I do not have much experience with make files or attaching static or dynamic libraries.... 对不起,我对make文件或附加静态或动态库没有多少经验....
I have to following system: Windows 7 + 64 Bit + Eclipse IDE for C/C++ Developers, Version: Juno Service Release 1 + mingw + python32 我必须遵循以下系统:适用于C / C ++开发人员的Windows 7 + 64 Bit + Eclipse IDE,版本:Juno Service Release 1 + mingw + python32
under path and symbols: + added include directory of python32 + added library "python32" which should correspond to libpython32.a + added library path 在路径和符号下:+添加了包含python32目录+添加了库“python32”,它应该对应于libpython32.a +添加的库路径
The compiling and linking seems to work, but when I try to start the exe, I get the following message: 编译和链接似乎工作,但当我尝试启动exe时,我收到以下消息:
"The program can't start because python32.dll is missing from your computer. Try reinstalling the program to fix this problem." “程序无法启动,因为您的计算机缺少python32.dll。请尝试重新安装该程序以解决此问题。”
I cannot understand this message because I try to add a static library (libpython32.a) to the source. 我无法理解这条消息,因为我尝试将静态库(libpython32.a)添加到源代码中。 Could you give me a gentle push in the right direction?
你能不能给我一个正确的方向?
Thank you very much for your help! 非常感谢您的帮助!
EDIT: added makefile and objects.mk 编辑:添加了makefile和objects.mk
MAKEFILE ################################################################################ # Automatically-generated file. MAKEFILE ################################################## ################################自动生成的文件。 Do not edit!
不要编辑! ################################################################################
################################################## ##############################
-include ../makefile.init
RM := rm -rf
# All of the sources participating in the build are defined here
-include sources.mk
-include src/subdir.mk
-include subdir.mk
-include objects.mk
ifneq ($(MAKECMDGOALS),clean)
ifneq ($(strip $(C++_DEPS)),)
-include $(C++_DEPS)
endif
ifneq ($(strip $(C_DEPS)),)
-include $(C_DEPS)
endif
ifneq ($(strip $(CC_DEPS)),)
-include $(CC_DEPS)
endif
ifneq ($(strip $(CPP_DEPS)),)
-include $(CPP_DEPS)
endif
ifneq ($(strip $(CXX_DEPS)),)
-include $(CXX_DEPS)
endif
ifneq ($(strip $(C_UPPER_DEPS)),)
-include $(C_UPPER_DEPS)
endif
endif
-include ../makefile.defs
# Add inputs and outputs from these tool invocations to the build variables
# All Target
all: Sandbox.exe
# Tool invocations
Sandbox.exe: $(OBJS) $(USER_OBJS)
@echo 'Building target: $@'
@echo 'Invoking: Cross G++ Linker'
g++ -L"C:\Python32\libs" -o "Sandbox.exe" $(OBJS) $(USER_OBJS) $(LIBS)
@echo 'Finished building target: $@'
@echo ' '
# Other Targets
clean:
-$(RM) $(C++_DEPS)$(OBJS)$(C_DEPS)$(CC_DEPS)$(CPP_DEPS)$(EXECUTABLES)$(CXX_DEPS)$(C_UPPER_DEPS) Sandbox.exe
-@echo ' '
.PHONY: all clean dependents
.SECONDARY:
-include ../makefile.targets
OBJECTS.MK OBJECTS.MK
################################################################################
# Automatically-generated file. Do not edit!
################################################################################
USER_OBJS :=
LIBS := -lgdi32 -ljpeg-8 -ltiff-5 -lpython32
On Windows the program search path and the shared library search path are controled by the same environment variable, PATH
. 在Windows上,程序搜索路径和共享库搜索路径由相同的环境变量
PATH
。 To embed Python, you need to put the directory that contains python32.dll
, typically c:\\python3.2
, on your PATH
. 要嵌入Python,您需要在
PATH
上放置包含python32.dll
的目录,通常是c:\\python3.2
。
Explanations how to change PATH
on Windows are easily googled; 解释如何在Windows上更改
PATH
很容易用谷歌搜索; see for example this videocast that explains it for running Python, or this SO answer that explains the procedure for Ruby. 例如,看看这个解释它用于运行Python的视频广播 ,或者这个解释Ruby过程的答案 。
Running Python on Windows is also covered in the Python on Windows FAQ. 在Windows上的Python FAQ中也介绍了在Windows上运行Python 。
The static library (libpython32.a) you throught is not a real static library, it contains only the definiation of python32.dll. 您经常使用的静态库(libpython32.a)不是真正的静态库,它只包含python32.dll的定义。 so it is nothing but a wrapper to python32.dll.
所以它只是python32.dll的包装器。
you must add the python install folder in your Windows PATH, so that Windows could found the dll by itself. 你必须在Windows PATH中添加python安装文件夹,以便Windows可以自己找到dll。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.