Solution for link error:Cannot Open File 'python27_d.lib'

本文介绍了解决在C调用Python时遇到的“LINK:fatal error LNK1104: cannot open file 'python27_d.lib'”错误的方法。通过调整预处理器定义或在代码中添加特定宏,可以成功避免此问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

引自:http://guangboo.org/2013/01/17/solution-link-errorcannot-open-file-python27_dlib

感谢原作者

使用C调用Python函数或解释器时,在添加了Include Directories和Library Directories等配置后,仍然编译错误:"LINK : fatal error LNK1104: cannot open file 'python27_d.lib'".代码如下:

复制代码
#include <stdio.h>
#include <Python.h>

int main(void)
{
    Py_Initialize();
    if(!Py_IsInitialized()){
        printf("Python Initialize failed.\n");
        return 1;
    }

    PyRun_SimpleString("print('hello world!')");
    Py_Finalize();
    system("pause");

    return 0;
}
复制代码

 

Stackoverflow贴出了pyconfig.h头文件中对Python27_d.lib的引用(http://stackoverflow.com/questions/11311877/creating-a-dll-from-a-wrapped-cpp-file-with-

swig/14372472#14372472),该文件用于调试用,如下代码:

复制代码
/* For Windows the Python core is in a DLL by default.  Test
Py_NO_ENABLE_SHARED to find out.  Also support MS_NO_COREDLL for b/w compat */
#if !defined(MS_NO_COREDLL) && !defined(Py_NO_ENABLE_SHARED)
#    define Py_ENABLE_SHARED 1 /* standard symbol for shared library */
#    define MS_COREDLL    /* deprecated old symbol */
#endif /* !MS_NO_COREDLL && ... */

/*  All windows compilers that use this header support __declspec */
#define HAVE_DECLSPEC_DLL

/* For an MSVC DLL, we can nominate the .lib files used by extensions */
#ifdef MS_COREDLL
#    ifndef Py_BUILD_CORE /* not building the core - must be an ext */
#        if defined(_MSC_VER)
            /* So MSVC users need not specify the .lib file in
            their Makefile (other compilers are generally
            taken care of by distutils.) */
#            ifdef _DEBUG
#                pragma comment(lib,"python27_d.lib")
#            else
#                pragma comment(lib,"python27.lib")
#            endif /* _DEBUG */
#        endif /* _MSC_VER */
#    endif /* Py_BUILD_CORE */
#endif /* MS_COREDLL */
复制代码

 

  

可以发现,只有MS_NO_COREDLL或Py_NO_ENABLE_SHARED被定义了,那么#ifdef MS_COREDLL块的代码就不会被编译。因此可以在Configuration Properties->C/C++->Preprocessor->Preprocessor Definitions添加MS_NO_COREDLL或Py_NO_ENABLE_SHARED,去掉_DEBUG,将没有效果。也可以在引用python.h头文件前添加#define MS_NO_COREDLL或#define Py_NO_ENABLE_SHARED。

RuntimeError: SetPrecisionMode:build/CMakeFiles/torch_npu.dir/compiler_depend.ts:155 NPU function error: at_npu::native::AclSetCompileopt(aclCompileOpt::ACL_PRECISION_MODE, precision_mode), error code is 500001 [ERROR] 2025-07-07-15:58:56 (PID:4001235, Device:0, RankID:-1) ERR00100 PTA call acl api failed [Error]: The internal ACL of the system is incorrect. Rectify the fault based on the error information in the ascend log. EC0010: [PID: 4001235] 2025-07-07-15:58:56.704.356 Failed to import Python module [AttributeError: `np.float_` was removed in the NumPy 2.0 release. Use `np.float64` instead..]. Solution: Check that all required components are properly installed and the specified Python path matches the Python installation directory. (If the path does not match the directory, run set_env.sh in the installation package.) TraceBack (most recent call last): AOE Failed to call InitCannKB[FUNC:Initialize][FILE:python_adapter_manager.cc][LINE:47] Failed to initialize TeConfigInfo. [GraphOpt][InitializeInner][InitTbeFunc] Failed to init tbe.[FUNC:InitializeTeFusion][FILE:tbe_op_store_adapter.cc][LINE:1889] [GraphOpt][InitializeInner][InitTeFusion]: Failed to initialize TeFusion.[FUNC:InitializeInner][FILE:tbe_op_store_adapter.cc][LINE:1856] [SubGraphOpt][PreCompileOp][InitAdapter] InitializeAdapter adapter [tbe_op_adapter] failed! Ret [4294967295][FUNC:InitializeAdapter][FILE:op_store_adapter_manager.cc][LINE:79] [SubGraphOpt][PreCompileOp][Init] Initialize op store adapter failed, OpsStoreName[tbe-custom].[FUNC:Initialize][FILE:op_store_adapter_manager.cc][LINE:120] [FusionMngr][Init] Op store adapter manager init failed.[FUNC:Initialize][FILE:fusion_manager.cc][LINE:115] PluginManager InvokeAll failed.[FUNC:Initialize][FILE:ops_kernel_manager.cc][LINE:83] OpsManager initialize failed.[FUNC:InnerInitialize][FILE:gelib.cc][LINE:259] GELib::InnerInitialize failed.[FUNC:Initialize][FILE:gelib.cc][LINE:184] GEInitialize failed.[FUNC:GEInitialize][FILE:ge_api.cc][LINE:371] [Initialize][Ge]GEInitialize failed. ge result = 4294967295[FUNC:ReportCallError][FILE:log_inner.cpp][LINE:161] [Init][Compiler]Init compiler failed[FUNC:ReportInnerError][FILE:log_inner.cpp][LINE:145] [Set][Options]OpCompileProcessor init failed![FUNC:ReportInnerError][FILE:log_inner.cpp][LINE:145]
最新发布
07-08
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值