4

我想在我的 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 Bit + Eclipse IDE for C/C++ Developers,版本:Juno Service Release 1 + mingw + python32

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

编译和链接似乎可以工作,但是当我尝试启动 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
4

2 回答 2

3

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

如何在 Windows 上更改的说明PATH很容易在 Google 上搜索;例如,请参阅解释运行 Python 的视频广播,或解释Ruby 过程的SO 答案。

Python on Windows FAQ中也介绍了在 Windows 上运行 Python 。

于 2012-12-08T22:12:32.380 回答
0

你看到的静态库(libpython32.a)并不是真正的静态库,它只包含python32.dll的定义。所以它只不过是 python32.dll 的包装器。

您必须在 Windows PATH 中添加 python 安装文件夹,以便 Windows 可以自己找到 dll。

于 2012-12-11T05:47:33.680 回答