MinGW ld 链接错误 - 未定义引用

0 投票
1 回答
38 浏览
提问于 2025-04-14 15:55

我希望在这个人工智能的时代,至少还有一些人能帮忙解决那些“全知全能”的GPT无法处理的问题。

问题描述

我正在尝试为一个C++库创建一个Python接口,但有几个模块出现了链接错误。下面是错误的详细信息。

错误详情

2024-03-14 18:26:40,630 - ERROR - 
        ------------------------------
        Error compiling stepper_motor:
        
        Error code: 1
        Error: C:/SysGCC/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Users\User\AppData\Local\Temp\ccfElxip.o:stepper_motor_wrap.cxx:(.text+0x178a4): undefined reference to `SBC_RequestTriggerSwitches'collect2.exe: error: ld returned 1 exit status

        g++ command: ['g++', '-shared', '-o', 'd:\\CZI_scope\\code\\pymodules\\thorlabs_kinesis\\motion_control\\benchtop\\_stepper_motor.pyd', 'd:\\CZI_scope\\code\\pymodules\\thorlabs_kinesis\\motion_control\\benchtop\\stepper_motor_wrap.cxx', '-Ic:\\Users\\User\\mambaforge\\envs\\rich\\include', '-Id:\\CZI_scope\\code\\pymodules\\thorlabs_kinesis\\__include', '-Lc:\\Users\\User\\mambaforge\\envs\\rich', '-Ld:\\CZI_scope\\code\\pymodules\\thorlabs_kinesis\\__lib', '-lpython39', '-lThorlabs.MotionControl.Benchtop.StepperMotor', '-Wno-error']        
        ------------------------------

出问题的符号是在头文件中定义的(所以是链接错误而不是编译错误)。我用dumpbin工具查看了这个.lib文件的导出内容,输出结果显示这个名字被“搞乱”了。我知道这在C++库中是很常见的,所以我不确定这是否就是问题所在。

Dumpbin输出

?RequestTriggerSwitches@CBenchtopStepperMotorChannel@StepperMotor@Benchtop@MotionControl@Thorlabs@@QEBAFXZ (public: short __cdecl Thorlabs::MotionControl::Benchtop::StepperMotor::CBenchtopStepperMotorChannel::RequestTriggerSwitches(void)const )

头文件定义

#ifdef BENCHTOPSTEPPERMOTORDLL_EXPORTS
/// <summary> Gets the Benchtop Stepper API. </summary>
#define BENCHTOPSTEPPERMOTOR_API __declspec(dllexport)
#else
#define BENCHTOPSTEPPERMOTOR_API __declspec(dllimport)
#endif
extern "C"
{
BENCHTOPSTEPPERMOTOR_API short __cdecl SBC_RequestTriggerSwitches(char const * serialNo, short channel);
...
}

SWIG接口文件


%module stepper_motor

// Remove calling convention macros compatibility with SWIG
#define __cdecl
#define __stdcall
#define __declspec(x) 
#define WINAPI

%{
#include <windows.h>
#include <stdbool.h>
#define BENCHTOPSTEPPERMOTORDLL_EXPORTS 
#include "Thorlabs.MotionControl.Benchtop.StepperMotor.h"
%}
%include "Thorlabs.MotionControl.Benchtop.StepperMotor.h"

1 个回答

0

我使用了 dependencies.exe 工具,查看了这个DLL文件的导出内容,发现只有两个导出项和头文件中的函数签名有点相似:

717 (0x02cd),  (0x), public: short __cdecl Thorlabs::MotionControl::Benchtop::StepperMotor::CBenchtopStepperMotorChannel::RequestTriggerSwitches(void)const __ptr64, 0x000156a0, Microsoft
718 (0x02ce),  (0x), public: short __cdecl Thorlabs::MotionControl::Benchtop::StepperMotor::CStepperMotorCtrlBase::RequestTriggerSwitches(short) __ptr64, 0x0002b160, Microsoft

因为这两个导出项都和头文件里的内容不匹配,结合Criminal_Affair和其他人提到的情况,我能想到的唯一解释就是,尽管有头文件和文档说明,这个函数实际上并没有在DLL中导出。

谢谢大家的帮助。

撰写回答