Part 4 Use Macros in Configure. In to detect

Source: Internet
Author: User
Tags gtk

The fourth part is in configure. in, use a macro to detect ++
I. How to automatically search for a database and configure-I and-l:
1. The library carries the. PC file:
If the. PC file is added to the $ (prefix)/lib/pkgconfig/directory, the following information can be extracted:
Pkg_check_modules (DEP, GTK +-2.0> = 2.10.0 orbit-2.0> = 0.2)
Ac_subst (dep_cflags)
Ac_subst (dep_libs)

Here, I want to find two libraries, one of which is GTK. Go to/usr/lib/pkgconfig and check whether the file name is GTK +-2.0.pc, take the previous part of GTK +-2.0 as the Library name in pkg_check_modules, and then >=is the version number. If there are multiple databases, such as orbit, write them directly. As for DEP, It is a random name. Pay attention to the same three dep. The purpose of ac_subst is to output variables so that makefile. Am can be used later.
The following two ac_subst: the first one is used to set dep_cflags as the PKG-config -- cflags section of the above two databases, and the second one is used to set dep_libs as the PKG-config -- libs section of the above two databases.

Then, in your makefile. am, write @ dep_cflags @ in am_cppflags. Write @ dep_libs @ In the ldadd section. Or for the library, write @ dep_libs @ In the _ libadd section.

It must be noted that pkg_check_modules is a M4 macro, which is in/usr/share/aclocal. provided by the M4 file, I found it is PKG. this macro exists in M4.
Therefore, if your. M4 file is no longer located and the aclocal file cannot be found, use aclocal-I... to specify the. M4 file.

2. The library directly places its own. M4 in/usr/share/aclocal:
For example, am_path_gtk_2_0, am_path_gtkmm is put by GTK and gtkmm in the aclocal directory. M4, GTK is placed in the gtk-2.0.m4 file. It should be viewed directly. DNL am_path_gtk_2_0 (..., [...]) the [] clause is optional, and the [] clause in [] is optional.
In this way, you can first search for whether the library is provided, and then find the macro in. M4, for example, the usage of GTK macro is as follows:
Am_path_gtk_2_0 (2.10.0, ac_msg_error (kid 0.1 needs GTK + 2.2.0 ))
The first parameter is the version that should be met, and the second parameter is the information to be printed if it is found. This parameter is null. If the third parameter cannot be found, the information to be printed, the Macro that prints the error message "ac_msg_error" is used here.

Then, you can see its. M4. It gives you the gtk_cflags and gtk_libs in ac_subst, and then uses @ in makefile. AM to reference them.

In fact, GTK not only places the. M4 file in/usr/share/aclocal/, but also places the. PC file in/usr/lib/pkgconfig, as mentioned above. Which one do you want to use.

3. The Library has the XX-config script installed:
Think about it. wxwidget installs the WX-config script. The parameters are similar to those of the PKG-config command. This library is used in combination with Configure. In as follows:
Taking the gnome library as an example, it should provide. M4 or other files somewhere (such as the macros directory), and then in Configure. In:
# Gnome --:
# (These macros are in the 'macros' directory,
# Copied from the gnome-libs distribution .)

# Gnome_init sets the gnome_config variable, among other things:
Gnome_init
Gnome_common_init
Gnome_compile_warnings

# GNOME-CONFIG script knows about gnomemm:
# ('Gnome-config' is installed by gnome)
# So call gnome-config with some arguments:
Gnomemm_cflags = '$ gnome_config -- cflags gnomemm'
Gnomemm_libs = '$ gnome_config -- libs gnomemm'

Ac_subst (gnomemm_cflags)
Ac_subst (gnomemm_libs)

Other libraries are similar to those used for online search.

4. The library cannot tell you anything:
In this case, you need to specify it yourself! You can use the ac_arg_with macro to provide parameters. The usage is as follows:
Assume that the header file and library file of MySQL are respectively packed in the include and Lib directories under the same directory, you can do this:
# Ask user for path to libmysqlclient stuff :.
Ac_arg_with (MySQL,
[-- With-mysql = <path> prefix of MySQL installation. e.g./usr/local or/usr],
[Mysql_prefix = $ with_mysql],
Ac_msg_error ([you must call configure with the -- with-mysql option.
This tells configure where to find the MySQL C library and headers.
E.g. -- With-mysql =/usr/local or -- With-mysql =/usr])
)

Ac_subst (mysql_prefix)
Mysql_libs = "-L $ {mysql_prefix}/lib/MySQL-lmysqlclient"
Mysql_cflags = "-I $ {mysql_prefix}/include"
Ac_subst (mysql_libs)
Ac_subst (mysql_cflags)

The above changes can meet your needs. Sometimes, you may prefer to provide two options: one -- With-headers = for providing-I, and the other -- With-libs = for providing-L.
Then, ac_msg_error is used here. If the user does not use this option, the information will be printed and the configure will be exited. You can remove it.

Then, you can see -- With-mysql in./configure -- help.

Ii. Other tests:
However, you only need to look at some of the chapters in the gnuguan autoconf.html.
For example, a macro is used to detect the existence of a lib and the existence of a function in Lib:
Ac_search_libs (SQRT, M, [Echo 'found! '], [Echo' not found! '; Exit], [])
It goes to libm (the second Parameter M indicates libm) and finds SQRT. if it finds the print found, it cannot find the print not found and exits config. if it is found,-LM will be added to libs! This libs is used by users and is similar to am_ldadd written by developers in makefile. am. Anyway, this will be added to the link of each program.
Here we show that you can use shell commands in a macro!

For example, ac_check_headers (sys/STAT. H, [], []) is used to detect the existence of the stat. h header file. If yes, in config. H, A # define have_sys_stat_h 1 is defined. Replace/with _, and add have in front. Others are similar in config. h.
There are many more, such as searching for a file in a specified path.

In any case, a specific makefile may be set. the variables used by AM (see @ in the preceding Section), and the second is in config. h. /configure -- help to check the last some influential environment variables ). This depends on the specific macro. There may be other methods.

In fact, in the early days, the problems people encountered were not the problem of finding the library in the previous article. The person who wrote configure was because he had to transplant a software on many UNIX platforms, on these platforms, there are differences in header files, underlying APIs, and C libraries. For example, if you do not have an API, do you need to consider Conditional compilation and replace it with your own alternatives, such as header files? To use an API that contains different header files, you must first use configure. macro in, and then put it in config. h, and then Conditional compilation. This type has rich macros available. Check the autoconf.html manual.

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.