C + + Learning Note 23: Libraries

Source: Internet
Author: User

Static library (Archives)

    • The suffix is generally "*.a"
    • Create a compile-and-link command for a single static library using two target files: ar cr libtest.a test1.o test2.o
    • Link all referenced and unhandled symbols when the linker searches for a static library
    • Place a link to a static library at the end of the command line to ensure that its references are parsed correctly

Dynamic library (Shared Object)

    • Shared target libraries (Windows-like DLLs) with a suffix of "*.so"
    • Compile command: g++-shared-fpic-o libtest.so test1.o test2.o
    • PIC: Location independent code (position-independent codes)
    • the translator first links the dynamic library, followed by the static library
    • If you want to force a link to a static library, compile using the-static option

Standard library and library dependencies

C Standard library: libc

Math Library Standalone: LIBM; When you need to call a mathematical function, explicitly link the math library: g++-O compute COMPUTE.C-LM

C + + standard library: libstdc++

Compile the C++11 program, use g++ -4.8-std=c++11, and for integrated development environments such as Code::Blocks, select the C++11 option in the Compiler Settings dialog box

Correlation of libraries

    • When linking, be aware that cross-references are parsed correctly, for example: Libtiff libraries require libjpeg libraries (JPEG image processing) and LIBZ libraries (compression processing)
    • Standalone Library Links: g++-static-o tifftest Tifftest.c-ltiff-ljpeg-lz
    • Related Library Links: g++-o app App.o-la-lb-la (cross-correlation between libraries)

Loading and unloading of dynamic libraries

Dynamic library loading Dlopen (): Header file: "Dlfcn.h"

function lookup and load function Dlsym ()

Dynamic library unload function: Dlclose ()

Dynamic Library error handling function Dlerror ()

Call a function in the dynamic library, set the function name to G

The C function should encapsulate extern "C" {...} when mixing C/D + + encoding. block, ensure that name resolution works correctly (C does not support function overloading)

Link Options: "-ldl"

void *handle = Dlopen ("libtest.so", Rtld_lazy);

Declaring a function pointer to a function in a dynamic library, finding it by the name of the called function

void (*test) () = Dlsym (handle, "G");

(*test) ()//Use function pointers to invoke functions in the dynamic library

Dlclose (handle);

C + + Learning Note 23: Libraries

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.