对.dll库中函数的未定义引用

Undefined Reference to Functions from .dll Library

本文关键字:未定义 引用 函数 dll      更新时间:2023-10-16

前言:我在Windows 10机器上使用Code::Blocks软件,并用C++编程。我正在使用普林斯顿仪器科学CCD相机的库。

我会尽量在这里具体一点。我正试图使用控制普林斯顿仪器相机(PIcam)的多个功能创建一个.dll文件。我之所以制作这个.dll,是因为我想把这个代码(在C++中)嵌入到另一个Python程序中。这是我当前的相关main.cpp代码:

#include "main.h"
#include "stdio.h"
#include "picam.h"
// a sample exported function
void DLL_EXPORT SomeFunction(const LPCSTR sometext)
{
    MessageBoxA(0, sometext, "DLL Message", MB_OK | MB_ICONINFORMATION);
}
void DLL_EXPORT connectCamera()
{
    Picam_InitializeLibrary();
    PicamHandle camera;
    PicamCameraID id;
    //const pichar* string;
    //PicamAvailableData data;
    //PicamAcquisitionErrorsMask errors;
    //piint readoutstride = 0;
    if (Picam_OpenFirstCamera( &camera ) == PicamError_None )
        Picam_GetCameraID( camera, &id );
    else {
        Picam_ConnectDemoCamera(
            PicamModel_Pixis100F,
            "0008675309",
            &id );
            Picam_OpenCamera( &id, &camera );
        printf( "No Camera Detected, Creating Demo Cameran" );
    }
}

但是编译器在我构建代码后会给我这些错误。它声称我调用的每一个函数都是未定义的引用,即使在我成功链接了Code::Blocks中的库之后也是如此。

我知道我的图书馆链接正确。所有这些函数都位于Picam.lib库中,我知道它的链接是正确的。以下是显示它的构建日志代码:

mingw32-g++.exe -shared -Wl,--output-def=binDebuglibSampleDLL.def -Wl,--out-implib=binDebuglibSampleDLL.a -Wl,--dll -LC:UsersPhilipDocumentsCppProjectsSampleDLL objDebugmain.o  -o binDebugSampleDLL.dll  -lPicam -luser32 -lPicam C:UsersPhilipDocumentsCppProjectsSampleDLLPicam.lib
objDebugmain.o: In function `Z13connectCamerav':
C:/Users/Philip/Documents/CppProjects/SampleDLL/main.cpp:13: undefined reference to `_imp__Picam_InitializeLibrary@0'
C:/Users/Philip/Documents/CppProjects/SampleDLL/main.cpp:21: undefined reference to `_imp__Picam_OpenFirstCamera@4'
C:/Users/Philip/Documents/CppProjects/SampleDLL/main.cpp:22: undefined reference to `_imp__Picam_GetCameraID@8'
C:/Users/Philip/Documents/CppProjects/SampleDLL/main.cpp:27: undefined reference to `_imp__Picam_ConnectDemoCamera@12'
C:/Users/Philip/Documents/CppProjects/SampleDLL/main.cpp:28: undefined reference to `_imp__Picam_OpenCamera@8'
collect2.exe: error: ld returned 1 exit status
Creating library file: binDebuglibSampleDLL.a
Process terminated with status 1 (0 minute(s), 0 second(s))
5 error(s), 0 warning(s) (0 minute(s), 0 second(s))

我不知道还能做些什么来解决这个问题。看起来一切都是正确的,但函数仍然无法在库中识别。有人有什么想法吗?

我知道我的图书馆链接正确。

那你就做好了,不是吗!

然而,如果你愿意保持开放的心态并真正理解链接过程,你可能应该知道gcc(你正在使用)不知道如何处理.lib文件,它们是Microsoft库文件。gcc的库文件以.a.so结尾。