Vc6.0 compilation error solution highlights

Source: Internet
Author: User

Original post address: http://hym131690.blog.163.com/blog/static/642553162009212104926450/

I recently want to learn about network programming, and most socket programming environments are UNIX, which is inconvenient and cannot be compiled in windows. I find an article to solve this problem. Cool and cool!

Linking... server. OBJ: Error lnk2001: unresolved external symbol _ imp _ Send @ 16

Server. OBJ: Error lnk2001: unresolved external symbol _ imp _ wsagetlasterror @ 0 s

Erver. OBJ: Error lnk2001: unresolved external symbol _ imp _ Recv @ 16

Server. OBJ: Error lnk2001: unresolved external symbol _ imp _ wsacleanup @ 0

Server. OBJ: Error lnk2001: unresolved external symbol _ imp _ closesocket @ 4

Server. OBJ: Error lnk2001: unresolved external symbol _ imp _ inet_ntoa @ 4 s

Erver. OBJ: Error lnk2001: unresolved external symbol _ imp _ ntohs @ 4

Server. OBJ: Error lnk2001: unresolved external symbol _ imp _ Accept @ 12

Server. OBJ: Error lnk2001: unresolved external symbol _ imp _ listen @ 8

Server. OBJ: Error lnk2001: unresolved external symbol _ imp _ bind @ 12

Server. OBJ: Error lnk2001: unresolved external symbol _ imp _ htons @ 4

Server. OBJ: Error lnk2001: unresolved external symbol _ imp _ htonl @ 4

Server. OBJ: Error lnk2001: unresolved external symbol _ imp _ inet_addr @ 4

Server. OBJ: Error lnk2001: unresolved external symbol _ imp _ socket @ 12

Server. OBJ: Error lnk2001: unresolved external symbol _ imp _ wsastartup @ 8

Cause and solution:

The corresponding function cannot be found. The problem is that the corresponding lib library is not added to the link option, and Winsock wants to connect wsock32.lib. The specific library can be found by looking at the function name not found in lnk2001. You can use msdn or other means to obtain the name of the library where the function is located, and the link will be OK.

You can add wsock32.lib to project-> setting-> link-> Object/library modules, or add # pragma comment (Lib, "wsock32.lib") to stdafx. cpp ")

Generally, this is caused by incorrect project type selection. for example, in VC, 1. "Win32 console application" should have been selected, but "Win32 application" 2 was selected incorrectly. when the MFC class is used, the "Settings" of "project" is selected, and the MFC library is not used for compilation. correction Methods: 1. select "project"-> "Settings"-> connection. In the editing window at the bottom, find "/subsystem" and change it to "/subsystem: Console" "/subsystem: windows ". 2. in the "project"-> "Settings"-> "general" option, change to "use MFC as a shared DLL" in the MFC item ".

When using VC ++, you will often encounter a link error lnk2001, which is very annoying, because for programmers, the best mistake to change is a compilation error. Generally, when a connection error occurs, compilation is successful. There are many causes of connection errors, especially lnk2001 errors, which are often unknown. If you do not thoroughly learn and understand VC ++, it is very difficult to correct the connection error lnk2001. When learning VC ++, The lnk2001 error message for beginners is unresolved external symbol "symbol" (uncertain external "symbol "). This error message is generated if the connection program cannot find the referenced function, variable, or tag in all the libraries and target files. In general, there are two reasons for the error: first, the referenced function, the variable does not exist, the spelling is incorrect, or the usage is incorrect. Second, different versions of the Connection Library may be used. The following are possible causes of lnk2001 errors: 1. lnk2001 due to an Encoding Error. 1. mismatched program code or module definition (. Def) files can cause lnk2001. For example, if a variable "var1" is declared in the C ++ source file, but an attempt is made to access the variable "var1" in another file, this error occurs. 2. If the inline function is defined in the. cpp file, rather than being defined in the header file, the lnk2001 error will occur. 3. When a function is called, if the parameter types used do not match the type declared by the function, lnk2001 is generated. 4. lnk2001 will occur when you try to call a virtual function from the constructor or destructor of the base class. 5. Pay attention to the availability of functions and variables. Only global variables and functions are public. Static functions and static variables have the same scope of use. When attempting to access any static variables not declared in the file from outside the file, it will cause a compilation error or lnk2001. Variables declared in the function (local variables) can only be used within the scope of the function. The global constants of c ++ only have static connection performance. This is different from C. If you try to use global variables in multiple files in C ++, lnk2001 errors will also occur. One solution is to add the initialization code of the constant to the header file when necessary, and. the CPP file contains this header file. Another method is to assign a constant to the variable during use. II. lnk2001 1. if the/nod (/nodefalib Lib) option is used during compilation, the compiler writes the Runtime Library and the MFC Library required by the program to the target file module during connection, however, these libraries will not be linked to the project file unless they are explicitly included in the file. In this case, using/nod will cause the error lnk2001. 2. If no program entry is set for wwinmaincrtstartup, the "unresolved external on _ winmain @ 16" lnk2001 error message will be obtained when Unicode and MFC are used. 3. when compiling with the/MD option, since all runtime libraries are retained in the dynamic link library, the source file references "func, reference "_ imp _ FUNC" in the target file. If you try to use the static library libc. lib or libcmt. when Lib is connected, lnk2001 is generated on _ imp _ FUNC. If the/MD option is not used for compilation, msvcxx is used. lnk2001 also occurs during lib connection. 4. When compiling with the/ml option, lnk2001 occurs on _ errno if libcmt. Lib is used. 5. lnk2001 is generated when the release modal library is used for the compilation and debugging of the application. Similarly, the same problem occurs when you use the debug modal library to connect to the release application. 6. Mixed use of libraries and compilers of different versions can also cause problems, because the new library may contain symbols and instructions not available in earlier versions. 7. using inline and non-inline compilation options in different modules can lead to lnk2001. If function inline (/ob1 or/ob2) is enabled when the C ++ library is created, but function inline is disabled in the corresponding header file that describes the function (without the inline keyword ), the error message is displayed. To avoid this problem, use the inline keyword to mark the inline function in the corresponding header file. 8. Incorrect/subsystem or/entry settings can also lead to lnk2001. In fact, there are many reasons for lnk2001. The above reasons are only part of them. For Beginners, this is enough to understand for a while. However, the purpose of analyzing error causes is to avoid errors. Although the lnk2001 error is difficult, it can be avoided and solved as long as you have noticed the above problems.

1. Fatal error c1010: unexpected end of file while looking for precompiled header directive.

When searching for the pre-compiled header file path, the end of the file should not be encountered. (Generally no # include "stdafx. H ")

2. Fatal error c1083: cannot open include file: 'r ....... H': no such file or directory

You cannot open the file "r ....... H ": there is no such file or directory.

3. Error c2011: 'C ...... ': 'Class' type redefinition

Class "C ......" Redefinition.

4. Error c2018: unknown character '0xa3'

Unknown character '0xa3 '. (Generally Chinese characters or Chinese Punctuation Marks)

5. Error c2057: Expected constant expression

It is expected to be a constant expression. (Usually in the case Branch of the switch statement)

6. Error c2065: 'idd _ mydialog ': Undeclared identifier

"Idd_mydialog": Undeclared identifier.

7. Error c2082: redefinition of formal parameter 'breset'

The function parameter "breset" is redefined in the function body.

8. Error c2143: syntax error: Missing ': 'before '{'

Syntax error: ";" is missing before "{".

9. Error c2146: syntax error: Missing '; 'before identifier' DC'

Syntax error: ";" is lost before "DC".

10. Error c2196: Case value '69' already used

The Value 69 has been used. (Usually in the case Branch of the switch statement)

Lnk2005 errors are often encountered in programming-repeated definition errors. In fact, lnk2005 errors are not very difficult to solve. Find out the cause of its formation, and you can easily solve it. The lnk2005 error may occur in the following situations: 1. Define global variables repeatedly. There may be two situations: A. For some programmers who are new to programming, they sometimes think that they can use definitions to declare where they need to use global variables. In fact, this is wrong. global variables are for the entire project. The correct definition should be defined in a CPP file as follows: int g_test; in the CPP file in use, use: extern int g_test. If you still use int g_test, the lnk2005 error will be generated. The error message is similar to AAA. OBJ error lnk2005 int book C? Book @ 3ha already defined in BBB. obj. Remember that you cannot assign values to variables. Otherwise, the lnk2005 error will still occur. What we need here is a "Declaration", not a "Definition "! According to the C ++ standard, a variable is declared and must satisfy both conditions at the same time; otherwise, it is defined: (1) the declaration must use the extern keyword; (2) the initial value cannot be assigned to the variable. The following declaration is: extern int A; the following defines int A; int A = 0; extern int A = 0; b. For programmers who are not so rigorous in programming, they always define a global variable in files that require variables, and do not consider the variable name, this often leads to repeated variable names and lnk2005 errors. 2. Duplicate header files. The header files that need to be included include definitions of variables, functions, and classes, and must be included multiple times in other places, if there are no related Macros in the header file and other measures to prevent duplicate links, the lnk2005 error will occur. The solution is to perform similar processing in the header file to be included: # ifndef my_h_file // if this macro is not defined # define my_h_file // define this macro ....... // Header file body content ....... # Endif is implemented using macros. You can also use pre-compilation to add the following content to the header file: # pragma once // header file subject 3. This is caused by the use of third-party libraries. This situation is mainly caused by library conflicts between the function library and the MFC in the C runtime. The specific method is to put the database that prompts an error in front of another database. In addition, selecting different C function libraries may cause this error. Microsoft and C have two types of runtime function libraries: libc. Lib, which does not support multithreading. The other is multi-threaded: msvcrt. Lib. This error may occur if these two function libraries are used together in a project. In general, it requires the library of MFC to be linked before the function library of C runtime, we recommend that you use msvcrt that supports multiple threads. lib. Therefore, before using a third-party library, you must first know which library it is linked to. Otherwise, the lnk2005 error may occur. If you have to use a third-party library, you can try to modify it as described below, but it cannot be guaranteed to solve the problem. The first two methods are provided by Microsoft: a. Select VC menu project-> Settings-> link-> catagory and select input. Then, enter the library you want to ignore in the edit column of ignore libraries, for example, nafxcwd. LIB; libcmtd. lib. Then fill in the correct library order in the edit column of Object/library modules. here you need to be able to determine what is the correct order! B. Select the VC menu project-> Settings-> link page, and enter/verbose: Lib in the edit column of project options, in this way, you can see the link sequence in the output window during the compilation of the link program. C. Select the VC menu project-> Settings-> C/C ++ page, select code generation for catagory, and then select other libraries such as multithread DLL in user runtime libraray to try them one by one. With regard to the relevant processing process of the compiler, refer to: http://www.donews.net/xzwenlan/archive/2004/12/23/211668.aspx this is the lnk2005 error I have encountered several situations, certainly there are other situations may also cause this error, so I don't want you to think about how to troubleshoot the lnk2005 error after reading this article. The process of programming is a process of thinking, so you can start your mind more and get more!

Solution 2,

Error prompt during lnk2005...... already defined... error link:

Libcmt. lib (crt0dat. OBJ): Error lnk2005: _ amsg_exit already defined in msvcrtd. lib (msvcr80d. DLL) libcmt. lib (crt0dat. OBJ): Error lnk2005: _ initterm_e already defined in msvcrtd. lib (msvcr80d. DLL) libcmt. lib (crt0dat. OBJ): Error lnk2005: _ exit already defined in msvcrtd. lib (msvcr80d. DLL) libcmt. lib (crt0dat. OBJ): Error lnk2005: _ exit already defined in msvcrtd. lib (msvcr80d. DLL) libcmt. lib (crt0dat. OBJ): Error lnk2005: _ cexit already defined in msvcrtd. lib (msvcr80d. DLL) libcmt. lib (fflush. OBJ): Error lnk2005: _ fflush already defined in msvcrtd. lib (msvcr80d. DLL) libcmt. lib (setlocal. OBJ): Error lnk2005: _ configthreadlocale already defined in msvcrtd. lib (msvcr80d. DLL) libcmt. lib (tidtable. OBJ): Error lnk2005: _ encode_pointer already defined in msvcrtd. lib (msvcr80d. DLL) libcmt. lib (tidtable. OBJ): Error lnk2005: _ decode_pointer already defined in msvcrtd. lib (msvcr80d. DLL) libcmt. lib (invarg. OBJ): Error lnk2005: _ invoke_watson already defined in msvcrtd. lib (msvcr80d. DLL) libcmt. lib (realloc. OBJ): Error lnk2005: _ realloc already defined in msvcrtd. lib (msvcr80d. DLL) libcmt. lib (crt0init. OBJ): Error lnk2005: ___ xi_a already defined in msvcrtd. lib (cinitexe. OBJ) libcmt. lib (crt0init. OBJ): Error lnk2005: ___ xi_z already defined in msvcrtd. lib (cinitexe. OBJ) libcmt. lib (crt0init. OBJ): Error lnk2005: ___ xc_a already defined in msvcrtd. lib (cinitexe. OBJ) libcmt. lib (crt0init. OBJ): Error lnk2005: ___ xc_z already defined in msvcrtd. lib (cinitexe. OBJ) libcmt. lib (hooks. OBJ): Error lnk2005: "Void _ cdecl terminate (void )"(? Terminate @ yaxxz) already defined in msvcrtd. lib (msvcr80d. DLL) libcmt. lib (winxfltr. OBJ): Error lnk2005: _ xcptfilter already defined in msvcrtd. lib (msvcr80d. DLL) libcmt. lib (mlock. OBJ): Error lnk2005: _ unlock already defined in msvcrtd. lib (msvcr80d. DLL) libcmt. lib (mlock. OBJ): Error lnk2005: _ Lock already defined in msvcrtd. lib (msvcr80d. DLL) libcmt. lib (dosmap. OBJ): Error lnk2005: _ errno already defined in msvcrtd. lib (msvcr80d. DLL) libcmt. lib (crt0.obj): Error lnk2005: _ maincrtstartup already defined in msvcrtd. lib (crtexe. OBJ) libcmt. lib (errmode. OBJ): Error lnk2005: ___ set_app_type already defined in msvcrtd. lib (msvcr80d. DLL) libcmt. lib (isctype. OBJ): Error lnk2005: _ isctype already defined in msvcrtd. lib (msvcr80d. DLL) libcmt. lib (tolower. OBJ): Error lnk2005: _ tolower already defined in msvcrtd. lib (msvcr80d. DLL)

Solution: Project Properties, linker, input, and ignore specific library on the right. Enter "msvcrtd. lib" and click OK.

Solution 3: Error lnk2005: _ dllmain @ 12 already defined in audio. OBJ

An error occurred while compiling the link in VC ++ 6.0: mfcs42d. Lib (dllmodul. OBJ): Error lnk2005: _ dllmain @ 12 already defined in audio. OBJ

Symptom when the link sequence between the C Runtime (CRT) Library and Microsoft basic class (MFC) Library is incorrect, one of the following lnk2005 errors may occur: nafxcwd. lib (afxmem. OBJ): Error lnk2005: "Void * _ cdecl operator new (unsigned INT )"(?? 2 @ yapaxi @ Z) already defined in libcmtd. lib (New. OBJ) nafxcwd. lib (afxmem. OBJ): Error lnk2005: "Void _ cdecl operator Delete (void *)"(?? 3 @ yaxpax @ Z) already defined in libcmtd. lib (dbgnew. OBJ) nafxcwd. lib (afxmem. OBJ): Error lnk2005: "Void * _ cdecl operator new (unsigned int, Int, char const *, INT )"(?? 2 @ yapaxihpbdh @ Z) already defined in libcmtd. lib (dbgnew. OBJ) mfcs40d. lib (dllmodul. OBJ): Error lnk2005: _ dllmain @ 12 already defined in msvcrtd. lib (dllmain. OBJ) mfcs42d. lib (dllmodul. OBJ): Error lnk2005: _ dllmain @ 12 already defined in msvcrtd. lib (dllmain. OBJ)

The reason is that the CRT library uses weak external links for new, delete, and dllmain functions. The MFC Library also contains the new, delete, and dllmain functions. These functions need to be linked to the MFC Library first, and then to the CRT library.

Solution 1: force the linker to connect to the database in the correct order. 1. Click set in the project menu ". 2. In the "settings of the following projects" view in the "Project Settings" dialog box, click to select the project configuration with a link error. 3. On the "Link" tab, click to select "input" in the "category" combo box ". 4. In the ignore library box, insert the library name (for example, nafxcwd. Lib; libcmtd. Lib ).

Note: The equivalent linker command line is:/nod: <library name>. 5. In the "Object/Library module" box, insert the database name. Make sure that these libraries are listed in sequence and are the first two libraries in the row (for example, nafxcwd. lib and libcmtd. Lib ). To set this option in Visual C ++. net, read the online help topic "Set Visual C ++ Project Properties.

Solution 2: Find and correct the problematic module. To view the link sequence of the current library, follow these steps: 1. Click set in the project menu ". 2. In the "settings of the following projects" view in the "Project Settings" dialog box, click to select the project configuration with a link error. 3. On the "Link" tab, type/verbose: Lib in the "Project Options" box. 4. regenerate the project. These libraries are listed in the output window during the link process.

This is caused by design.

When using the MFC library for more information, you must first link them and then link the CRT library. This can be done by ensuring that each file in the project contains msdev/mfc/include/afx. h first. You can either directly include (# include <afx. h>) or indirectly include (# include <stdafx. h>. The afx. h file contains the correct database order by using the # pragma comment (Lib, "<libname>") command.

If the source file extension is. c, or the file extension is. CPP but does not use MFC, you can create a small header file (forcelib. h) and place it at the top of the module. This new header file ensures that the database is searched in the correct order.

Visual c ++ does not contain this header file. To create this file, follow these steps: 1. Open msdev/mfc/include/afx. h. 2. Select # ifndef _ afx_noforce_libs and # endif //! The rows between _ afx_noforce_libs. 3. Copy the selected part to the Windows clipboard. 4. Create a new text file. 5. paste the content of the clipboard to the new file. 6. Save the file as msdev/mfc/include/forcelib. h.

Step 1. Start Microsoft Visual Studio. NET. 2. on the "file" menu, point to "new" and click "project ". 3. Click "Visual C ++ project" under "project type", and then click "MFC Application" under "template ". 4. In the "name" text box, type q148652. 5. In the "location" text box, type C:/test and click "OK ". 6. In the "MFC Application Wizard" dialog box, click "application type ". 7. Click "based on dialog box" under "application type", and then click "use MFC in static library" under "use of MFC ". 8. Click Finish ". 9. In Solution Explorer, select all three. cpp files under the source file. 10. Right-click the three selected files and click Delete ". 11. Right-click "source file", point to "add", and click "Add new project ". 12. Click "C ++ file" under "template ". In the "name" text box, type AA. Click Open ". 13. paste the following code to AA. in CPP: int test () {New int; return 1 ;}14. right-click "source file", point to "add", and click "add existing item ". 15. Select the following file: & #8226; q148652.cpp & #8226; q148652dlg. cpp & #8226; stdafx. cpp 16. Click Open ". 17. The file you selected in step 1 appears under "Source File. 18. Select all four. cpp files under "source files. 19. Right-click the selected four. cpp files and click "properties ". 20. Expand "configuration attributes" and then "C/C ++ ". 21. Click pre-compile header ". 22. Set the "Create/use precompiled header" attribute to "do not use precompiled header ". Click OK ". 23. On the "generate" menu, click "regenerate SOLUTION ".

My Aladdin has adopted the solution: solution 1 in solution 3: force the linker to link to the database in the correct order 1. on the "project" menu, click "set ". 2. In the "settings of the following projects" view in the "Project Settings" dialog box, click to select the project configuration with a link error. 3. On the "Link" tab, click to select "input" in the "category" combo box ". 4. In the ignore library box, insert the library name (for example, nafxcwd. Lib; libcmtd. Lib ). Note: The equivalent linker command line is:/nod: <library name>. 5. In the "Object/Library module" box, insert the database name. Make sure that these libraries are listed in sequence and are the first two libraries in the row (for example, nafxcwd. lib and libcmtd. Lib ).

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.