gpt4 book ai didi

c++ - 如何调用用c++编写的程序调用我用c编写的DLL

转载 作者:太空宇宙 更新时间:2023-11-04 02:04:33 25 4
gpt4 key购买 nike

我用 C 开发了一个 dll,我用这个方法调用了我的 C++ 程序

#include <cstdlib>
#include <iostream>
#include <windows.h>

#ifdef __cplusplus
extern "C" {
#endif
#include "mydll.h"
#ifdef __cplusplus
}
#endif
using namespace std;

int main(int argc, char *argv[])
{
struct myown_struct *ss = NULL;
int i = 0,j = 0, result = 0;
struct myown_struct2 *s = NULL;
myfunction("string1", "string2", "string3", 1500);
system("PAUSE");
return EXIT_SUCCESS;
}

mydll.h :

#ifndef _DLL_H_
#define _DLL_H_

#if BUILDING_DLL
# define DLLIMPORT __declspec (dllexport)
#else /* Not BUILDING_DLL */
# define DLLIMPORT __declspec (dllimport)
#endif /* Not BUILDING_DLL */

struct myown_struct {
int fd;
char buf[32];
unsigned int len;
} __attribute__((packed));

DLLIMPORT void myfunction(char *s1, char* s2, char *s3, int n);

#endif /* _DLL_H_ */

我收到这个错误:

C:\Users\user01\Desktop\test\main.cpp 在函数int main(int, char**) C:\Users\user01\Desktop\test\main.cpp myfunction' undeclared (先用这个函数)

我该如何解决?

最佳答案

您的头文件 mydll.h 似乎缺少 myfunction 的声明。更正该遗漏,您的代码将编译。

除此之外,您还需要确保:

  • 编译 DLL 时创建的 .lib 导入库会在链接 C++ 程序时传递给链接器。
  • 当您尝试运行您的程序时,可以找到 DLL。最好将 DLL 放在与可执行文件相同的目录中。

我还要评论说,我希望在头文件中看到 extern "C",而不是强制头文件的每个用户都编写 extern "C"。头文件确实应该独立存在。

关于c++ - 如何调用用c++编写的程序调用我用c编写的DLL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22349654/

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