简体   繁体   中英

Does the linker prevent duplicated linkage?

After some research and tests, I've found that, when linking to a library, my project needs to have the same linking option for the runtime library (MT, MD, etc) as the library I'm linking to.

What I'm wondering is, if I use a static library, (which are usually statically linked to the runtime library), am I not linking twice with the runtime library, since it's statically linked in my library and in my application? Or the linker prevents this?

Usually the static library will not be linked to the runtime library. Instead, all references to the runtime will be left unresolved, ie your static library file will simply contain the object files of your code, but not the object files of the runtime library.

Only when you build an actual executable using that library do you link to the runtime library, which will resolve the open references from the static library. This is usually the default behavior when compiling static linked libraries.

That being said, with most compilers you can force the linker to resolve external references already for the static library. This may be beneficial if your library has dependencies that you don't want to pass on to compilation of the final executable.

However, if that leads to duplicate symbols, as is likely when forcing early linking to the runtime, that may break the linker. If you're lucky you'll just get a warning about duplicate symbols, but it may just as well not link at all, depending on the implementation of your linker.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM