Implementation and error debugging of a simple non-MFC DLL

Source: Internet
Author: User

When I first started learning DLL, I first read a DLL program:

First write a DLL header file:
// Lib. h
# Ifndef lib_h
# Define lib_h

Extern "C" int _ declspec (dllexport) add (int x, int y );

# Endif

Write another DLL source file:

# Include "Lib. H"

Int add (int x, int y)
{
Return X + Y;
}

Generate the Lib. dll file.

Then write a function to call it:

# Include <stdio. h>

# Include <windows. h>

Typedef int (* lpaddfun) (INT, INT); // macro-Defined Function Type

Int main (INT argc, char * argv [])
{
Hinstance hdll; // DLL handle
Lpaddfun addfun; // function pointer
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;
}

The following error occurs during running:
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.

 

 

Suggestions from others are as follows:

 

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

Error lnk2001: unresolved external symbol _ winmain @ 16 in Vc solution

Recommendation
I. Problem Description

Error lnk2001: unresolved external symbol _ winmain @ 16
Debug/main.exe: Fatal error lnk 1120:1 unresolved externals
Error executing link.exe;
Ii. Possible causes

1. You have created a console program with VC. Its entry function should be main, and you have used winmain.

2. You opened a. c/. cpp file with VC, and then compiled the file directly. This file uses winmian instead of main as the entry function. The default setting of VC is for console programs.
3. Solution

1. Go to project> setting> C/C ++, select Preprocessor from category, delete _ console from processor definitions, and add _ WINDOWS

2. Go to project-> setting-> link and change/subsystem: console to/subsystem: Windows in project options.

3. Save the settings and rebuild all.

For settings in vs2005, refer to corresponding settings.

Iv. settings in vs2005

1. Select project> properties from the menu to bring up the property pages window.

2. select Configuration properties-> C/C ++-> Preprocessor from the left-side Navigation Pane, delete _ console from the Preprocessor definitions item on the right-side bar, and add _ windows.

3. Select Configuration Properties> linker> system from the left-side Navigation Pane, and change the item corresponding to subsystem in the right-side Navigation Pane to Windows (/subsystem: Windows)

4. Rebuild all. OK?

Test: (Environment: vs2005, programming language VC)

1. Write the following code in a text editor:

// Test. c
# I nclude <windows. h>

Int apientry winmain (hinstance,
Hinstance hprevinstance,
Lpstr lpcmdline,
Int ncmdshow)
{
MessageBox (null, "Hello! "," Title ", mb_ OK );
}

Assume that the file package is stored as test. C.

2. Use vs2005 to create a Win32 console application. Note that in application settings, set it to empty project. Then add test. C to the project.

3. The project created in vs2005 supports Unicode by default. I don't need this, so select project-> properties in the menu to bring up the property pages window. Select Configuration Properties> general in the left sidebar, and change the character set in the right sidebar to use multi-byte character set.

4. Compile with the following error:
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. Follow the setup method in vs2005 and compile again. The error disappears!

I have been doing this for a long time, but it is still correct,

However, this is the opposite of the above suggestions.
1. Go to project-> setting-> C/C ++, select Preprocessor in category, change _ WINDOWS to _ console in processor definitions, and click OK,

Reopen

2. Go to project-> setting-> link and change/subsystem: Windows to/subsystem: console in project options.

I don't know why. Here I am writing a simple non-MFC DLL in the C language format.

Thank you for your prompt!

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.