Notes when using VC to link static libraries

Source: Internet
Author: User

The VC project must be consistent with the linked static library (*. Lib) file:

For example: Does the Shared Mode use mfc dll or the static mode use mfc dll?Is it multithreading or a single thread? Is debug mode or release mode? Project property configuration involved /MDD And /MTD /MD /MtIf they are inconsistent, an error will be reported during the link stage, such as libcmt. lib (invarg. OBJ): Error lnk2005: _ initp_misc_invarg is already in libcmtd. lib (invarg. object) definition or function cannot be parsed. (zt) 1. VC compilation optionsMulti-thread (/mt) multi-thread debugging (/MTD) multi-thread DLL (/MD)Multi-thread debugging DLL (/MDD)

 

2. C Runtime Library          Library filesSingle thread (static link) ml     Libc. LibDebug single thread (static link) MLD   Libcd. LibMultithread (static link) Mt     Libcmt. LibDebug multithread (static link) MTD     Libcmtd. LibMultithread (Dynamic Link) MD     Msvcrt. LibDebug multithread (Dynamic Link) mddmsvcrtd. Lib

 

3. Differences between various C Runtime Libraries

 

(1) The Single-threaded library with static links can only be used for single-threaded applications. The target code of the C Runtime Library is eventually compiled in the binary file of the application. You can use the/ml compilation option to set a single-threaded library that uses static links for Visual C ++.

 

(2) The target code of the static multi-threaded library with static links is finally compiled in the binary file of the application, but it can be used in the multi-threaded program. You can use the/MT compilation option to set a multi-threaded library that uses static links in Visual C ++.

 

(3) dynamically linked runtime libraries save all C library functions in a separate Dynamic Linked Library msvcrtxx. dll. msvcrtxx. dll handles multithreading issues. You can use the/MD compilation option to set the Runtime Library that uses dynamic links in Visual C ++. The/MDD,/MLD, or/MTD options use the debug Runtime Library (the runtime function library of the debug version), which corresponds to/MD,/ml, or/MT respectively. The Runtime Library of the debug version contains debugging information and uses some protection mechanisms to help identify errors and enhance the detection of errors. Therefore, it cannot match the release version in terms of running performance.

 

4. The following describes the Visual C ++ compiler options in msdn:These options select a single-threaded or multi-threaded runtime routine to indicate whether the multi-threaded module is a DLL, and select the release or debug version of The Runtime Library. (1)/MD     Define _ MT and _ DLL to select the specific multi-thread version and dll version of the runtime routine from the standard. h file at the same time. This option also enables the compiler to put the library name msvcrt. Lib into the. OBJ file. The application compiled with this option is statically linked to msvcrt. Lib. This library provides a code layer that allows the linker to parse external references. The actual working code is included in msvcr71.dll. This library must be available for applications linked to msvcrt. Lib at runtime. When
When _ static_cpplib (/d_static_cpplib) is used in/MD, it will cause the application to pass through the static multi-thread Standard C ++ Library (libcpmt. lib) instead of the dynamic version (msvcprt. lib), and still use msvcrt. lib is dynamically linked to the master CRT.

 

(2)/MDD     Define _ debug, _ Mt, and _ DLL to select a specific version of multi-thread debugging and DLL for the runtime routine from the standard. h file. It also enables the compiler to put the library name msvcrtd. Lib into the. OBJ file.

 

(3)/ml     Enable the compiler to put the library name libc. Lib into the. OBJ file so that the linker can use libc. lib to parse external symbols. This is the default operation of the compiler. Libc. Lib does not support multithreading.

 

(4)/MLD     Define _ debug and make the compiler put the library name libcd. Lib into the. OBJ file so that the linker can use libcd. lib to parse external symbols. Libcd. Lib does not support multithreading.

 

(5)/mt     Define _ mt to select a specific multi-threaded version of the runtime routine from the Standard Header (. h) file. This option also enables the compiler to put the library name libcmt. Lib into the. OBJ file so that the linker can use libcmt. lib to parse external symbols. Multi-threaded Program Creation requires/mt   Or/MD (or their debugging equivalent options/MTD or/MDD ).

 

(6)/MTD     Define _ debug and _ Mt. Defining _ mt will result in selecting a specific multi-thread version of the runtime routine from the standard. h file. This option also enables the compiler to put the library name libcmtd. Lib into the. OBJ file so that the linker can use libcmtd. lib to parse external symbols. To create a multi-threaded program,/MTD or/MDD (or their non-debugging equivalent options/Mt or Md) is required ).

 

(7)/LD     Create a DLL. Pass the/DLL option to the linker. The linker searches for the dllmain function, but does not need this function. If the dllmain function is not compiled, the linker inserts the dllmain function that returns true. Link DLL startup code. If the Export (. exp) file is not specified on the command line, create the import/export (. Lib); link the import/export link to the application that calls your DLL. Interpret/Fe as the name DLL rather than the. exe file. The default program name is the base name. dll.
But not the base name. .exe. If one of the/m options is not explicitly specified, change the default Runtime Library support to/mt.

 

(8)/LDD     Create a debug DLL. Define _ debug. Warning Do not mix the static and dynamic versions of the Runtime Library. There are multiple runtime database copies in a process, which may cause problems because the static data in the copies is not shared with other copies. The linker does not stop using both static and dynamic version links in the. exe file, but you can still use two (or more) Copies of the Runtime Library. For example, if it is linked to the Runtime library using the dynamic (DLL) version. when the EXE file is used together, the dynamic link library linked by the Runtime Library of the static (non-DLL) version may cause problems. (You should also avoid mixing the debug and non-DEBUG Versions of these libraries in a process ).

No.1: Error:Libcmtd. Lib (wincrt0.obj): Error lnk2019: external symbol that cannot be parsed _ winmain @ 16, which is referenced in the ___ tmaincrtstartup Function

Solution:

Project --> Property --> Configuration Property --> linker --> advanced --> entry point: Enter wwinmaincrtstartup.

No. 2: Error:

1> uafxcwd. Lib (afxmem. OBJ): Error lnk2005: "Void * _ cdecl operator new (unsigned INT )"(?? 2 @ yapaxi @ Z) has been defined in libcmtd. Lib (New. OBJ)
1> uafxcwd. Lib (afxmem. OBJ): Error lnk2005: "Void _ cdecl operator Delete (void *)"(?? 3 @ yaxpax @ Z) has been defined in libcmtd. Lib (dbgdel. OBJ)
1> uafxcwd. Lib (afxmem. OBJ): Error lnk2005: "Void * _ cdecl operator new [] (unsigned INT )"(?? _ U @ yapaxi @ Z) has been defined in libcpmtd. Lib (newaop. OBJ)
1> uafxcwd. Lib (afxmem. OBJ): Error lnk2005: "Void _ cdecl operator Delete [] (void *)"(?? _ V @ yaxpax @ Z) has been defined in libcmtd. Lib (delete2.obj)

Solution:

Project --> Property --> Configuration Property --> linker --> input --> additional dependency: add

Uafxcwd. Lib

Libcmtd. Lib

Project --> properties --> Configuration properties --> linker --> input --> ignore specific default Library: Join

Uafxcwd. Lib

Libcmtd. Lib

No. 3: Warning:

1> C: \ Program Files (x86) \ Microsoft Visual Studio 10.0 \ Vc \ include \ stdint. H (72): Warning c4005: "int8_min": Macro redefinition
1> C: \ Program Files (x86) \ microsoft sdks \ windows \ v7.0a \ include \ intsafe. H (144): see the previous definition of "int8_min"
1> C: \ Program Files (x86) \ Microsoft Visual Studio 10.0 \ Vc \ include \ stdint. H (73): Warning c4005: "int16_min": Macro redefinition
1> C: \ Program Files (x86) \ microsoft sdks \ windows \ v7.0a \ include \ intsafe. H (146): see the previous definition of "int16_min"
1> C: \ Program Files (x86) \ Microsoft Visual Studio 10.0 \ Vc \ include \ stdint. H (74): Warning c4005: "int32_min": Macro redefinition
1> C: \ Program Files (x86) \ microsoft sdks \ windows \ v7.0a \ include \ intsafe. H (148): see the previous definition of "int32_min"
1> C: \ Program Files (x86) \ Microsoft Visual Studio 10.0 \ Vc \ include \ stdint. H (76): Warning c4005: "int8_max": Macro redefinition

Solution:

Add the following to the header file:

# Ifdef _ msc_ver
# Pragma warning (push)
# Pragma warning (Disable: 4005)
# Include <intsafe. h>
# Include <stdint. h>
# Pragma warning (POP)
# Endif

No. 4: Warning:

Cancel the strcpy strcpy_s warnings.


Solution:

Project --> properties --> Configuration properties --> C/C ++ --> Preprocessor --> pre-processor definition: _ crt_secure_no_warnings

Add it to the header file.

# Ifndef _ crt_secure_no_warnings
# DEFINE _ crt_secure_no_warnings
# Endif

Reprinted from:

Http://blog.sina.com.cn/s/blog_6db64b4a01010pxr.html

Http://blog.csdn.net/mkr127/article/details/8555345

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.