如何在C++中创建一个DLL库,然后在C项目VisualStudio(2015)中使用它?
我只看到了 1 个 question类似于我的问题,但我无法从中理解太多。
我看过很多关于如何将用 C++ 编写的 .dll 用于另一个 C++ 项目的教程,一个用于 C# 的 C .dll,但没有关于如何将 C++ .dll 用于 C VS 项目的示例。 我真的需要帮助,我在互联网上搜索过,尝试了各种“解决方案”来解决我的问题,但仍然没有任何解决方案。
我真的需要你的帮助。
C++ dll工程有以下内容:
//C++ dll Header Library, having the name "dllDelay.h":
#include <iostream>
#include <stdio.h>
#include <windows.h>
extern "C" __declspec(dllexport) void dllDelay(DWORD dwMsec);
#endif
//C++ .cpp file named "dllDelay.cpp":
#include "dllDelay.h"
#include "stdafx.h"
extern "C" __declspec(dllexport)
void dllDelay(DWORD dwMsec) {
Sleep(dwMsec);
}
C VisualStudio(2015) 项目内容如下:
/*This function is intended to Delay 10 seconds, measure that elapsed time
and write it into a file. I've checked this function using Sleep() instead of
dllDelay() and it worked fine, so this function has no errors.*/
#include "dllDelay.h"
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <windows.h>
int main(void)
{
FILE *write1;
// measure elapsed time on windows with QueryPerformanceCounter() :
LARGE_INTEGER frequency; // ticks per second
LARGE_INTEGER t1, t2; // ticks
double elapsedTime;
write1 = fopen("write_difftime_1Test.txt", "w");
fprintf(write1, "\n Sleep : 10000ms = 10s");
time_t start, stop;
clock_t ticks;
long count;
double i = 0, v = 0, j = 0;
//make 10 such measurements and write them into file
while ((j < 10) && ((write1 != NULL) && (TRUE != fseek(write1, 0L, SEEK_END))))
{
QueryPerformanceFrequency(&frequency);
QueryPerformanceCounter(&t1);
time(&start);
//The function from dll
dllDelay(10000);
time(&stop);
QueryPerformanceCounter(&t2);
// compute and print the elapsed time in millisec
elapsedTime = (t2.QuadPart - t1.QuadPart) * 1000.0 / frequency.QuadPart;
fprintf(write1, "\n Elapsed time : %lf s. timeDiff time: %f in seconds\n\n", elapsedTime / 1000, difftime(stop, start));
j++;
}
fclose(write1);
return 0;
} `
此功能旨在延迟 10 秒,测量耗时 并将其写入文件。 我已经使用 Sleep() 而不是 dllDelay() 并且它工作正常,所以这个函数没有错误。
但是当我使用 #include "dllDelay.h"
我得到 3111 错误 例如:
- 期待一个'{'
FILE *write1;
未定义标识符 *write1
clock_t ticks;
未定义标识符 clock_t
- 声明与“float log10(float_Xx)”(在第 227 行声明)不兼容 - 在 cmath 中 - 我们有
using _CSTD log10 的标准文件;使用 _CSTD modf;使用 _CSTD 战俘;
- 声明与“double pow(double_Xx, int _Yx)”(在第 22 行声明)不兼容 - 在 cmath 中 - 我们有
using _CSTD log10 的标准文件;使用 _CSTD modf;使用 _CSTD 战俘;
- 因此在文件 cmath 中 - 整个文件被标记为有错误
- 以及许多其他此类错误 - 这意味着这两种语言在某种程度上被破坏了。
我构建了 dll(当然是在 dll 项目中),将 .dll 文件复制到找到 exe 的 C 项目文件夹中,我将 .lib 文件添加到解决方案资源管理器中,但出现了这些错误。
我真的需要你的帮助,我到处都找过了,但没有找到指南或任何关于在 C 项目中使用 C++ .dll 的信息。 :|
感谢您的宝贵时间。
最佳答案
在您的 C++ DLL 项目中打开项目属性并定义预处理器符号:
然后在你的头文件中,根据是否定义了红色圈出的预处理器符号定义另一个符号:
#ifdef CPPDLL_EXPORTS
#define DLLIMPORT_EXPORT __declspec(dllexport)
#else
#define DLLIMPORT_EXPORT __declspec(dllimport)
#endif
然后在你的函数声明前使用定义的符号:
DLLIMPORT_EXPORT void dllDelay(DWORD dwMsec);
这具有以下效果:
在您的 DLL 项目中,定义了符号 (DLLIMPORT_EXPORT)。因此,DLLIMPORT_EXPORT 将评估为 __declspec(dllexport)。在使用 DLL 的 C 项目中,不会定义该符号。因此,当包含头文件时,DLLIMPORT_EXPORT 的计算结果为 __declspec(dllimport)。这样做将导入该函数,您将能够使用它。如果不这样做,将在尝试调用该函数时导致链接器错误(未解析的外部符号)。
希望这对您有所帮助!
PS:您应该将头文件中不需要的所有#includes 移动到您的实现 (CPP) 文件:-)
关于c - 如何在 C++ 中创建 DLL 库,然后在 C 项目 VisualStudio 中使用它?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49402415/