我对使用 dll 和链接各种文件非常陌生。
我只知道在同一个 .c 文件中编写 main() 函数和所有其他函数并运行它。
我有一个用于模式匹配的程序。它获取字符串并检查它是否存在于整个文本字符串中。喜欢
文本字符串:我叫约翰
要匹配的字符串:姓名
回答:是的
主要功能是这样的:
int main(int argc, const char *argv[])
{
char target[200];
char *ch = target;
char pattern[20];
int i,k,count,l;
printf("\nEnter the string: \n");
fgets(target,100,stdin);
printf("Enter the string to be matched: \n");
fgets(pattern,20,stdin);
l=strlen(pattern);
i = kmp(target, strlen(target)-1, pattern, strlen(pattern)-1);
//printf("I is : %d\n",i);
if (i == -1)
puts("False");
else
puts("True");
getch();
return 0;
}
调用函数 kmp() 并返回结果。我们还可以在 kmp() 函数中打印结果。 kmp()函数如下:
int kmp(char *target, int tsize, char *pattern, int psize)
{
int i;
int *pi = compute_prefix_function(pattern, psize);
int k = -1;
if (!pi)
return -1;
for (i = 0; i < tsize; i++) {
while (k > -1 && pattern[k+1] != target[i])
k = pi[k];
if (target[i] == pattern[k+1])
k++;
if (k == psize - 1) {
free(pi);
return i-k;
}
}
free(pi);
return -1;
}
在kmp中我们调用compute_prefix_function(pattern, psize);如下:
int *compute_prefix_function(char *pattern, int psize)
{
int k = -1;
int i = 1;
int *pi = malloc(sizeof(int)*psize);
if (!pi)
return NULL;
pi[0] = k;
for (i = 1; i < psize; i++) {
while (k > -1 && pattern[k+1] != pattern[i])
k = pi[k];
if (pattern[i] == pattern[k+1])
k++;
pi[i] = k;
}
return pi;
}
需要调用的头文件:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
我想做的是:
以 dll/共享库格式创建实现。本质上,dll 应该有一个函数扩展,它接受一个字符串并返回一个 bool 值,说明该字符串是否存在。
对于哪个函数我需要放入.c文件和头文件以及如何为此创建.dll文件?
我正在使用 Windows 7、VS 2010 和 C 编程。
请逐步解释。
最佳答案
我会在后面详细介绍 DLL,但首先,这里是您需要执行此操作的源文件的布局。
您需要三个文件:
- 主程序
- kmp.h
- kmp.c.
代码结构:
文件main.c
#include <stdio.h>
#include "kmp.h" // this will make the kmp() function known to main()
int main(int argc, const char *argv[])
{
char target[200];
... same code as you aready have
}
文件kmp.h
// prototype to make kmp() function known to external programs (via #include)
extern int kmp(char *target, int tsize, char *pattern, int psize);
文件kmp.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// declare kmp prototype as DLL-export
_declspec(dllexport) int kmp(char *target, int tsize, char *pattern, int psize);
// prototype for internal helper function
static int *compute_prefix_function(char *pattern, int psize);
//
// implementation of kmp() function (and helper)
//
int kmp(char *target, int tsize, char *pattern, int psize)
{
int i;
... same program code as you aready have
}
int *compute_prefix_function(char *pattern, int psize)
{
int k = -1;
... same program code as you aready have
}
.
现在,作为第一步,您可以制作这三个文件,并在您当前的项目中编译它们(即将您当前的项目源代码拆分为这三个文件,只需在 kmp 中省略 __declspec(dllexport) 的行并像以前一样编译(非 DLL)以查看是否一切正常。
.
然后您需要为 kmp.h 和 kmp.c 创建一个 DLL 项目(这将编译一个 KMP.DLL 和 KMP.LIB)。然后您使用 main.c 创建一个普通程序(如您当前的示例)并需要将其与 KMP.LIB/KMP.DLL 链接
下面可能有点模糊,因为我这里只有VS2005,但是创建DLL工程的步骤基本上应该是这样的:
- 新项目:键入 Win32/Win32-Project
- 姓名 KMP
- 在向导中选择 Type DLL 并勾选“Empty Project”
- 添加您的 kmp.c 和 kmp.h 文件
在您的主项目(带有 main.c 程序的项目)中,您可以执行
- 文件菜单 > 添加 > 现有项目 > KMP.vcproj
这将自动构建 DLL 并将其链接到您的 main.c 程序项目。
关于c - 如何将 main 函数与头文件链接起来并创建它的 dll 文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17040920/