Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What every C++ developer should know about linking [closed]

Tags:

c++

linker

The problem

Information on how linking works in detail are scarce. Also IDEs hide the details of compilation which is a real pain when you have some linking related problem with your project.

Usually C++ books tell me that

C++ code --> preprocessed c++ code --> object code

But they really do not go into too much detail on what an average developer should know about linking despite the fact, that linking errors are common. How should a fresh C++ programmer know how to tackle an error like the following?

XmlRpcSocket.o:XmlRpcSocket.cpp:(.text+0x48b): undefined reference to `WSAGetLastError@0'

But this question is not specifically about this problem (-lwsock32 solves it). The problem is the lack of general knowledge about linking. My university C++ lecturer talked about linking for the time of one slide with a few black-boxes on it.

Also, resources on how linking works are scarce and most of the people I know still considers linking as a black-box operation. What I have learned about linking is through experimentation, and kinda "picked it up along the way", but the problem with this approach is that it raises more questions than it answers.

For example: I know .LIB files are library files which are bundles of object files. Now, how should one build and use a .LIB file? When is it desirable to use .LIB file? When sould I build static .LIB files or ones that reference DLLs? When I link a .LIB file with my object files do everything gets copied in, or just the object files I use? When should I build DLLs/so files instead of linking statically? Do I have to learn about the inner structure of object files to solve common problems? What do I have to know about name-mangling? When is it relevant? Can I link several different standard libraries with my project if one of the dlls load the old msvcrt? etc.

The question

Obviously I don't expect an answer to all the above listed questions in one go. I just need to know where to start. Is there a resource like "What Every Programmer Should Know About Memory", which talks about linking? So, I need resources to learn from and your insight on which direction should I go to learn about the linking process.

What are the things every developer should know about linking?

like image 880
Klaufir Avatar asked Aug 24 '12 22:08

Klaufir


Video Answer


2 Answers

There are TONS of resources.

I guess that might be overwhelming for a "beginner" ;)

As far as "Windows", you could do worse than to start here:

  • Windows Architecture Overview

  • Beginner's Guide to Linkers

I'd also recommend this:

  • Programming from the Ground Up

'Hope that helps :)

PS:

As clarification:

  • You want to know how the bits you've "compiled" (into machine code) all come together by "linking". A totally fair question :)

  • It also helps to know a bit about how that machine code relates to a "running program"...

  • ... and how a "running program" uses some bits from the (static) .exe, and other bits from the dynamic runtime (.dll's, or "dynamically linked libraries).

  • All of these details are often completely platform- and OS-specific

  • Hence the (varied) links.

Again, I hope all that helps with your initial question.

like image 110
paulsm4 Avatar answered Oct 26 '22 18:10

paulsm4


I highly recommend reading John Levine's "Linkers & Loaders". Most of the book is available online here but you should buy the book and support the author. This book taught me quite a lot about how to really dig into the binaries.

like image 43
D.Shawley Avatar answered Oct 26 '22 18:10

D.Shawley