Search code examples
c++dynamic-linkingstatic-linking

How library classes are instantiated


I'm going to ask how it's done in c++, but this idea can apply to multiple languages. If you know how to do it in objective-c as well, please provide any similarities between the two

Lets say I want to create an instance of an ofstream like

ofstream myfile;

I'm assuming all I have on my computer is the *.o file (in a library archive) and the *.h file for iostream class. If this part isn't true let me know. I am assuming this when all I have installed is the runtime and the devel packages, not the source files.

How does it connect the header file to the object file, is there a naming scheme. And where does it look and in what order.?

Why this is confusing me is normally when I want to create a class I link my implementation of the class with the program, so where does it now and how does it now to link the files?

One more, does it matter if it loaded statically or dynamically?

Thank you in advance, and sry if this is a silly question.


Solution

  • Computer Science 101:

    1. Broadly speaking (VERY broadly!), there are two kinds of "programs":

      a) Interpreted: you read the program source line-by-line every time you execute it <= *nix shell scripts and DOS .bat files are "interpeted"

      b) Compiled: you read the source once (to convert it into a "binary machine code"). You link the machine code "object files" to build an "executable program".

    2. You're talking about "compiled programs"

    3. The "ofstream" part is irrelevant once the program is "compiled"

      The binary implementation for "ofstream" can be compiled directly into the executable, or it can be dynamically loaded from a shared library (.dll) at runtime.

    4. A "compiler" users ".h" headers to process the source file.

      A "linker" uses ".lib" libraries to match symbols and link static code at link type.

      The "Operating System" recognizes dynamic links and loads the needed shared libraries (.dll's) at runtime.

    Three different things, all independent of each other: Compiler/source code, Linker/machine object code, OS/executable programs

    'Hope that helps .. a bit...