问题
I'm trying to run Python modules in C++ using "#include <Python.h>", however, after setting the "Additional Include Dependencies" of the project to "\include" I get the following error when debuging,
LINK : fatal error LNK1104: cannot open file 'python27_d.lib'
I read that I should download the development version of Python, but I didn't find a link for that, plus, don't I just need the file 'python27_d.lib' to be copied to the "libs" folder?
Please note that I'm using the Anaconda distribution of Python.
Thanks in advance!
回答1:
I don't know much about python, but the message indicates that python27_d.lib either doesn't exist, or at least doesn't exist where the linker is looking for it.
You already fixed the compiler include issue, now find the python27_d.lib file with Windows Explorer and and add that path to the Additional Library Dependencies path. It's under Configuration -> Linker -> General -> Additional Library Directories.
The "_d" indicates it's a debug library, so you'll want that one for your Debug configuration, and the one without the "_d" (probably) for your release configuration.
回答2:
I normally circumvent this by using the non-debug Python lib in debug builds. Typically, this leads to code like:
#ifdef _DEBUG
#undef _DEBUG
#include <Python.h>
#define _DEBUG
#else
#include <Python.h>
#endif
where you hide the definition of _DEBUG during the inclusion of Python.h.
回答3:
Put visual studio in release mode instead of debug.
回答4:
You don't necessarily have to use a Python debug build... [even if you are not usimng boost] I would have a look at the boost.python documentation where they have a wrapper for Python.h which handles all the windows debug issues, so that you can build a debug extension against a release python dll.
http://www.boost.org/doc/libs/1_53_0/libs/python/doc/building.html#id19 Python Debugging Builds
回答5:
I ran into similar errors while attempting to use Boost.python to access Anaconda python packages from C++. Let me start off by saying that my personal impression of the C++ Boost libraries is that they are a great idea with incomplete documentation. There is a ton of documentation on boost.org, but it invariably seems to leave out critical details which the authors appear to consider too trivial to bother mentioning. But, let me get off my soapbox ...
The ongoing impetus for me to [hopefully, eventually] figure out how to get Boost.Python to work on my system is that there are so many great Python scientific packages [SymPy, Numpy, SciPy, matplotlib, etc] included in the Anaconda distribution and it would really be great to access them from C++ projects built with Qt Creator. And the Boost docs do seem to suggest that Boost.Python is supposed to do that for me. Alas, those docs seem to leave out critical details that the authors appear to consider too trivial to bother mentioning ...
Anyway, initially, I got a build error indicating that python.h could not be found. I got rid of that by adding these two statements to my Qt Creator project's .pro file, which tell qmake where Boost installed its include files and where Anaconda installed its python.h file on my system:
INCLUDEPATH += C:\boost_1_55_0
INCLUDEPATH += C:\Anaconda\include
After that, I got a LNK1104 error indicating that 'python27.lib' could not be found. I got rid of that by adding these two statements to my .pro file. The first tells qmake where to find Anaconda's python27.lib file. The second tells qmake where to find the boost.python binary:
LIBS += "C:/Anaconda/libs"
LIBS += "C:/boost_1_55_0/stage/lib/libboost_python-vc110-mt-gd-1_55.lib"
But, that is as far as I have gotten so far. I now get an error indicating it cannot open file 'C:/Anaconda/libs.obj' which I have not yet found a fix for. The error, of course, is caused by that file not existing. The challenge is to discover why it is being sought and where to find it.
回答6:
On Visual studio, you need to add 'Additional include directories' for the project. Steps below. right click on project -> properties -> c/c++ -> Additional Include Directories -> point it to 'Python\include' folder(ex: c:\python\include).
来源:https://stackoverflow.com/questions/16200997/why-doesnt-include-python-h-work