Windows Programming (6) Change entry, simple link Error Analysis

Source: Internet
Author: User

From scratch, learn windows programming (6) -- change entry, simple analysis of link errors
Or the hello. c program. Let's modify it to start today's topic.


1 # include <stdio. h>
2
3 int myentry ()
4 {
5 printf ("hello world ");
6 return 0;
7} as you can see, I replaced the original location of main with myentry. What will happen?

D: est> cl/c hello. c
Microsoft (R) 32-bit C/C ++ Optimizing Compiler Version 12.00.8804 for 80x86
Copyright (C) Microsoft Corp 1984-1998. All rights reserved.

Hello. c

OK. No problem. hello. obj is generated.

D: est> link hello. obj
Microsoft (R) Incremental Linker Version 6.00.8447
Copyright (C) Microsoft Corp 1992-1998. All rights reserved.

LIBC. lib (crt0.obj): error LNK2001: unresolved external symbol _ main
Hello.exe: fatal error LNK1120: 1 unresolved externals

An error occurred during the link process and a LINK2001 error was reported. This error is said on MSDN:

Unresolved external symbol "symbol"

Code references something (such as a function, variable, or label) that the linker cant find in the libraries and object files.

Possible causes

What the code asks for doesnt exist (the symbol is spelled incorrectly or uses the wrong case, for example ).
The code asks for the wrong thing (you are using mixed versions of the libraries, some from one version of the product, others from another version ).
This error message is followed by fatal error LNK1120.

The specific cause can be easily inferred based on the previous knowledge. If you are interested, you can try reasoning first and then look at the following analysis process. This gives you a deeper understanding.

LINK errors are a class of errors that are unique to C/C ++ and difficult to understand and solve. The C language is better. In C ++, LINK errors often contain "garbled" characters, which makes it hard for the first-time contact of children's shoes. Let's take a look at this small example.

First, we use cl from hello. c generates hello. obj file, because the/Zl option is not added, the generated hello. the obj file still contains two defaultlib files, one of which is libc. lib, and an oldnames. lib. In this way, the hello. obj and libc. lib, and libc. lib is formed by the combination of many obj, which contains the crt0.obj file generated by crt0.c. Recall the source code file of crt0.c we saw last time. The function in it is mainCRTStartup, and the main function is called in the mainCRTStartup function. The external symbol of the main function is not found (other functions such as _ heap_init... So an LNK2001 error is reported, telling the user that the main symbol is not found and you forget it.

Related extensions

Use the/entry option to select an entry for link

D: est> cl/c hello. c
Microsoft (R) 32-bit C/C ++ Optimizing Compiler Version 12.00.8804 for 80x86
Copyright (C) Microsoft Corp 1984-1998. All rights reserved.

Hello. c

D: est> link/entry: myentry hello. obj
Microsoft (R) Incremental Linker Version 6.00.8447
Copyright (C) Microsoft Corp 1992-1998. All rights reserved.

LIBC. lib (crt0.obj): error LNK2001: unresolved external symbol _ main
Hello.exe: fatal error LNK1120: 1 unresolved externals

Others remain unchanged. Add the/entry option during link, and the result is still the same as the original one. Therefore, it can be determined that the link process needs to locate all the symbols, unlike the compilation process, which can be "eye-catching". Even some external can be ignored. In addition, whether the function is "used" or not, it must be found by the link program. Here "use" means to execute it during execution. Let's look at another example:


1 # include <stdio. h> 2 3 int myentry () 4 {5 printf ("hello world"); 6 return 0; 7} 8 9 int main () 10 {11 myentry (); 12} 13 14 int test () 15 {16 nofunc (); 17 return 0; 18} as shown in the code above, when the link is compiled by default, the test function is not called, and the nofunc function is not defined. Let's compile the link.

D: est> cl/c hello. c
Microsoft (R) 32-bit C/C ++ Optimizing Compiler Version 12.00.8804 for 80x86
Copyright (C) Microsoft Corp 1984-1998. All rights reserved.

Hello. c

D: est> link hello. obj
Microsoft (R) Incremental Linker Version 6.00.8447
Copyright (C) Microsoft Corp 1992-1998. All rights reserved.

Hello. obj: error LNK2001: unresolved external symbol _ nofunc
Hello.exe: fatal error LNK1120: 1 unresolved externals

We can see that the nofunc symbol cannot be found during link, thus an error is reported.

This is also a troublesome part of C/C ++, which involves the division of compilation, linking, and runtime. This is clear, but there are many concepts, if some things are hidden in addition to their implementation and principles, it will be even harder to understand.

Use the/Zl Option

If you use the/Zl option for myentry code.

D: est> cl/c/Zl hello. c
Microsoft (R) 32-bit C/C ++ Optimizing Compiler Version 15.00.21022.08 for 80x86
Copyright (C) Microsoft Corporation. All rights reserved.

Hello. c

D: est> link hello. obj
Microsoft (R) Incremental Linker Version 9.00.21022.08
Copyright (C) Microsoft Corporation. All rights reserved.

LINK: fatal error LNK1561: entry point must be defined

In this case, the LNK1561 error occurs. The corresponding explanations on MSDN are as follows:

Entry point must be defined

The linker did not find an entry point. you may have intended to link as a DLL, in which case you shoshould link with the/DLL option. you may have also forgotten to specify the name of the entry point; link with the/ENTRY option.

Otherwise, you shoshould include a main, wmain, WinMain, or wMain function in your code.

If you using LIB and intend to build a. dll, one reason for this error is that you supplied a. def file. If so, remove the. def file from the build.

Because/Zl removes defaultlib, link is hello directly. obj instead of connecting to libc. lib, because the default entry symbol is mainCRTStartup, there is no libc. lib, And the mainCRTStartup in it does not exist. Therefore, an error message indicating no entry point is displayed.

Way1:

According to the description above on MSDN, how can we use the main function, compile/Zl, and link?

First, change myentry to main. After:

D: est> cl/c/Zl hello. c
Microsoft (R) 32-bit C/C ++ Optimizing Compiler Version 12.00.8804 for 80x86
Copyright (C) Microsoft Corp 1984-1998. All rights reserved.

Hello. c

D: est> link hello. obj
Microsoft (R) Incremental Linker Version 6.00.8447
Copyright (C) Microsoft Corp 1992-1998. All rights reserved.

Hello. obj: error LNK2001: unresolved external symbol _ printf
LINK: error LNK2001: unresolved external symbol _ mainCRTStartup
Hello.exe: fatal error LNK1120: 2 unresolved externals

At this time, the error is different. Here, the link between printf and mainCRTStartup cannot be found.

Way2:

Specify the/entry option as myentry.

D: est> cl/c/Zl hello. c
Microsoft (R) 32-bit C/C ++ Optimizing Compiler Version 12.00.8804 for 80x86
Copyright (C) Microsoft Corp 1984-1998. All rights reserved.

Hello. c

D: est> link/entry: myentry hello. obj
Microsoft (R) Incremental Linker Version 6.00.8447
Copyright (C) Microsoft Corp 1992-1998. All rights reserved.

LINK: fatal error LNK1221: a subsystem cant be inferred and must be defined

A new error occurs, LNK1221. Let's take a look at the explanation:

A subsystem can't be inferred and must be defined

The linker does not have enough information to infer which subsystem you will target your application.

To fix this error, use the/SUBSYSTEM option.

It seems that during link, we can also automatically judge the subsystem of the compiled target application based on the searched symbol and defined entry. About the subsystem, it seems that only the compilation option is available under windows, let's take a look.

D: est> link/entry: myentry/subsystem: console hello. obj
Microsoft (R) Incremental Linker Version 6.00.8447
Copyright (C) Microsoft Corp 1992-1998. All rights reserved.

Hello. obj: error LNK2001: unresolved external symbol _ printf
Hello.exe: fatal error LNK1120: 1 unresolved externals

OK. When it is added, the LNK2001 error also occurs, which is less than the above _ mainCRTStartup and requires a _ printf.

Way3:

Combine the above two methods to use way1 and way2 together, use the main function to replace the source code of myentry, and then set the main entry point. <

Related Article

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.