一个简单非MFC dll的实现及错误调试

来源:互联网 发布:上海java培训机构 编辑:程序博客网 时间:2024/05/16 14:28

 刚开始学习DLL,初看一个dll程序:

先写一个DLL头文件:
// lib.h
#ifndef LIB_H
#define LIB_H

extern "C" int _declspec (dllexport) add( int x, int y );

#endif

再写一个DLL源文件:

#include "lib.h"

int add( int x, int y )
{
return x+y;
}

把它生成了lib.dll文件

然后写一个函数来调用:

#include < stdio.h >

#include < windows.h >

typedef int ( *lpAddFun )( int, int );//宏定义函数类型

int main( int argc, char *argv[] )
{
HINSTANCE hDll; //dll句柄
lpAddFun addFun; //函数指针
hDll = LoadLibrary( "..//Debug//dllnotmfc.dll" );
if( hDll != NULL)
{
addFun = ( lpAddFun )GetProcAddress( hDll, "add" );
if( addFun != NULL )
{
int result = addFun( 2, 3 );
printf( "%d", result );
}
FreeLibrary( hDll );
}
return 0;
}

运行此时出现了如下错误:
Linking...
LIBCD.lib(wincrt0.obj) : error LNK2001: unresolved external symbol _WinMain@16
Debug/dllCALL.exe : fatal error LNK1120: 1 unresolved externals
Error executing link.exe.

 

 

别人给我的建议是这样:

 

http://blog.csai.cn/user1/16781/archives/2006/6412.html

vc中error LNK2001:unresolved external symbol _WinMain@16的解决方法


推荐
一,问题描述

error LNK2001: unresolved external symbol _WinMain@16
debug/main.exe:fatal error LNK 1120:1 unresolved externals
error executing link.exe;
二,产生这个问题可能的原因

1, 你用vc建了一个控制台程序,它的入口函数应该是main, 而你使用了WinMain.

2.  你用vc打开了一个.c/.cpp 文件,然后直接编译这个文件,这个文件中使用了WinMian而不是main作为入口函数。vc这时的默认设置是针对控制台程序的。
三, 解决方法

1.进入project->setting->c/c++, 在category中选择preprocessor,在processor definitions中删除_CONSOLE, 添加_WINDOWS

2.进入project->setting->Link, 在Project options中将 /subsystem:console改为/subsystem:windows.

3.保存设置,Rebuild All.

VS2005中的设置请参考对应项进行设置

四,VS2005中的设置

1.菜单中选择 Project->Properties, 弹出Property Pages窗口

2.在左边栏中依次选择:Configuration Properties->C/C++->Preprocessor,然后在右边栏的Preprocessor Definitions对应的项中删除_CONSOLE, 添加_WINDOWS.

3.在左边栏中依次选择:Configuration Properties->Linker->System,然后在右边栏的SubSystem对应的项改为Windows(/SUBSYSTEM:WINDOWS)

4.Rebuild All. Ok ?

测试:(环境:vs2005,编程语言vc)

1.用文本编辑器编写如下代码:

// test.c
#i nclude <windows.h>

int APIENTRY WinMain(HINSTANCE hInstance,
                    HINSTANCE hPrevInstance,
                    LPSTR    lpCmdLine,
                    int      nCmdShow)
{
MessageBox(NULL, "Hello!", "title", MB_OK);
}

假设把文件包存为test.c.

2.用vs2005建一个Win32 Console Application, 注意在Application Settings设置为Empty project. 然后把test.c添加到工程中去。

3.vs2005建立的工程默认是支持UNICODE的,我不用这个,所以在菜单中选择 Project->Properties, 弹出Property Pages窗口。在左边栏中依次选择:Configuration Properties->General,然后把右边栏的Character Set 改为Use Multi-Byte Character Set.

4.编译,出现如下错误:
MSVCRTD.lib(crtexe.obj) : error LNK2019: unresolved external symbol _main referenced in function ___tmainCRTStartup
C:/test/Debug/test.exe : fatal error LNK1120: 1 unresolved externals

5.按照上述VS2005中的设置方法进行设置,然后再编译,错误消失了吧!

对于这个问题,我弄了好长时间,但还是调对了,

不过和上面的建议正好所做的正好相反,是把
1.进入project->setting->c/c++, 在category中选择preprocessor,在processor definitions中_WINDOWS 改成了_CONSOLE, 然后单击OK,

重新打开

2.进入project->setting->Link, 在Project options中将 /subsystem:windows改为/subsystem:console.


我也不知道这是为什么,这里我是在写一个简单的非MFC dll用的是C语言格式写的。

在这里还是谢谢他们的提示!

 

原创粉丝点击