gpt4 book ai didi

c++ - 无法创建简单的 DLL 示例

转载 作者:行者123 更新时间:2023-11-28 01:29:59 25 4
gpt4 key购买 nike

我关注了microsoft documentation并在 DLL 项目应该像这样编译时偶然发现:

1>------ Build started: Project: MathLibrary, Configuration: Debug Win32 ------  
1> stdafx.cpp
1> dllmain.cpp
1> MathLibrary.cpp
1> Creating library c:\users\username\documents\visual studio 2015\Projects\MathLibraryAndClient\Debug\MathLibrary.lib and object c:\users\username\documents\visual studio 2015\Projects\MathLibraryAndClient\Debug\MathLibrary.exp
1> MathLibrary.vcxproj -> c:\users\username\documents\visual studio 2015\Projects\MathLibraryAndClient\Debug\MathLibrary.dll
========== Build: 1 succeeded, 0 failed, 0 up-to-date, 0 skipped ==========

但是我成功地编译了我的 DLL 项目,但我的输出较少。结果我的主应用程序无法编译,因为编译器说“没有你的 DLL 的 .lib 文件”。

DLL 项目的输出如下所示:

1>------ Rebuild All started: Project: testDLL, Configuration: Debug Win32 ------
1>stdafx.cpp
1>dllmain.cpp
1>someClass.cpp
1>testDLL.cpp
1>Generating Code...
1>testDLL.vcxproj -> D:\stud\VStest\testDLL\Debug\testDLL.dll
========== Rebuild All: 1 succeeded, 0 failed, 0 skipped ==========

我的输出显然缺少“创建库”部分。我不明白怎么了,我的 VS 是 2017。

附言MS 在 CPP 文件中的注释是什么意思?

// Compile by using: cl /EHsc /DMATHLIBRARY_EXPORTS /LD MathLibrary.cpp 

最佳答案

在您的 DLL 项目中转到

项目设置 -> C/C++ -> 预处理器 -> 预处理器定义

并添加

MATHLIBRARY_EXPORTS 定义

所以 MATHLIBRARY_API

#ifdef MATHLIBRARY_EXPORTS  
#define MATHLIBRARY_API __declspec(dllexport)
#else
#define MATHLIBRARY_API __declspec(dllimport)
#endif

将被解析为__declspec(dllexport)。这将指示编译器/链接器将标有此属性的函数导出到 DLL 导出部分以及导入库(如果您将项目设置为 create import library)。

此外,您可以按如下方式实现您的类:

namespace MathLibrary  
{
// This class is exported from the MathLibrary.dll
class MATHLIBRARY_API Functions
{
public:
// Returns a + b
static double Add(double a, double b);

// Returns a * b
static double Multiply(double a, double b);

// Returns a + (a * b)
static double AddMultiply(double a, double b);
};
}

或者您可以使用 def files而不是 __declspec

关于c++ - 无法创建简单的 DLL 示例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51923303/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com