What Every C ++ Developer Should Know About Binding

Problem

Information on how links work is not enough. In addition, the IDE hides compilation details, which are a real pain when you have a problem linking to your project.

Usually C ++ books tell me that

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

But they really donโ€™t go into too much detail about what the average developer should know about communication, even though communication errors are common. As a new C ++ programmer, he knows how to solve the error, for example the following:

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

But this question is not specific to this problem (-lwsock32 solves it). The problem is the lack of general knowledge of linking. My C ++ university lecturer talked about timing one slide with several black boxes on it.

In addition, resources for how links work are scarce, and most of the people I know still consider binding as a black box operation. What I learned about binding is an experiment, and it seems to have โ€œlifted it in its path,โ€ but the problem with this approach is that it has more questions than answers.

For example: I know that .LIB files are library files that are packages of object files. Now, how to create and use a .LIB file? When is it advisable to use a .LIB file? When can I create static .LIB files or those that reference a DLL? When I link the .LIB file to my object files, is everything copied, or only the object files that I use? When do I need to create DLLs / so libraries instead of linking statically? Should I learn about the internal structure of object files to solve common problems? What should I know about imitation? When is this relevant? Is it possible to link several different standard libraries with my project if one of the DLL loads the old msvcrt? and etc.

Question

Obviously, I do not expect an answer to all of the above questions at once. I just need to know where to start. Is there a resource like โ€œWhat Every Programmer Should Know About Memory,โ€ which talks about communication? So, I need resources to learn and understand which direction I should go in order to learn about the linking process.

What things should every developer know about binding?

+6
source share
4 answers

There are tons of resources.

I guess this can be huge for a "newbie";)

As for Windows, you can do worse than start here:

I would also recommend this:

'Hope that helps :)

PS:

By way of explanation:

  • You want to know how the bits that you "compiled" (in machine code) are combined by "binding." Totally fair question :)

  • It also helps to learn a little about how this machine code relates to a "running program" ...

  • ... and how the "running program" uses some bits from the (static) .exe and other bits from the dynamic runtime (DLL or dynamically linked libraries).

    / li>
  • All of these details are often fully compatible with the platform and OS.

  • Consequently, (diverse) links.

Again, I hope all of this helps with your initial question.

+2
source

I highly recommend reading John Levine's "Linkers and Loaders" . Most of the book is available online here , but you must buy the book and support the author. In this book, I talked a lot about how to really dig into binary files.

+1
source

If you are starting to program / C ++, I will start with a good understanding of how static and dynamic libraries work, and take a couple of tutorials to create a DLL and .lib. Google is your best friend for this: I just find it, for example: http://www.bogotobogo.com/cplusplus/libraries.php .

Note that the way you create lib depends on your C ++ compiler. The ones on my head are an integrated development environment (IDE) such as Visual Studio or the gnu C ++ compiler.

0
source

One point you should be aware of is ODR, and especially what it means for inline characters.

Wikipedia entries can be a good start.

0
source

Source: https://habr.com/ru/post/923716/


All Articles