如何在C++;然后在C项目VisualStudio中使用它? 如何在C++中创建一个DLL库,然后在C Project VisualStudio(2015)中使用它? > /P> //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 /*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;

如何在C++;然后在C项目VisualStudio中使用它? 如何在C++中创建一个DLL库,然后在C Project VisualStudio(2015)中使用它? > /P> //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 /*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;,c,dll,visual-studio-2015,C,Dll,Visual Studio 2015,我只见过1个类似我的问题,但我不能从中了解太多 我见过很多教程,如何使用C++编写的DLL到另一个C++项目中,一个C语言中使用的.dll,但没有一个例子,说明如何将C++ +dll应用到C vs项目中。 我真的需要帮助,我在互联网上到处搜索,尝试了各种各样的“解决方案”,但仍然没有任何解决方案 我真的需要你的帮助 >强> C++ DLL项目有以下内容:>/P> //C++ dll Header Library, having the name "dllDelay.h": #include

我只见过1个类似我的问题,但我不能从中了解太多

<>我见过很多教程,如何使用C++编写的DLL到另一个C++项目中,一个C语言中使用的.dll,但没有一个例子,说明如何将C++ +dll应用到C vs项目中。 我真的需要帮助,我在互联网上到处搜索,尝试了各种各样的“解决方案”,但仍然没有任何解决方案

我真的需要你的帮助

<> >强> C++ DLL项目有以下内容:>/P>
//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
/*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;

C VisualStudio(2015)项目包含以下内容:

//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
/*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()并且工作正常,因此此函数没有错误*/
#包括“dllDelay.h”
#包括
#包括
#包括
#包括
内部主(空)
{
文件*write1;
//使用QueryPerformanceCounter()测量windows上的运行时间:
大整数频率;//每秒滴答数
大整数t1,t2;//滴答声
双延时;
write1=fopen(“write_difftime_1Test.txt”,“w”);
fprintf(write1,“\n睡眠:10000ms=10s”);
开始、停止时间;
时钟滴答作响;
长计数;
双i=0,v=0,j=0;
//进行10次这样的测量并将其写入文件
而((j<10)和((write1!=NULL)和((TRUE!=fseek(write1,0L,SEEK_END)))
{
QueryPerformanceFrequency(&frequency);
QueryPerformanceCounter(&t1);
时间(&开始);
//来自dll的函数
dllDelay(10000);
时间(&停止);
QueryPerformanceCounter(&t2);
//以毫秒为单位计算并打印经过的时间
elapsedTime=(t2.QuadPart-t1.QuadPart)*1000.0/frequency.QuadPart;
fprintf(write1,“\n运行时间:%lf s.timeDiff时间:%f以秒为单位\n\n”,elapsedTime/1000,difftime(停止、启动));
j++;
}
fclose(write1);
返回0;
} `

此功能旨在延迟10秒,测量经过的时间 并将其写入一个文件我已使用Sleep()而不是 dllDelay()并且工作正常,因此此函数没有错误。

但当我使用
#include“dllDelay.h”
时,我会收到3111个错误,例如:

  • 应为“{”
  • 标识符*write1在
    文件*write1;

  • 标识符时钟未从
    时钟信号中定义;

  • 声明与cmath中的“float log10(float_Xx)”(在第227行声明)不兼容,cmath是一个标准文件,其中我们使用了
    using\u CSTD log10;using\u CSTD modf;using\u CSTD pow;
  • 声明与cmath中的“double pow(double_Xx,int_Yx)”(在第22行声明)不兼容,cmath是一个标准文件,其中我们使用了
    ,使用了
  • 因此,在文件cmath中,整个文件都显示有错误
  • 还有许多其他类似的错误——这意味着这两种语言不知何故被弄乱了
我构建了dll(当然是在dll项目中),将.dll文件复制到找到exe的C项目文件夹中,我将.lib文件添加到解决方案资源管理器中,并得到了这些错误

<>我真的很需要你的帮助,我到处都找遍了,没有找到一个关于C项目中使用C++的DLL的指南或任何东西。

谢谢您的时间。

在您的C++ DLL项目中打开Project属性并定义一个预处理器符号:

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

#ifdef CPPDLL_EXPORTS
#define DLLIMPORT_EXPORT __declspec(dllexport)
#else
#define DLLIMPORT_EXPORT __declspec(dllimport)
#endif
然后在函数声明前使用定义的符号:

DLLIMPORT_EXPORT void dllDelay(DWORD dwMsec);
这具有以下效果:

在DLL项目中,定义了符号(DLLIMPORT\u EXPORT)。因此,DLLIMPORT\u EXPORT将计算为\u declspec(dllexport)。在使用DLL的C项目中,将不定义符号。因此,DLLIMPORT\u EXPORT将计算为\u declspec(DLLIMPORT)当包含头文件时。执行此操作将导入函数,并且您将能够使用它。如果不执行此操作,将导致尝试调用函数时出现链接器错误(无法解析的外部符号)

希望这有帮助


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

首先,
#include
在头文件中?在C项目中包含头文件时,这将如何工作?另外,
外部“C”是C++专用的,在C源文件中包含头文件时,不能使用它。这就是为什么你会在DLL的C文件中看到代码“>代码”。大多数教程都应该告诉你的。我还没有找到一个关于使用DLIMLPOWER和宏的从头到尾的完整教程,因为例子和C++一样,是用相同的编程语言。DLL在C++中使用。我尝试使用DLL,这是我第一次接触DLL,失望的不是FI。这是一份完整的指南。|非常感谢您的时间和耐心。我马上就去试试:)