Shared library. Creating and installing shared libraries on Linux


Most modern versions of Unix support shared libraries. Shared libraries allow you to remove library functions from the executable file; As a result, a single copy of the library is stored in system memory and accessed by all processes. This significantly reduces the size of the executable files, but may slightly increase the load when the application is launched for the first time or when a library function is called for the first time. Another advantage of shared libraries is that when you update a library, you don't have to fix the library links in every program that uses that library. (Here we assume that the number and types of arguments to library functions have not changed.)

Linux OS provides the ability to tell the compiler cc, whether shared libraries should be used in the program or not.

7.7. Memory allocation

Standard ISO C defines three functions that help allocate memory:

    Function malloc allocates a specified number of bytes of memory. The allocated memory is not cleared.

    Function calloc allocates space for a specified number of objects of a certain size. The allocated memory is filled with zero bytes.

    Function realloc redistributes previously allocated memory, increasing or decreasing its size. An increase in the previously allocated volume may be accompanied by moving a memory section to a new location. In addition, the portion of memory that ends up between the end of the previously allocated block and the new end is not initialized.

#include

void *malloc(size_t size);

void *calloc(size_t nobj, size_t size);

void *realloc (void *ptr, size_t newsize);

/* all three functions return a non-null pointer on success, NULL on error */

void free(void *ptr);

These three functions ensure that pointers are returned that have an alignment suitable for storing data objects of any type. For example, if a constraint on a particular system is that objects of type double must be placed at addresses that are multiples of 8, then all pointers returned by these functions will contain addresses that are multiples of 8.

All three functions return an untyped pointer void *, therefore, by connecting the header file to the program that contains function prototypes, we should not perform an explicit type cast when assigning function values ​​to typed pointers.

Function free frees previously allocated memory pointed to by the argument ptr. The freed space is typically placed in a free memory pool and can be re-allocated the next time one of the three functions is called.

Function realloc allows you to increase or decrease the size of a previously allocated memory area. For example, if we allocate memory for an array of 512 elements and then, while filling it, suddenly discover that we need memory to store more than 512 elements, we can call the function realloc. In this case, if there is a block of free memory of sufficient size behind the previously allocated area, the function realloc doesn't move anything anywhere. It simply appends the area of ​​the required size to the end and returns the same pointer that was passed to it. However, if there is no free memory behind the previously allocated area, the function realloc finds another area of ​​memory of sufficient size and copies the contents of the existing 512-element array to a new location, then frees the old memory area and returns a pointer to the new area. Since a previously allocated memory area can be moved, we should not use other pointers to this area after calling the function realloc.

Note that the last argument to the function realloc determines the new size of the required area, not the difference between the new and old sizes. In the special case when in the argument ptr a null pointer is passed, a function realloc behaves like a function malloc, allocating a memory area of ​​size newsize.

Memory allocation functions are usually implemented using a system call sbrk. This system call expands (or truncates) the heap area of ​​the process (Figure 7.3). Example of a typical function implementation malloc And free is given in section 8.7 [Kernighan, Ritchie].

Although the system call sbrk can either increase or decrease the amount of process memory, most versions malloc And free never reduce it. The freed space becomes available for subsequent allocation, and, as a rule, is not returned to the kernel - it is placed in the function's free memory pool malloc.

It is important to understand that most implementations allocate slightly more memory than required and use additional space to store overhead information: the size of the allocated block, a pointer to the next allocated block, etc. This means that writing outside the allocated area can destroy the overhead information in the next block. Errors of this kind are often catastrophic and extremely difficult to find because they may not appear for quite a long time. In addition, it is possible to destroy the service information of a memory block if you write data before the beginning of the allocated area.

Writing outside the allocated memory block can destroy not only service information. The memory before and after such a block can be used to store other dynamic objects. These objects may not be associated with the section of the program that destroys them, which further complicates the search for the source of damage.

Other possible errors that can be fatal are an attempt to free a block of memory that has already been freed, as well as passing a function free a pointer that was not obtained from one of the three memory allocation functions. If a process calls a function malloc, but forgets to call the function free, then the amount of memory used begins to increase continuously; this is called a memory leak. If the process will not return blocks of memory that have become unnecessary using the function free, then the amount of address space occupied by the process will slowly increase until the free space runs out. This can lead to decreased system performance due to unnecessary calls to the page file.

Because memory allocation errors are so difficult to detect, some operating systems (including Linux) provide versions of memory allocation functions that perform additional error checking on each call.

A shared library allows the symbols it contains to be used across multiple movies without having to copy those symbols into the movie libraries. Because of this, shared library objects are called resources(Assets). In this case, the shared library is used as an external file and is not added to the created (or edited) movie.

The use of shared libraries is advisable, for example, in the following cases:

  • when using the same soundtrack on several pages of the site;
  • when sharing text font characters on several pages of the site;
  • when you need to provide a single source for animation elements used in multiple scenes in a movie or across multiple movies;
  • when you want to have a central library of resources to make it easier to control changes.

Flash MX supports two types of shared libraries:

  • Run-time- shared runtime library; symbols included in such a library are available for sharing by several films, however, such symbols can only be edited directly in the source film;
  • Author-time- library shared during development; The symbols included in such a library can be shared among multiple films, and the contents of the library can be edited in any co-owned film.

For shared library resources to be available in movies hosted on a remote site, the library's Flash file must be exported to SWF format and uploaded to the Web site.

Comment
The previous version of Flash only supported the runtime shared library
.

To create a shared library like Run-time, necessary:

  1. Determine its resources (the characters included in it) in a separate film.
  2. Allow export of shared characters.
  3. Specify the URL of the site where the library will be hosted.
  4. Export a Flash file with this library in SWF format and upload it to a website.

To be able to use shared library symbols Run-time in other films ("co-owners"), you need to create a link to the shared symbols in each of them.

Now let's look at the steps listed above in more detail.

After you create a shared library, you need to specify which symbols included in it can be exported to other movies. To do this you need to do the following:

  1. Select the symbol you want to make "shared" from the list.
  2. In the symbol's context menu, select the command Linking(Binding).
  3. In the dialog box that opens Symbol Linkage Properties(Symbol binding parameters), fig. 10.12, check the box Export for runtime sharing(Allow export at runtime).
  4. In a text field Identifier enter the name (identifier) ​​of the symbol under which it will be exported to the co-owner movie; Although the field defaults to the symbol's library name, if it contains spaces, remove them.
  5. In a text field URL Enter the Internet address of the source movie (that is, the shared library SWF file).
  6. If the exported symbol should be used directly from the first frame of the co-owner movie, select the Export in first frame.
  7. If you want the exported symbol to be available in an ActionScript script, select the Export for ActionScript.

Rice. 10.12. Shared Library Symbol Settings Dialog Box

To use shared library resources Run-time in a co-owned movie, you need to do the following:

  1. Open this movie's library by selecting Window team Library.
  2. From the library drop-down menu, select the command New Symbol; As a result, a dialog box will appear on the screen Create New Symbol(Create a new symbol), the central part of which is similar in format to the dialog box Symbol Linkage Properties(Fig. 10.13).
  3. In a text field Identifier Enter the name of the symbol that you want to import into the co-owner movie.
  4. In a text field URL Enter the Internet address of the source movie.


Rice. 10.13. Dialog box for setting split character parameters

A shared library allows the symbols it contains to be used across multiple movies without having to copy those symbols into the movie libraries. Because of this, shared library objects are called resources(Assets). In this case, the shared library is used as an external file and is not added to the created (or edited) movie.

The use of shared libraries is advisable, for example, in the following cases:

  • when using the same soundtrack on several pages of the site;
  • when sharing text font characters on several pages of the site;
  • when you need to provide a single source for animation elements used in multiple scenes in a movie or across multiple movies;
  • when you want to have a central library of resources to make it easier to control changes.

Flash MX supports two types of shared libraries:

  • Run-time- shared runtime library; symbols included in such a library are available for sharing by several films, however, such symbols can only be edited directly in the source film;
  • Author-time- library shared during development; The symbols included in such a library can be shared among multiple films, and the contents of the library can be edited in any co-owned film.

For shared library resources to be available in movies hosted on a remote site, the library's Flash file must be exported to SWF format and uploaded to the Web site.

Comment

The previous version of Flash only supported the runtime shared library.

To create a shared library like Run-time necessary:

  1. Determine its resources (the characters included in it) in a separate film.
  2. Allow export of shared characters.
  3. Specify the URL of the site where the library will be hosted.
  4. Export a Flash file with this library in SWF format and upload it to a website.

To be able to use shared library symbols Run-time in other movies (“co-owners”), you need to create a link to the shared symbols in each of them.

Now let's look at the steps listed above in more detail.

After you create a shared library, you need to specify which symbols included in it can be exported to other movies. To do this you need to do the following:

  1. Select the symbol from the list that you want to make "separable".
  2. In the symbol's context menu, select the command Linking(Binding).
  3. In the dialog box that opens Symbol Linkage Properties(Symbol binding parameters), fig. 10.12, check the box Export for runtime sharing(Allow export at runtime).
  4. In a text field Identifier enter the name (identifier) ​​of the symbol under which it will be exported to the co-owner movie; Although the field defaults to the symbol's library name, if it contains spaces, remove them.
  5. In a text field URL Enter the Internet address of the source movie (that is, the shared library SWF file).
  6. If the exported symbol should be used directly from the first frame of the co-owner movie, select the Export in first frame.
  7. If you want the exported symbol to be available in an ActionScript script, select the Export for ActionScript.

Rice. 10.12. Shared Library Symbol Settings Dialog Box

To use shared library resources Run-time in a co-owned movie, you need to do the following:

  1. Open this movie's library by selecting Window team Library.
  2. From the library drop-down menu, select the command New Symbol; As a result, a dialog box will appear on the screen Create New Symbol(Create a new symbol), the central part of which is similar in format to the dialog box Symbol Linkage Properties(Fig. 10.13).
  3. In a text field Identifier Enter the name of the symbol that you want to import into the co-owner movie.
  4. In a text field URL Enter the Internet address of the source movie.

Rice. 10.13. Dialog box for setting split character parameters

Using a different type of shared library - Author-time- allows you to change (more precisely, replace) the contents of symbols in the edited FLA file. It should be borne in mind that the name of the symbol is already “hardwired” into the movie being edited. Therefore, a symbol imported from a shared library, as it were, replaces the original symbol, preserving its name. If the symbol you are importing contains nested symbols, they will also be imported.

To link the symbol to be “substituted” with the corresponding symbol from the shared library, you must:

  1. In the file you are editing, open the library window by selecting from the menu Window team Library.
  2. In the list of symbols, select the symbol to be “substituted”, right-click on it and select the command from the context menu Properties.
  3. At the bottom of the dialog box that opens. (see Fig. 10.13), in the group of elements Source(Source) click on the button Browse(View), and in the window Window Explorer select the FLA file with the desired library; As a result, another window will appear on the screen - Select Source Symbol(Select source symbol), which contains a list of all symbols of the shared library (Fig. 10.14).


Rice. 10.14. Select Source Symbol Dialog Box

  1. Select the desired symbol from the list and click on the button OK; this will close the window and change the format of the controls in the group Source(Fig. 10.15):
    • the checkbox will become available Always update before publishing(Always update before publishing) - if set, the symbol in the edited movie will be automatically replaced with a symbol from the shared library when publishing the movie;
    • in the text field File the path to the file with the shared library will appear;
    • in the text field Symbol Name the name of the symbol from the shared library will appear;
    • the button will become available symbol, clicking on which opens a dialog box Select Source Symbol shown in Fig. 10.14.


Rice. 10.15. Format controls in the Source group after linking symbols

Comment

A movie can use resources from multiple shared libraries of any type.

In conclusion, here's another way to use the contents of another movie's library. To do this you need:

  1. Select from menu File(File) main window Flash command Open As Library(Open as library).
  2. Using a window Windows Explorer specify a Flash file with the required library; As a result, the window of this library will be open on the table.
  3. Copy the desired symbols from the library window to the movie library (or directly to the table).

The contents of such a library, like the contents of shared and permanent libraries, cannot be modified from a client movie (that is, from a movie that uses its resources).

Static and dynamic libraries.

Libraries are “collections” of subroutines or objects, usually focused on solving a set of related problems. From the point of view of their organization and use, libraries can be static or dynamic.

Static libraries can be a set of source codes that the programmer plugs into his program, or in the form of pre-compiled object files linked together at the compilation stage. On Windows, such files usually have the extension<.lib>. By linking with a static library, the program includes all the functions it uses, which increases its size but makes it more autonomous.

Dynamic libraries (shared library, dynamic link library) are loaded by the operating system at the “demand” of the running program already during its execution. If the required library has already been loaded into memory, then re-loading is not performed. In this case, the same set of functions or library objects can be used simultaneously by several running programs, which allows efficient use of RAM resources. Dynamic libraries on Windows usually have the extension<.dll>.

To use the library, you need to tell the compiler that you need to include it and call the function from the library (using the corresponding header file), while the source text of the function is not needed.

Creating a dynamic library.

In Microsoft Visual Studio, creating a project to build a dynamic library is similar to the usual procedure for creating a project for a command-line application. The project type is Win32 Console Application, only now in the project creation wizard you will need to select the “DLL” item:

If there is no such item in the wizard, you can create a regular Console Application project, and then set its type in the project properties:

Configuration Properties => General => Configuration Type: Dynamic Library (.dll)

Declspec(dllexport) void display(const char * str);

#include "dlltest.h"

void display(const char * str)

The __declspec(dllexport) modifier allows the library to export the specified function for use by other applications.

As a result of building the project, a dynamic library file with the extension will be created<.dll>, as well as an import library file with the extension<.lib>. The import library is intended to facilitate subsequent use of the dynamic library. Even though the import library file extension is the same as the standard extension for static libraries, they should not be confused.

Using a dynamic library.

There are two ways to use a dynamic library in a program. Implicit linking involves using an import library to determine the addresses of the functions provided by the library. The operating system loads the DLL after loading the program's executable file. The executable calls the exported DLL functions as if the functions were contained in the executable itself.

When explicitly linked, an executable that uses a DLL must make function calls to explicitly load and unload the DLL and access the DLL's exported functions. The client executable calls exported functions using a function pointer.

Regardless of the method chosen, the executable file can use the same DLL. Moreover, these mechanisms are not mutually exclusive, because while one executable links implicitly to the DLL, another can do the explicit linking.

As an example, we will use the implicit linking mechanism (as the simplest) to connect the constructed simple library.

Let's create a separate project for a regular console application (possibly within the same solution as the library project itself). To use the display function implemented in the library, you must:

    Include the corresponding header file “dlltest.h”. To do this, you need to either use the path to the file directly in the #include directive (if the projects are in the same solution folder, then it is better to use a relative path), or in the project properties add the path to this header file in the section

Configuration Parameters => C/C++ => General => Additional Include Directories

    Use the appropriate import library file. To do this, you can use a directive of the form (here the relative path to the built lib file is used).

You can also add the path to the import library file in the project properties under

Configuration Parameters => Linker => Input => Additional Dependencies

Now let's create the main program file with the following content:

#pragma comment(lib, "../Debug/dlltest.lib")

#include "dlltest.h"

display("Hello");

system("pause");

To successfully launch the program, all that remains is to ensure the availability of the corresponding DLL library. When the program starts, the operating system will look for all required library files in the folder where the program was launched, as well as in the system paths specified by the PATH environment variable. Accordingly, the DLL file of the built library must either be located in the same folder as the executable file of the calling program, or in one of the folders defined in PATH.

Did you know, What is a thought experiment, gedanken experiment?
This is a non-existent practice, an otherworldly experience, an imagination of something that does not actually exist. Thought experiments are like waking dreams. They give birth to monsters. Unlike a physical experiment, which is an experimental test of hypotheses, a “thought experiment” magically replaces experimental testing with desired conclusions that have not been tested in practice, manipulating logical constructions that actually violate logic itself by using unproven premises as proven ones, that is, by substitution. Thus, the main task of the applicants of “thought experiments” is to deceive the listener or reader by replacing a real physical experiment with its “doll” - fictitious reasoning on parole without the physical verification itself.
Filling physics with imaginary, “thought experiments” has led to the emergence of an absurd, surreal, confused picture of the world. A real researcher must distinguish such “candy wrappers” from real values.

Relativists and positivists argue that “thought experiments” are a very useful tool for testing theories (also arising in our minds) for consistency. In this they deceive people, since any verification can only be carried out by a source independent of the object of verification. The applicant of the hypothesis himself cannot be a test of his own statement, since the reason for this statement itself is the absence of contradictions in the statement visible to the applicant.

We see this in the example of SRT and GTR, which have turned into a kind of religion that controls science and public opinion. No amount of facts that contradict them can overcome Einstein’s formula: “If a fact does not correspond to the theory, change the fact” (In another version, “Does the fact not correspond to the theory? - So much the worse for the fact”).

The maximum that a “thought experiment” can claim is only the internal consistency of the hypothesis within the framework of the applicant’s own, often by no means true, logic. This does not check compliance with practice. Real verification can only take place in an actual physical experiment.

An experiment is an experiment because it is not a refinement of thought, but a test of thought. A thought that is self-consistent cannot verify itself. This was proven by Kurt Gödel.







2024 gtavrl.ru.