C Runtime Library and a deep understanding of the meaning of compilation options-04

Source: Internet
Author: User
The following content is from the network and the author is not clear. Transferred from: Http://advancedcj.wordpress.com/2010/06/18/c-runtime-library-4/

(Link)

The name of the editor named cl.exe in vcpus, which has the following standards: Program Library-related options:/ml,/MLD,/mt,/MTD,/MD,/MDD. These options tell the compiler application the version of the C standard library to be used. /Ml (default option) corresponds to the standard library (libc) of the single-thread static version. lib);/MT corresponds to the standard library (libcmt. lib). At this time, the compiler will automatically define the _ Mt macro;/MD corresponding to the multi-threaded dll version (imported into msvcrt. lib, DLL is msvcrt. the compiler automatically defines two macros: _ MT and _ DLL. The option D will allow the compiler to automatically define one more _ debug macro, indicating that the debugging version of the corresponding standard library is used. Therefore,/MLD corresponds to the single-thread static standard library (libcd. lib),/MTD corresponds to the multi-threaded static standard library (libcmtd. lib),/MDD corresponds to the debugging version of the multi-threaded DLL standard library (imported to msvcrtd. lib, DLL is msvcrtd. DLL ). Although we did clearly tell the compiler application what version of the standard library we want to use during compilation, when the compiler is finished, how does the linker know who the target files are missing when it's time to start? In order to pass on lovesickness, our compiler has done something secret. There will be a special area in the target file compiled by Cl (if you are concerned about the location of the area in the file, refer to coff and PE file formats) stores information that guides the linker to work. One of them is the default library, which specifies one or more library file names, tell the linker to add them to the input file list during scanning (of course, the order is after the specified input file in the command line ). Here, we will first make a small experiment. Write a simple program and save it as main. C: /* Main. c */  
Int Main ()
{
Return   0 ;
} Use the following command to compile main. C (what? You never use command lines to compile programs? This ......) : Cl / C main. c /C tells Cl to compile only source files without links. Because/ml is the default option, the preceding command is equivalent to Cl/C/ml main. C. If there is no problem (it's the best thing to do if something goes wrong! Of course, unless your environment variables are not set, you should find the vcvars32.bat file in the bin directory of VC and run it .), A main. OBJ file will appear in the current directory, which is our lovely target file. Open it with a text editor (yes, the text editor, do not be afraid) and search for the "defaultlib" string. You will usually see something like this: "-defaultlib: libc-defaultlib: oldnames ". Aha, that's right. This is the default library information stored in the target file. Our target file clearly specifies two default libraries. One is the standard library libc of the single-threaded static version. lib (this is consistent with the/ml option), and the other is oldnames. lib (to be compatible with Microsoft's previous C/C ++ development systems ). The chain connector of vcss is link.exe. Because main. OBJ stores the default library information, you can use Link main. OBJ libc. Lib Or Link main. OBJ To generate the executable file main.exe. These two commands are equivalent. But if you use Link main. OBJ libcd. Lib The linker will give a warning: "Warning lnk4098: defaultlib" libc "conflicts with use of other libs; Use/nodefaultlib: Library ", because the default values of the standard library version you explicitly specify are different from those of the target file. Generally, ensure that the default standard library versions specified for all target files merged by the linker are consistent. Otherwise, the compiler will give the above warning, the lnk2005 and lnk1169 Link errors sometimes occur and sometimes do not. So when is this sometimes? Well, don't worry. Everything Below is exactly what you want to get to the bottom. Create a source file named mylib. C. The content is as follows: /* Mylib. c */
# Include
Void Foo ()
{
Printf ( " % S " , " I am from mylib! N " );
} Use Cl / C / MLD mylib. c Command compilation. Note that the/MLD option specifies libcd. Lib as the default standard library. Lib.exe is a VC command used to package the target file into a library, so we can use Lib / Out: My. Lib mylib. OBJ Package mylib. OBJ into a library. The output file name is my. Lib. Next, change main. C: /* Main. c */
Void Foo ();
Int Main ()
{
Foo ();
Return   0 ;
} Use Cl / C main. c Compile and then use Link main. OBJ my. Lib . This command can successfully generate main.exe without generating lnk2005 and lnk1169 Link errors. You only get a warning message: "Warning lnk4098: defaultlib" libcd "conflicts with use of other libs; Use/nodefaultlib: Library ". Based on the scan rules described above, we can analyze what the linker has done at this time. At the beginning, E, U, and D are empty sets. The linker first scans main. OBJ, add it to the E set, add the unparsed Foo to the U, add the main to the D, and because the main. the default standard library of obj is libc. lib, so it is added to the end of the current input file list. Then scan my. lib, because this is a library, it will take all the symbols in the current U (of course, a foo now) and my. all target modules in lib (of course there is only one mylib. OBJ. Result: mylib. OBJ does define Foo, so it is added to E, foo is transferred from u to D, mylib. the printf referenced by obj is added to u. Similarly, mylib. the default standard library specified by obj is libcd. lib, which is also added to the end of the current input file list (in libc. lib ). Constantly iterate on the modules of my. Lib library to match the symbols in U until u and d do not change. Obviously, now we have reached such a fixed point, so we will scan the next input file, which is libc. Lib. The linker found libc. printf. in OBJ, printf is defined, so printf moves from u to D, while printf. OBJ is added to E. All the symbols defined by OBJ are added to D, and unparsed symbols in OBJ are added to u. The linker will also set the target modules (such as crt0.obj) where some initialization operations will be used by each program and the modules they reference (such as malloc. OBJ, free. (OBJ) is automatically added to E, and U and D are updated to reflect this change. In fact, the unresolved symbols in each target module of the standard library can be defined in other modules of the library. Therefore, when the linker completes libc. Lib processing, the u must be empty. Finally, libcd. Lib is processed. Because the U is empty at this time, the linker will discard all the target modules in it to end scanning, then merge the target modules in E and output executable files.
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.