如何在C ++中创建DLL库,然后在C项目VisualStudio中使用它?

时间:2018-03-21 09:25:01

标签: c dll visual-studio-2015

如何在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错误,例如:

  • 期待&#39; {&#39;
  • 标识符* write1未定义自FILE *write1;

  • 标识符clock_t未定义自clock_t ticks;

  • 声明与&#34;浮动log10(float_Xx)&#34;不兼容(在第227行声明) - 在cmath中 - 我们有using _CSTD log10; using _CSTD modf; using _CSTD pow;
  • 的标准文件
  • 声明与&#34; double pow(double_Xx,int _Yx)&#34;不兼容(在第22行声明) - 在cmath中 - 我们有using _CSTD log10; using _CSTD modf; using _CSTD pow;
  • 的标准文件
  • 所以在文件cmath中 - 整个文件被标记为有错误
  • 以及此类类型的许多其他错误 - 意味着2种语言以某种方式被破坏。

我构建了dll(当然是在dll项目中),将.dll文件复制到找到exe的C Project文件夹中,我在解决方案资源管理器中添加了.lib文件并得到了这些错误。

我真的需要你的帮助,我到处寻找,并没有找到关于C项目中使用的C ++ .dll的使用的指南或任何内容。 :|

感谢您的时间。

1 个答案:

答案 0 :(得分:1)

在C ++ DLL项目中,打开项目属性并定义预处理器符号:

Project properties screenshot

然后在头文件中,根据是否定义了以红色圈出的预处理器符号来定义另一个符号:

#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项目中,不会定义符号。当包含头文件时,Ergo,DLLIMPORT_EXPORT评估为 __ declspec(dllimport)。执行此操作将导入该功能,您将能够使用它。如果不这样做,将在尝试调用该函数时导致链接器错误(未解析的外部符号)。

希望这有帮助!

PS:您应该将头文件中不需要的所有#include移动到您的实现(CPP)文件中: - )

相关问题