(我确实发现了很多类似的问题,但到目前为止还没有这些问题适合我......)
===========更新了错误消息,图像,命令行===========
我正在尝试#include <Python.h>
(几乎所有的代码,主要功能几乎是空的)在visual studio中,但它一直提醒我cannot open source file "Python.h"
,如果我运行程序,它会引发错误:
fatal error C1083: Cannot open include file: 'Python.h': No such file or directory
。我在项目Property Pages
&gt;中添加了include和library目录。 VC++ Directories
,无法正常工作,尝试将路径添加到C/C++
&gt; Additional Include Directories
,无法正常工作,我尝试将其更改为发布模式,但仍无法正常工作......
=================更新2.0 ================
我将%(AdditionalIncludeDirectories);
添加到C/C++
&gt; Additional Include Directories
但似乎行不通。
然后我做了一些非常愚蠢的事情:我将标题和.dll复制到标题包含文件夹......现在它不会提醒我不能再找到Python.h了,我可以编写代码:
Py_Initialize();
PyRun_SimpleString("print('Hello Python!')");
Py_Finalize();
但它不会编译......我收到了一条新的错误消息:
'"C:\Amarth\Programing\CPlusPlusLearning\Release\CPlusPlusLearning.exe"' is not recognized as an internal or external command,
operable program or batch file.
在输出中,它是:
1>------ Build started: Project: CPlusPlusLearning, Configuration: Release Win32 ------
1> PartOne.cpp
1>PartOne.cpp(34): warning C4244: 'argument': conversion from 'double' to 'float', possible loss of data
1>PartOne.obj : error LNK2001: unresolved external symbol __imp__Py_Finalize
1>PartOne.obj : error LNK2001: unresolved external symbol __imp__Py_Initialize
1>PartOne.obj : error LNK2001: unresolved external symbol __imp__PyRun_SimpleStringFlags
1>C:\Amarth\Computer_Graphics\Programing\CPlusPlusLearning\Release\CPlusPlusLearning.exe : fatal error LNK1120: 3 unresolved externals
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
这似乎是显而易见的,因为编译器只能找到函数声明但无法找到定义...此外,根据我见过的类似问题,大多数人在添加include目录后解决了这个问题。因此,如果我真的面临一个奇特的问题,是否有可能找到并复制所有函数定义,然后以某种方式使其工作?
==============其他消息===============
我正在使用由Anaconda安装的python 3.5。 include
和libs
文件夹位于C:\Users\Amarthgul\Anaconda3
下,该文件夹也已添加到system vaiable
&gt; path
。我的计算机中也有Python 3.6,但通常我只使用它的手册,但它在python环境中没有任何问题。命令行:
/GS /GL /W3 /Gy /Zc:wchar_t /I"C:\Users\Amarthgul\Anaconda3\include" /Zi /Gm- /O2 /sdl /Fd"x64\Release\vc140.pdb" /Zc:inline /fp:precise /D "NDEBUG" /D "_CONSOLE" /D "_UNICODE" /D "UNICODE" /errorReport:prompt /WX- /Zc:forScope /Gd /Oi /MD /Fa"x64\Release\" /EHsc /nologo /Fo"x64\Release\" /Fp"x64\Release\CPlusPlusLearning.pch"
答案 0 :(得分:3)
在Visual Studio社区2015中,我将Build \ Configuration Manager中的“Active solution configuration”从“Debug”更改为“Release”。这为我解决了这个问题。
我从以下代码获得了以下示例代码:Tutorial Python embedded in C++
#include <python.h>
#include <stdio.h>
#include <conio.h>
int main()
{
CPyInstance pyInstance;
PyRun_SimpleString("print('Hello World from Embedded Python!!!')");
printf("\nPress any key to exit...\n");
if (!_getch()) _getch();
return 0;
}
class CPyInstance
{
public:
CPyInstance()
{
Py_Initialize();
}
~CPyInstance()
{
Py_Finalize();
}
};
class CPyObject
{
private:
PyObject* p;
public:
CPyObject() : p(NULL)
{ }
CPyObject(PyObject* _p) : p(_p)
{ }
~CPyObject()
{
Release();
}
PyObject* getObject()
{
return p;
}
PyObject* setObject(PyObject* _p)
{
return (p = _p);
}
PyObject* AddRef()
{
if (p)
{
Py_INCREF(p);
}
return p;
}
void Release()
{
if (p)
{
Py_DECREF(p);
}
p = NULL;
}
PyObject* operator ->()
{
return p;
}
bool is()
{
return p? true : false;
}
operator PyObject* ()
{
return p;
}
PyObject* operator = (PyObject* pp)
{
p = pp;
return p;
}
operator bool()
{
return p ? true : false;
}
};
答案 1 :(得分:1)