在 Visual Studio 中用 C(不是 C++)编写 dll 导致无法解析的符号
我一直在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
添加
Add
更改扩展名后,您现在在客户端代码中使用了错误的名称。这些名称不再像您将其编译为 C++ 代码时那样进行修饰。像这样导出名称的正确方法是,永远不会使用这些修饰,并且您不依赖于语言:
要查看导出的名称,请在 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:
To see the exported names, use Dumpbin.exe /exports on your DLL.