c++ - C++ 中的 Python 代码

标签 c++ python eclipse python-embedding

我想在我的 C++ 框架中使用一些 Python 代码来绘制一些统计数据。我已经找到了以下帖子(关于如何在 C++ 中嵌入 Python),但遵循说明并没有成功:Embed python code in C++ (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;
}

很抱歉,我没有太多制作文件或附加静态或动态库的经验....

我必须遵循以下系统: Windows 7 + 64 位 + 面向 C/C++ 开发人员的 Eclipse IDE,版本:Juno Service Release 1 + mingw + python32

在路径和符号下: + 添加了 python32 的 include 目录 + 添加了对应于 libpython32.a 的库“python32” + 添加库路径

编译和链接似乎工作正常,但是当我尝试启动 exe 时,我收到以下消息:

“该程序无法启动,因为您的计算机缺少 python32.dll。请尝试重新安装该程序以解决此问题。”

我无法理解此消息,因为我尝试将静态库 (libpython32.a) 添加到源中。你能轻轻地插入我朝着正确的方向前进吗?

非常感谢您的帮助!

编辑: 添加了 makefile 和 objects.mk

生成文件 ################################################## ############################## # 自动生成的文件。不要编辑! ################################################## ##############################

-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

对象.MK

################################################################################
# Automatically-generated file. Do not edit!
################################################################################

USER_OBJS :=

LIBS := -lgdi32 -ljpeg-8 -ltiff-5 -lpython32

最佳答案

在 Windows 上,程序搜索路径和共享库搜索路径由相同的环境变量 PATH 控制。要嵌入 Python,您需要将包含 python32.dll 的目录(通常为 c:\python3.2)放入您的 PATH

有关如何在 Windows 上更改 PATH 的说明很容易用 google 搜索;参见例如 this videocast这解释了它用于运行 Python,或 this SO answer解释了 Ruby 的过程。

在 Windows 上运行 Python 也包含在 the Python on Windows FAQ.

关于c++ - C++ 中的 Python 代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13781561/

相关文章:

c++ - 使用 CMake 检测支持的 C++ 标准

c++ - `substr` 是否使用相同的底层内存?

c++ - 在 MFC 功能区栏中删除以编程方式添加的面板分隔符

python - 使用 python ElementTree 内存不足

c++ - 从导致AccessViolationException的线程访问文件缓冲区

python - 修改Z3 python中的变量

python - 具有颜色渐变的 Matplotlib 3D 散点图

java - 由于 org.eclipse.core.runtime 无法启动 eclipse

c++ - 为 cmake 生成的 eclipse 项目链接 boost::program_options 和 Armadillo 之间的错误

java - Eclipse - 数组无法解析