不能在Visual Studio中包含Python.h [英] can't include Python.h in visual studio

查看:990
本文介绍了不能在Visual Studio中包含Python.h的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

(我确实发现了很多类似的问题,但到目前为止,这些都不适合我...)

(I do find a lot of similar questions, but so far non of these fits me...)

===========更新的错误消息,图像,命令行===========

===========Updated error message, images, Command Line===========

我正在Visual Studio中尝试#include <Python.h>(几乎所有代码,主要功能还几乎为空),但它不断提醒我cannot open source file "Python.h",如果我运行该程序,则会引发错误:

I am trying to #include <Python.h>(that's nearly all the code, the main function is almost empty yet) in visual studio, but it keeps reminding me cannot open source file "Python.h", if I run the program, it will raise an error:

fatal error C1083: Cannot open include file: 'Python.h': No such file or directory

.我在项目Property Pages> VC++ Directories中添加了include和library目录,无法正常工作,试图将路径添加至C/C++> Additional Include Directories,无法正常工作,我试图将其更改为发布模式,但仍无法正常工作...

. I added the include and library directories in project Property Pages > VC++ Directories, not working, tried to add the path to C/C++ > Additional Include Directories, not working, and I tried to change it to release mode, still not working...

=================更新2.0 ===============

=================Update 2.0================

我将%(AdditionalIncludeDirectories);添加到C/C++> Additional Include Directories,但似乎不起作用.

I add %(AdditionalIncludeDirectories); to C/C++ > Additional Include Directoriesbut seems not work.

然后我做了一些非常愚蠢的事情:我将标头和.dll复制到标头包含文件夹中...现在它不提醒我找不到Python.h,我可以编写代码:

Then I did something really stupid: I copied the headers and .dll to the header include folder... Now it doesn't remind me can't find Python.h any longer, I can code:

Py_Initialize();
PyRun_SimpleString("print('Hello Python!')");
Py_Finalize();

但是无法编译...我收到了新的错误消息:

but it won't compile... I got a new error message:

'"C:\Amarth\Programing\CPlusPlusLearning\Release\CPlusPlusLearning.exe"' is not recognized as an internal or external command,
operable program or batch file.

在Output中,它是:

And in Output, it's:

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 ==========

这似乎很明显,因为编译器只能找到函数声明,而不能找到定义...此外,根据我所看到的类似问题,大多数人在添加包含目录后解决了该问题.因此,如果我真的遇到一个棘手的问题,是否有可能找到并复制所有函数定义,然后使其以某种方式起作用?

That seems obvious as the compiler can only find function declaration but can't find the definition... Besides, according to the similar questions I've seen, most people solved the problem after add the include directories. So if I'm really facing a fancy problem, will it be possible to find and copy all the function definitions then make it work in some way ?

==============其他消息===============

==============OTHER MESSAGES===============

我正在使用Anaconda安装的python 3.5. includelibs文件夹位于C:\Users\Amarthgul\Anaconda3下,该文件夹也已添加到system vaiable> path中.我的计算机中还有一个Python 3.6,但通常我只使用其手册,但在python环境中并没有造成任何麻烦.命令行:

I'm using python 3.5, installed by Anaconda. The include and libs folder is under C:\Users\Amarthgul\Anaconda3, which is also added to system vaiable > path. There's also a Python 3.6 in my computer, but normally I only use its Manuals, and yet it haven't cause any trouble in python environment. Command Line:

/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"  

推荐答案

在Visual Studio社区2015中,我将Build \ Configuration Manager中的活动解决方案配置"从调试"更改为发布".那为我解决了这个问题.

In Visual Studio Community 2015 I changed the "Active solution configuration" in Build \ Configuration Manager from 'Debug' to 'Release. That solved this problem for me.

我从以下代码中获得了以下示例代码:嵌入在C ++中的Python教程

I got my following example code from: 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;
             }
};

这篇关于不能在Visual Studio中包含Python.h的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆