配置如下
VS2013项目: Win32 Console,Release X64
包含目录 : C:\Python27\include
库 目 录 : C:\Python27\libs
附加依赖项: Python27.lib
Python版本: Python 2.7.13
刚学习Python,想用C++来调用Python的脚本,找了个简单的例子编译没通过,首先确认包含目录、库目录、附加依赖项均已添加。
编译报错
1>------ 已启动全部重新生成: 项目: CallPy, 配置: Release x64 ------
1> callpy.cpp
1>callpy.obj : error LNK2001: 无法解析的外部符号 __imp_PyString_FromString
1>callpy.obj : error LNK2001: 无法解析的外部符号 __imp_PyCallable_Check
1>callpy.obj : error LNK2001: 无法解析的外部符号 __imp_PyTuple_New
1>callpy.obj : error LNK2001: 无法解析的外部符号 __imp_PyObject_CallObject
1>callpy.obj : error LNK2001: 无法解析的外部符号 __imp_PyTuple_SetItem
1>callpy.obj : error LNK2001: 无法解析的外部符号 __imp_Py_BuildValue
1>callpy.obj : error LNK2001: 无法解析的外部符号 __imp_Py_Initialize
1>callpy.obj : error LNK2001: 无法解析的外部符号 __imp_Py_IsInitialized
1>callpy.obj : error LNK2001: 无法解析的外部符号 __imp_PyImport_Import
1>callpy.obj : error LNK2001: 无法解析的外部符号 __imp_PyModule_GetDict
1>callpy.obj : error LNK2001: 无法解析的外部符号 __imp_PyDict_GetItemString
1>callpy.obj : error LNK2001: 无法解析的外部符号 __imp_PyRun_SimpleStringFlags
1>callpy.obj : error LNK2001: 无法解析的外部符号 __imp_Py_Finalize
1>d:\documents\visual studio 2013\Projects\CallPy\x64\Release\CallPy.exe : fatal error LNK1120: 13 个无法解析的外部命令
========== 全部重新生成: 成功 0 个,失败 1 个,跳过 0 个 ==========
解决方案
原因: 由于Python版本和项目编译版本不匹配导致这个问题,本文出错的原因是
解决: 1. 使用 Python x86版本,VS项目更改为Win32,包含目录及库目录一律为Python x86,可正常编译
2. 使用 Python x64版本,通过配置管理器,在活动解决方案平台中新建X64平台,包含目录及库目录一律为Python x64,可正常编译
参考例子源码链接:http://blog.csdn.net/taiyang1987912/article/details/44779719