Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to create and use static Library in Automake

2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

This article is to share with you about how to create and use static libraries in Automake. The editor thinks it is very practical, so I share it with you. I hope you can get something after reading this article.

1. The directory structure is as follows:

Example |-- src directory (storing source code files) |-- hello.c |-- lib directory (storing files used to generate libraries) |-- test.c is used to generate static libraries libhello.a |-- include directory (storing header files used in programs) |-- hello.h

two。 Source files in various directories written

Hello.h file extern void print (char *); test.c file # include void print (char * msg) {print ("% hello.h", msg);} hello.c file # include "hello.h" int main () {print ("Hello static library!"); / / the function return 0 in the static library is used here;}

3. Write lib/Makefile.am files

Noinst_LIBRARIES=libhello.a libhello_a_SOURCES=test.c AUTOMAKE_OPTIONS=foreign

The first line noinst indicates that a static library is generated, and you don't need make install. Just specify its location and name.

You can use it.

The second line represents the source file used to generate the static library. If you want to generate the static library somewhere else, you can follow the =

Add a path (it is recommended to use an absolute path and generate the static libraries you want to use in the same folder, such as lib).

The third line, AUTOMAKE_OPTIONS, is the option for Automake. Automake is mainly to help develop GNU software.

To maintain the software, so when you execute Automake, you will check whether the standard GNU software exists in the directory.

Files that should be available, such as' NEWS', 'AUTHOR',' ChangeLog', etc. When set to foreign, Automake

Will use the standard of general software to check. If you don't add this sentence, you need to execute touch NEWS before autoconf

README AUTHORS ChangeLog to generate files like 'NEWS',' AUTHOR', 'ChangeLog', etc.

4. Write src/Makefile.am files

AUTOMAKE_OPTIONS=foreign INCLUDES=-I../include bin_PROGRAMS=hello hello_SOURCES=hello.c hello_LDADD=../lib/libhello.a

The second line specifies the location of the header file, and-I is the abbreviation for idirafter. .. / include specifies the location of the header file,.. It's on.

First-level directory, that is, the example directory here.

The third line specifies the name of the generated executable file hello, where the executable file is generated under src, and it is recommended that the executable text

The component is generated into a specific folder and separated from the source code, such as / root/test directory. It is written as:

Bin_PROGRAMS=/root/test/hello, the following fourth and fifth elements are correspondingly changed to: _ root_test_hello_SOURCES=hello.c _ root_test_hello_LDADD=../lib/libhello.a

The fourth line specifies the source code file that generates the executable file hello. If hello.c is in another directory, you need to add

The complete path.

The fifth line specifies where you want to use the static library.

5. Generate the static library file lib/libhello.a.

Execute autoscan to generate the configure.scan file, rename it to configure.in, and modify its contents.

# configure.in # Process this file with autoconf to produce a configure script. AC_PREREQ 2.59 AC_INIT (libhello.a,1.1, []) AM_INIT_AUTOMAKE # Checks for programs. AC_PROG_CC # Checks for libraries. AC_PROG_RANLIB// needs to be added because of the use of the static library # Checks for header files. # Checks for typedefs, structures, and compiler characteristics. # Checks for library functions. AC_OUTPUT ([Makefile]) AC_INIT (FILE)

This macro is used to check the path of the source code, autoscan will be generated automatically, generally do not need to modify it.

AM_INIT_AUTOMAKE (PACKAGE,VERSION)

This is a necessary macro to use Automake, PACKAGE is the name of the software to be generated, and VERSION is the version

This number. You can also put information such as packages and version numbers in the AC_INIT (FILE) macro.

AC_PROG_CC

Check the C compiler available to the system. This macro is required if the source code is written in C.

AC_OUTPUT (FILE)

Set the file to be generated by configure. If it is Makefile, configure will check it out.

Populate the Makefile.in file to produce the appropriate Makefile. The following FILE is an output list of Makefile

You can choose the location and number of Makefile to be output. It is recommended that you only output Makefile in src.

Execute aclocal, autoconf, automake-- add-missing,. / configure, make in the lib directory

You can see the generated static library file libhello.an in this directory.

6. In the src directory, execute autoscan to generate the configure.scan file, rename it to configure.in and fix it.

Change its content.

# configure.in # Process this file with autoconf to produce a configure script. AC_PREREQ 2.59 AC_INIT (hello,1.1, []) AM_INIT_AUTOMAKE AC_CONFIG_SRCDIR ([hello.c]) # Checks for programs. AC_PROG_CC # Checks for libraries. # Checks for header files. # Checks for typedefs, structures, and compiler characteristics. # Checks for library functions. AC_OUTPUT ([Makefile])

7. Execute aclocal, autoconf, automake-- add-missing,. / configure, make in the src directory

Generate the executable file hello

8. Execute make install to install, and finally type hello to run the program to see the effect:

Hello static library!

The execution was successful!

Create and use static libraries using gcc

1. Write mylib.h files

# ifndef _ mylib_h_ # define _ mylib_h_ void welcome (); void outstring (const char * str); # endif

two。 Write mylib.c files to generate static libraries.

# include void welcome () {printf ("welcome to libmylib/n");} void outstring (const char * str) {if (stringency null) printf ("% s", str);}

3. Compile the source file to generate the object code

Gcc-o mylib.o-c mylib.c

4. Add the target file generated above to the static library and copy the static library to the default path of the system

Ar rcs libmylib.a mylib.o cp libmylib.a / usr/lib/

5. Write a test program to use the static library libmylib.a you just created

# include "mylib.h" # include Int main () {printf ("create and use library:/n"); welcome (); outstring ("It's a successful/n");}

6. Compile programs that use library functions

Gcc-o test test.c-lmylib

Run. / test to view the results.

Create and use dynamic libraries using Automake

The difference between a dynamic library and a static library is that the dynamic library is loaded into memory for calling functions when the program is executed.

1. The directory structure is as follows:

Example |-- src directory (storing source code files) |-- hello.c |-- lib directory (storing files used to generate libraries) |-- test.c is used to generate dynamic libraries libhello.la |-- include directory (storing header files used in programs) |-- hello.h

two。 Write the source files in each directory as follows:

Hello.h file extern void print (char *); test.c file # include void print (char * msg) {print ("% hello.h", msg);} hello.c file # include "hello.h" int main () {print ("Hello static library!"); / / the function return 0 in the dynamic library is used here;}

3. Compile the files that need to generate dynamic libraries in the lib directory, generate dynamic libraries, and install them into the standard library of the system for

Program call. The specific steps are as follows:

(1) write Makefile.am files

AUTOMAKE_OPTIONS=foreign lib_LTLIBRARIES=libhello.la libhello_la_SOURCES=test.c

Here lib_LTLIBRARIES means the generated dynamic library, and then specify the source file on which the dynamic library depends

Test.c, if there are multiple source files separated by spaces.

(2) in the lib directory, use the command autoscan to generate the configure.scan file and rename it to configure.in. this

You need to add the macro AC _ PROG_LIBTOOL to automatically generate a dynamic library using libtool.

# configure.in # Process this file with autoconf to produce a configure script. AC_PREREQ 2.59 AC_INIT (hello,1.0, [miaoquan@nou.com.cn]) AM_INIT_AUTOMAKE AC_CONFIG_SRCDIR ([test.c]) # AC_CONFIG_HEADER ([config.h]) # Checks for programs. AC_PROG_CC # Checks for header files. # Checks for typedefs, structures, and compiler characteristics. # Checks for library functions. AC_PROG_LIBTOOL AC_CONFIG_FILES ([Makefile]) AC_OUTPUT

(3) execute commands aclocal, libtoolize-f-c, autoconf, automake-- add-missing,. / configure,

Make and make install install the dynamic library into the standard library of the system for calling (usually / usr/local/lib).

Note: libtoolize provides a standard way to add libtool support to a software package, while GNU libtool is

A common library supports scripts that hide the complexity of using dynamic libraries in a unified, portable interface.

4. Generate hello executable files in the src directory

(1) write src/Makefile.am files

AUTOMAKE_OPTIONS=foreign INCLUDES=-I../include bin_PROGRAMS=hello hello_SOURCES=hello.c hello_LDADD=-lhello

-ldir specifies the path to the compile-time search base. Unlike static libraries, you do not have to specify a library path when creating a dynamic library

Path, the compiler automatically looks for libhello.so files in the standard library.

(2) execute autoscan to generate the configure.scan file, rename it to configure.in and modify its contents.

# configure.in # Process this file with autoconf to produce a configure script. AC_PREREQ 2.59 AC_INIT (hello,1.0, [miaoquan@nou.com.cn]) AM_INIT_AUTOMAKE AC_CONFIG_SRCDIR ([hello.c]) # AC_CONFIG_HEADER ([config.h]) # Checks for programs. AC_PROG_CC # Checks for header files. # Checks for typedefs, structures, and compiler characteristics. # Checks for library functions. AC_CONFIG_FILES ([Makefile]) AC_OUTPUT

(3) compile and generate the object file in the src directory, and execute the commands aclocal, libtoolize-f-c, autoconf,

Automake-add-missing,. / configure, make, at this time you must feel that success is close at hand. Again

Execute the target file. / hello, but the result is unexpected:

. / hello: error while loading shared libraries: libhello.so.0: cannot open shared object file:

No such file or directory

When executing the target file, Shell cannot find the location of the shared library and requires us to manually load the library path.

5. Two methods of searching the path location of dynamic library by shell

(1) use the command to import the path of the dynamic library, as follows:

Export LD_LIBRARY_PATH=dir (e.g. / usr/local/lib)

(2) modify the / etc/ld.so.conf file, add the search path, and use the ldconfig command to load the changes.

It is a wise choice to add all the paths where you might store your library files to / etc/ld.so.conf. Add

The method is also extremely simple, writing the absolute path of the library file directly into the OK, one line at a time. For example:

/ usr/local/lib

/ usr/lib

/ lib

It should be noted that this way of setting the search path for the libraries when the program is connected (including shared libraries and static libraries)

The location of the library is sufficient, but it is not enough for the execution of programs that use shared libraries. This is because

In order to speed up the positioning of shared libraries during program execution, and avoid the inefficiency of using search paths to find shared libraries,

So directly read the library list file / etc/ld.so.cache from which to search. / etc/ld.so.cache is a non

The data file of the text, which cannot be edited directly, is determined by the search path set in / etc/ld.so.conf

The / sbin/ldconfig command is generated by pooling the shared library files under these search paths (ldconfig command

To be executed with root privileges). Therefore, in order to ensure the location of the library when the program is executed, in / etc/ld.so.conf

After setting the library search path, you must also run the / sbin/ldconfig command to update / etc/ld.so.cache

Not until after the file. Ldconfig, to put it simply, its function is to list the libraries under the path of / etc/ld.so.conf

Files are cached to / etc/ld.so.cache for use. So when some library files are installed (for example, glib has just been installed)

Or after modifying ld.so.conf and adding a new library path, you need to run / sbin/ldconfig to make all the library files

Cached in ld.so.cache, if not done, even if the library file is clearly under / usr/lib, it will not be used.

Used, the result of the compilation process reported an error, the lack of xxx library, to check and find that it is clearly there, want to scold

Computer pig is a ^ _ ^. This method is highly recommended!

Using gcc to create and use dynamic Library

1. Create the mylib.c program as a dynamic library with the following command:

Gcc-fPIC-o mylib.o-c mylib.c

Gcc-shared-o libtt.so mylib.o

-fPIC acts during the compilation phase, telling the compiler to generate location-independent code (Position-Independent Code)

In the generated code, there is no absolute address, all relative addresses are used, so the code can be loaded into the

It can be executed correctly at any location. This is exactly what the shared library requires. When the shared library is loaded, it is not in the location of memory.

It's fixed.

-shared acts during the linking phase and is actually passed to the linker ld to add additional traces needed as a shared library

The information is used to remove the information that is not needed by the shared library.

You can also use the following command directly:

Gcc-fPIC-shared-o libtt.so mylib.c

two。 Copy the dynamic library to the standard library of linux, usr/local/lib or / usr/lib or / lib:

Cp libttt.so / usr/local/lib

3. When compiling the source program in the src directory, specify the directory of the dynamic library file and call the functions in the dynamic library

Gcc-o test test.c / usr/lib/libttt.so

4. Set the shell dynamic library search path and run the generated executable file

This is how to create and use static libraries in Automake. The editor believes that there are some knowledge points that we may see or use in our daily work. I hope you can learn more from this article. For more details, please follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report