2012-12-08 33 views
4

我想在我的C++框架中使用一些Python代碼來繪製一些統計信息。我已經發現了以下發布(關於如何使嵌入在C++中的Python),但下面的說明並沒有導致成功:Embed python code in C++ (Windows + minGW + Python 2.7.2 + Eclipse)C++中的Python代碼

#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; 
} 

我很抱歉,但我沒有用make文件或附加太多的經驗靜態或動態庫....

我不得不以下系統: 視窗7 + 64 +位的Eclipse IDE爲C/C++開發人員,版本:朱諾服務版本1 + mingw的+ python32

下路徑和符號: +增加了包含python32 01的目錄添加庫「python32」這應該對應於libpython32.a +添加庫路徑

的編譯和鏈接似乎工作,但是當我嘗試啓動EXE,我得到以下信息:

「程序無法啓動,因爲您的計算機缺少python32.dll。嘗試重新安裝程序來解決這個問題。」

,因爲我嘗試靜態庫(libpython32.a)添加到源我無法理解這樣的消息。你能給我輕輕一推在正確的方向?

非常感謝您的幫助

編輯:! 添加和makefile objects.mk

MAKEFILE ##################### ################################################## ######### #自動生成的文件,不要編輯! ################################################# ###############################

-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: [email protected]' 
@echo 'Invoking: Cross G++ Linker' 
g++ -L"C:\Python32\libs" -o "Sandbox.exe" $(OBJS) $(USER_OBJS) $(LIBS) 
@echo 'Finished building target: [email protected]' 
@echo ' ' 

# Other Targets 
clean: 
-$(RM)  $(C++_DEPS)$(OBJS)$(C_DEPS)$(CC_DEPS)$(CPP_DEPS)$(EXECUTABLES)$(CXX_DEPS)$(C_UPPER_DEPS) Sandbox.exe 
[email protected] ' ' 

.PHONY: all clean dependents 
.SECONDARY: 

-include ../makefile.targets 

OBJECTS.MK

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

USER_OBJS := 

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

把這裏放在你的makefile文件 – 0x90

+0

順便說一句:也可以考慮相反的做法:讓你的C++統計代碼進入Python模塊。 SWIG會讓你更容易。 – Kos

+0

請參閱[在Windows上嵌入Python:爲什麼它必須是DLL?](http://stackoverflow.com/q/3953039/222914) –

回答

3

在Windows程序搜索路徑和共享庫搜索路徑由相同的環境變量PATH控制。要嵌入Python,您需要將包含python32.dll的目錄(通常爲c:\python3.2)放在您的PATH上。

解釋如何在Windows上更改PATH很容易上傳;例如,請參閱this videocast,它解釋了它運行Python的原因,或者this SO answer,它解釋了Ruby的過程。

在Windows上運行的Python也包括在the Python on Windows FAQ.

0

靜態庫(libpython32.a)你throught是不是一個真正的靜態庫,它僅包含python32.dll的definiation。所以它只不過是python32.dll的一個包裝。

您必須在您的Windows PATH中添加python install文件夾,以便Windows可以自己找到dll。