我仍然不完全确定如何在 Visual Studio 中正确编写 DLL。
文件.h
#ifndef UPLOAD_H_
# define UPLOAD_H_
# ifdef UPLOAD_EXPORT
# define UPLOAD_API __declspec(dllimport)
# else
# define UPLOAD_API __declspec(dllexport)
# endif // UPLOAD_EXPORT
#include <Windows.h>
#include <WinInet.h>
extern "C" UPLOAD_API int uploadFTP(
const char *...,
const char *...,
const char *...,
const char *...,
const char *...,
const char *...);
#endif // UPLOAD_H_
文件.cpp
#include "upload.h"
extern "C" UPLOAD_API int uploadFTP(
const char *...,
const char *...,
const char *...,
const char *...,
const char *...,
const char *...)
{
...
}
这实际上有效,但在 MSDN 上写在 .h 文件上:
#ifdef MATHFUNCSDLL_EXPORTS
#define MATHFUNCSDLL_API __declspec(dllexport)
#else
#define MATHFUNCSDLL_API __declspec(dllimport)
#endif
这实际上与我所做的相反,他们没有在 .cpp 文件的函数上指定 MATHFUNCSDLL_API。
编辑:
解决方案 -> UPLOAD_EXPORT 未在 Project Properties/C++/Preprocessor 下正确定义
最佳答案
MSDN 的.h 文件中的#define
语句是正确的。您在构建 DLL 时定义了 UPLOAD_EXPORTS
,因此所有 UPLOAD_API 函数都将声明为 dllexport
。您无需在其他任何地方定义它,因此所有客户端都会将它们视为 dllimport
。
PS:如果您希望从其他语言使用它们,您可能还想声明您的函数 WINAPI
(这意味着 __stdcall
)比C和C++。但是请注意,使用 dllexport
而不是 .def 文件会导出带有调用约定特定装饰(前导下划线等)的函数名称
关于c++ - 正确编写DLL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17170542/