Visual Studio: What exactly are lib files (used for)?

前端 未结 3 1025
谎友^
谎友^ 2021-01-13 09:43

I\'m learning C++ and came across those *.lib files that are obviously used by the linker. I had to set some additional dependencies for OpenGL.

  • What exactly a
相关标签:
3条回答
  • 2021-01-13 10:17

    .lib files are "libraries" and contain "collections" of compiled code so-to-speak. So it is a way to provide software components, without giving away the internal source-code for example. They can be generated as "output" of a "build" just like executables are.

    The specific contents depend on your platform / development environment, but they will contain symbols for the linker to "hook up" function-calls provided by e.g. the header-file of the library.

    Some libraries are "dynamic" (.DLL's on Windows), which means the "hooking" of function-calls is setup when the executable using the library is loaded, allowing the library implementation to be changed without rebuilding the executable.

    One last thing. You say you're learning C++, and a common confusing point is, that "symbols" generated by C++ compilers are "mangled" (in order to allow e.g. function overloading), and this "mangling" is not standardized across different compilers, so libraries often resort to C for the "API" of the library (just like OpenGL), even though the library may be implemented in C++ internally.

    I hope shed some light on .lib-files. Happy OpenGL coding :-)

    0 讨论(0)
  • 2021-01-13 10:31

    What exactly are library files in this context used for?

    They are compiled and linked code just like your executable. They're called static libraries that other programs can link to at compile time. In the case of OpenGL, you link to their libraries to build an executable that can run OpenGL code. Dynamic libraries (DLLs) are another type of library that executables link against, except at runtime.

    What are their contents?

    Static libs contain linked object code just like an exe. The *.obj files are the object code that the compiler generates for the linker.

    How are they generated?

    When the compiler creates the object files, it passes the work to the linker. You can create them in your development environment, just like executables.

    Is there anything else worth knowing about them?

    Yeah, they're used everywhere, so it doesn't hurt to get used to them.

    0 讨论(0)
  • 2021-01-13 10:35

    In simple terms, yes - .lib files are just a collection of .obj files.

    There is a slight complication on Windows that you can have two classes of lib files.
    Static lib files essentially contain a collection of .obj and are linked with your program to provide all the functions inside the .lib. They are mainly a convenience to save you having as many files to deal with.

    There are also stub .lib which provide just the definitions of functions which are contained in a .dll file. The .lib file is used at compile time to tell the compiler what to expect from the function, but the code is loaded at run time from the dll.

    0 讨论(0)
提交回复
热议问题