在 Visual Studio 中用 C(不是 C++)编写 dll 导致无法解析的符号

发布于 2024-08-24 10:43:39 字数 1155 浏览 3 评论 0原文

我一直在 MSDN 并且工作正常。然后,我删除了 dll 中的所有 C++ 样式代码,并将其替换为 C 等效代码,它仍然有效。

但是,当我将文件从 X.cpp 重命名为 Xc(我猜这会导致在 C 模式下编译)时,对于 dll 中的每个函数,我都会收到错误 LNK2019(无法解析的外部符号)。出于我的目的,dll 必须使用 C 而不是 C++ 语言,因为这是 Java Native Access 支持的。

这是 dll 的标头:

__declspec(dllexport) double Add(double a, double b);
__declspec(dllexport) double Subtract(double a, double b);
__declspec(dllexport) double Multiply(double a, double b);
__declspec(dllexport) double Divide(double a, double b);

这是使用该 dll 的 (C++) 测试程序的主体:(

#include <iostream>
#include "MyMathFuncs.h"
using namespace std;
int main()
{
    double a = 7.4;
    int b = 99;

    cout << "a + b = " <<
        Add(a, b) << endl;
    cout << "a - b = " <<
        Subtract(a, b) << endl;
    cout << "a * b = " <<
        Multiply(a, b) << endl;
    cout << "a / b = " <<
        Divide(a, b) << endl;

    return 0;
}

只是为了澄清测试程序是用 C++ 编写的,这很好;这只是我试图用 C 编译的 dll)。

I've been going through the dll walkthrough on MSDN and it works fine. I then removed all the C++ style code in the dll and replaced it with the C equivalent, and it still works.

BUT, when I rename the file from X.cpp to X.c (which I guess causes compilation in C-mode), I get error LNK2019 (unresolved external symbol) for every function in the dll. For my purposes it's essential that the dll be in C not C++ because that's what Java Native Access supports.

Here's the header of the dll:

__declspec(dllexport) double Add(double a, double b);
__declspec(dllexport) double Subtract(double a, double b);
__declspec(dllexport) double Multiply(double a, double b);
__declspec(dllexport) double Divide(double a, double b);

Here's the body of the (C++) testing program that uses the dll:

#include <iostream>
#include "MyMathFuncs.h"
using namespace std;
int main()
{
    double a = 7.4;
    int b = 99;

    cout << "a + b = " <<
        Add(a, b) << endl;
    cout << "a - b = " <<
        Subtract(a, b) << endl;
    cout << "a * b = " <<
        Multiply(a, b) << endl;
    cout << "a / b = " <<
        Divide(a, b) << endl;

    return 0;
}

(Just to clarify it's fine that the testing program is in C++; it's only the dll I'm trying to compile in C).

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

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

发布评论

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

评论(2

蘸点软妹酱 2024-08-31 10:43:39

添加

extern "C"
{
#include "MyMathFuncs.h"
}

Add

extern "C"
{
#include "MyMathFuncs.h"
}
落墨 2024-08-31 10:43:39

更改扩展名后,您现在在客户端代码中使用了错误的名称。这些名称不再像您将其编译为 C++ 代码时那样进行修饰。像这样导出名称的正确方法是,永远不会使用这些修饰,并且您不依赖于语言:

extern "C" __declspec(dllexport)
double Add(double a, double b);

要查看导出的名称,请在 DLL 上使用 Dumpbin.exe /exports。

After you changed the extension, you are now using the wrong names in the client code. Those names are no longer decorated as they were when you compiled it as C++ code. The proper way to export names like this, so that those decorations are never used and you don't depend on the language:

extern "C" __declspec(dllexport)
double Add(double a, double b);

To see the exported names, use Dumpbin.exe /exports on your DLL.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文