Compilation and use of static and dynamic libraries on Linux (with external symbol errors)

Source: Internet
Author: User

Main reference Blog GCC create and use static libraries and dynamic libraries

For students who are familiar with Windows, the static library on Linux. A is equivalent to win's. lib, dynamic library. So equals win. dll.

First, briefly explain the differences between the two libraries, and refer to the Linux programming

1. The static library is also called the Archive (archive, so the creation command is AR), the compiler and linker are responsible for combining the program code and the static library together to form a separate executable file;

But the disadvantage is that many applications run concurrently and use functions from the same static library, there is a multiple copy of the function in memory, and the program file itself has multiple copies of the same copy, which consumes a lot of memory and disk space.

2. Dynamic libraries, also known as shared libraries (so the Create command contains share). The executable file does not contain the function code of the dynamic library, but rather refers to the shared code that the runtime can access, and the dynamic library is loaded into memory when the function reference is parsed and a call to the dynamic library is generated. Therefore, the system can keep only one copy of the dynamic library, and when the function needs to change, only need to recompile the generation of the dynamic library, instead of recompiling the entire source program.

Now go straight to the point, paste the code. First I created three files hello.h hello.c main.c, the first two of which were the header and source files of function Hello (), and main.c called the Hello () function. The code is as follows

/*************************************************************************    > File name:hello.h *********** /#ifndef _hello_h_#define _hello_h_void HELLO (); endif
/*************************************************************************    > File name:hello.c *********** /#include <stdio.h> #include "hello.h" void Hello () {printf ("Hello world!\n");}
/*************************************************************************    > File name:main.c ************ /#include "hello.h" int main (int argc, char** argv) {    hello ();    return 0;}

The directory is organized as follows (tree is used here, I am Ubuntu system, Command apt-get install tree)

The two SH files are shell files that use dynamic libraries and static libraries, and the command line is integrated into the code as follows

########################################################################## File name:static-compile.sh########## ################################################################!/bin/bashcd./libgcc-c-I.. /include hello.c  # generate Hello.oar rc libhello.a hello.o  # Build LIBHELLO.ACD: /SRCGCC main.c-i. /include-l. /lib-lhello-o main  # generates MAINCD. /  # Go back to the root directory

First enter the Lib directory to compile the Custom function library source file hello.c to build the target file hello.o. Then use AR RC (AR is archive, RC represents replace and create respectively, even if already exists replace, create new file) to generate static library LIBHELLO.A.

Then go to the SRC directory, I followed by (no spaces) header file directory path (i for include), and immediately after the (no space) library file directory path (L for Lib)

########################################################################## File name:shared-compile.sh########## ################################################################!/bin/bashcd./lib# Generating Dynamic library LIBHELLO.SOGCC Hello.c-i .. /include-fpic-shared-o libhello.so # Generate executable CD. /SRCMV. /lib/libhello.so./cp./libhello.so/lib  
Cd.. / # Go back to the root directory

For dynamic libraries, the build command is-fpic and-shared. Pic Stands for position-independent Code, regardless of location, that is, the relative path and absolute path are used. Shared representative.

Before using a dynamic library, you need to copy. So into the/lib folder (or set the environment variable, see my blog at the beginning of my article).

Commands that use the dynamic library have a-l (lowercase L), followed by Hello, in addition to-I and-L.

-This is because my dynamic library is named Libhello.so, and using-L ignores the previous Lib and the suffix behind it.

Run static-compile.sh and shared-compile.sh under Linux, respectively, with the following effects

Because the dynamic library does not add the code of the library to the executable, it can be seen that the program size that is linked with the dynamic library is small (8592<8664)

Of course, after generating the program, the. A,. So (non-/lib directory) files can be deleted, the program can be run.

Finally, we talk about the practical application of the two, in Visual C + + programming.

Many times the unsolved external symbol (unresolved external symbol) error occurs, and if you are using another person's library, it is likely that you forgot to add the required. lib file (that is, the static library) to the additional dependencies in the menu settings-linker, input. (for example, when using some library functions of winsock2.h, there is no #pragma comment (lib, "Ws2_32.lib") to load the Ws2_32.lib static library, the function is only the declaration in the header file, but the definition in the library file is missing.

And if you forget to add the. dll path (often the Bin folder) to the environment variable (PS: My dynamic-compile.sh corresponds to the windows equivalent of putting the DLL into the System32 directory), in the compile time will not be error, but debug when the runtime will error.

This is the significant difference between a static library and a dynamic library, where the static library is found by the linker through the Include directory during compilation and linked to the executable file, while the dynamic library is a dynamic call during runtime, and only the runtime cannot find the corresponding dynamic library to make an error.

Say back to the external symbol error, if it is written by itself, it is possible that the function is only compiled, not defined. For example, F.h is written in void F (); The result of the corresponding f.cpp is that void F2 () {} Seems unlikely to occur, in fact, because the C + + overload in a sense changed the function name (and also changed a lot), such errors are very common for beginners.

There is a typical example is C + + call C function, the error is as follows

" void __cdecl f (void) "  in function _main
Hello.h#pragma oncevoid f ();
Hello.c#include "hello.h" void F () {}
Main.cpp#include "Hello.h" int main () {f (); return 0;}

Because C + + eyes, void f (void), in fact, void [email protected] @YAXXZ (void), (f later depends on the compiler), and in the eye, F is f. Generally need to use extern "C" to use, or directly change the hello.c suffix to hello.cpp.

Compilation and use of static and dynamic libraries on Linux (with external symbol errors)

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.