编译使用 dll 的代码时未解决的外部问题

发布于 2024-11-04 01:16:34 字数 1684 浏览 1 评论 0原文

我正在尝试开始在 VS 2005 中为我的代码使用 dll。我的代码非常简单,只是为了尝试一个测试用例。

testdll.h:

#ifdef TEST_EXPORTS
#define TESTDLLPORT   __declspec( dllexport )
#else
#define TESTDLLPORT   __declspec( dllimport )
#endif

namespace TestDLLNS
{
    static int s = 0;
    class MyTestDll {
    public:
        static TESTDLLPORT int printDLLFuncs();
    };
}

testdll.cpp:

// testdll.cpp : Defines the entry point for the DLL application.
//

#include "stdafx.h"
#include "testdll.h"

#ifdef _MANAGED
#pragma managed(push, off)
#endif

namespace TestDLLNS {
    int MyTestDll::printDLLFuncs() {
        cout << "DLL function called" << endl;
        return s;
    }
}
#ifdef _MANAGED
#pragma managed(pop)
#endif

test.cpp:

// test.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include "testdll.h"

int main(int argc, char* argv[])
{
    cout << "int: " << TestDLLNS::MyTestDll::printDLLFuncs() << endl;
    cout << "Called dll" << endl;
    return 0;
}

错误 1 ​​错误 LNK2019: 无法解析的外部符号“__declspec(dllimport) public: static int _cdecl TestDLLNS::MyTestDll::printDLLFuncs(void)” (函数 _main test.obj

dumpbin \exports testdllD.dll 中引用的 _imp_?printDLLFuncs@MyTestDll@TestDLLNS@@SAHXZ) 给出以下内容: 序号提示 RVA 名称

      1    0 0001105F ?printDLLFuncs@MyTestDll@TestDLLNS@@SAHXZ

因此该符号显然存在于 .dll 中。 Visual Studio 是否还应该创建一个 testdllD.lib 文件,我应该将其与 test.cpp 链接?如果是这样,我如何让 Visual Studio 制作 .dll 和 .lib。

编辑:我的导入/导出正确吗?据我了解,在编译 dll 时,您需要使用 dllexport,而在编译使用 dll 的可执行文件时,将使用 dllimport。

I'm trying to get started on using dll's for my code in VS 2005. The code I have is very simple, just to try a test case.

testdll.h:

#ifdef TEST_EXPORTS
#define TESTDLLPORT   __declspec( dllexport )
#else
#define TESTDLLPORT   __declspec( dllimport )
#endif

namespace TestDLLNS
{
    static int s = 0;
    class MyTestDll {
    public:
        static TESTDLLPORT int printDLLFuncs();
    };
}

testdll.cpp:

// testdll.cpp : Defines the entry point for the DLL application.
//

#include "stdafx.h"
#include "testdll.h"

#ifdef _MANAGED
#pragma managed(push, off)
#endif

namespace TestDLLNS {
    int MyTestDll::printDLLFuncs() {
        cout << "DLL function called" << endl;
        return s;
    }
}
#ifdef _MANAGED
#pragma managed(pop)
#endif

test.cpp:

// test.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include "testdll.h"

int main(int argc, char* argv[])
{
    cout << "int: " << TestDLLNS::MyTestDll::printDLLFuncs() << endl;
    cout << "Called dll" << endl;
    return 0;
}

Error 1 error LNK2019: unresolved external symbol "__declspec(dllimport) public: static int _cdecl TestDLLNS::MyTestDll::printDLLFuncs(void)" (_imp_?printDLLFuncs@MyTestDll@TestDLLNS@@SAHXZ) referenced in function _main test.obj

dumpbin \exports testdllD.dll gives the following:
ordinal hint RVA name

      1    0 0001105F ?printDLLFuncs@MyTestDll@TestDLLNS@@SAHXZ

So the symbol clearly exists in the .dll. Should Visual Studio also be creating a testdllD.lib files which I should be linking with test.cpp? If so, how I get visual studio to make both a .dll and a .lib.

Edit: Am I doing the importing/exporting correctly? From what I understand, whem compiling the dll you would want to use dllexport while when compiling the executable which uses the dll, dllimport would be used.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦在深巷 2024-11-11 01:16:34

有几点需要注意,希望您没有错过。

  1. 您遇到的是链接错误,意味着您的编译没问题,但存在链接错误。该错误表明链接未找到目标文件中引用的 printDLLFuncs() 函数的定义。
  2. 您必须提供在项目目录 -> 中定义函数的 .lib 文件的路径。库路径,或将其放在任何项目文件夹中,以便 Visual Studio 可以找到它。

There are few things to note and I hope you did not miss them.

  1. What you have is a linking error, means that your compiling was okay, but there is a link error. The error shows that the linking does not find a definition for your printDLLFuncs() function which was referenced in the object file.
  2. You either have to provide the path for the .lib file where the function is defined in the Project Directories -> library path, or put it in any of the project folders so your Visual Studio can find it.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文