[LINUX] Organize the super-basic usage of Autotools and pkg-config

Purpose of the article

Autotools and pkg-config will build nicely without modifying the build script even if the target environment is different. I've always used it somehow, so I'll sort out what I'm doing within my own range.

Tool overview

Autotools

Autotools Tutorial

Wikipedia Autotools Autotools is a type of tool and framework for developing software packages mainly on Unix-like operating systems (OS). This tool makes it easy to adapt packages to a wide variety of UNIX-compatible environments. Autotools mainly consists of autoconf / automake / libtools.

autoconf

Generate configure from configure.ac. The cross-compiler support is handled by configure. If you create a cross-development environment with Yocto, after source the toolchain, configure as follows. ./configure ${CONFIGURE_FLAGS}

If you look at the contents of $ {CONFIGURE_FLAGS}, you can see that it seems that you are setting environment variables for the cross compiler. echo ${CONFIGURE_FLAGS} --target=arm-poky-linux-gnueabi --host=arm-poky-linux-gnueabi --build=x86_64-linux --with-libtool-sysroot=/opt/poky/2.2+snapshot/sysroots/cortexa7hf-neon-vfpv4-poky-linux-gnueabi automake

Generate Makefile.in from Makefile.am. At the time of Makefile.in, environment-specific settings are not reflected. The final Makefile is generated by configure adding environment-specific settings to Makefile.in.

libtool

I'm not using it, so I'll omit it.

pkg-config

Wikipedia pkg-config pkg-config is a means to provide various flags, paths, etc. required when using the library with a common interface.

In other words, the library is easy to use.

sample

environment

Build & run procedure

Build

git clone https://github.com/tomoyuki-nakabayashi/autotools-hello.git
cd autotools-hello
autoreconf -fi
./configure
make

Run

./src/hello
hello world

Dive into sample

Except for using glib, I referred to "Hello, World" with GNU Autotools.

configure.ac Added a line to check in configure.ac if glib is in the build environment. PKG_CHECK_MODULES(GLIB, [glib-2.0])

PKG_CHECK_MODULES is a macro that interfaces with autoconf and pkg-config. In the first argument, specify the compile-time flag and the prefix used for library reference. In the second argument, specify the module name found in the pkg-config search path. (In my environment, the include path is "/usr/include/glib-2.0/", so "glib" is not good and you need to set it to "glib-2.0")

src/Makefile.am Add compile flags and library references using the prefix specified in PKG_CHECK_MODULES in configure.ac.

hello_CFLAGS = \
        @CFLAGS@ \
        @GLIB_CFLAGS@ 

hello_LDADD = \
        @GLIB_LIBS@ 

What does this mean? Looking at the log when make is executed, it seems that gcc is executed as ↓. gcc -g -O2 -I/usr/include/glib-2.0 -I/usr/lib/x86_64-linux-gnu/glib-2.0/include -g -O2 -o hello hello-main.o -lglib-2.0

If you make while editing Makefile.am, CFLAGS is "-g -O2" and GLIB_CFLAGS is "-I / usr / lib / x86_64-linux-gnu / glib-2.0 / include". It can be seen that ", GLIB_LIBS works with" -lglib-2.0 ". Looking at the src / Makefile, it looks like this: I see.

GLIB_CFLAGS = -I/usr/include/glib-2.0 -I/usr/lib/x86_64-linux-gnu/glib-2.0/include
GLIB_LIBS = -lglib-2.0

When using the cross-build environment created by Yocto, it is as follows. I see.

GLIB_CFLAGS = -I/opt/poky/2.2+snapshot/sysroots/cortexa7hf-neon-vfpv4-poky-linux-gnueabi/usr/include/glib-2.0 -I/opt/poky/2.2+snapshot/sysroots/cortexa7hf-neon-vfpv4-poky-linux-gnueabi/usr/lib/glib-2.0/include
GLIB_LIBS = -lglib-2.0

By the way, at the time of src / Makefile.in, it is not an environment-specific path. That's what configure reflects environment-specific settings. I see.

GLIB_CFLAGS = @GLIB_CFLAGS@
GLIB_LIBS = @GLIB_LIBS@

Summary

I knew what I was doing for a moment.

Recommended Posts

Organize the super-basic usage of Autotools and pkg-config
Organize the meaning of methods, classes and objects
Python --Explanation and usage summary of the top 24 packages
The story of Python and the story of NaN
Installation and easy usage of pytest
This and that of the inclusion notation.
[Python] Visualize the heat of Tokyo and XX prefectures (DataFrame usage memo)
Scraping the usage history of the community cycle
Review the concept and terminology of regression
The usage of TensorBoard has changed slightly
Organize the library of competitive professionals ~ Dice ~
About the behavior of copy, deepcopy and numpy.copy
Summary of the differences between PHP and Python
Full understanding of the concepts of Bellman-Ford and Dijkstra
The answer of "1/2" is different between python2 and 3
Change the color of Fabric errors and warnings
Compare the speed of Python append and map
Visualized the usage status of the sink in the company
General description of the CPUFreq core and CPUFreq notifiers
I read and implemented the Variants of UKR
About the * (asterisk) argument of python (and itertools.starmap)
A discussion of the strengths and weaknesses of Python
[Python] Class type and usage of datetime module
The nice and regrettable parts of Cloud Datalab
Consideration of the difference between ROC curve and PR curve
Get the title of yahoo news and analyze sentiment
Scraping the usage history of the community cycle PhantomJS version
The story of Python without increment and decrement operators.
Automatically determine and process the encoding of the text file
relation of the Fibonacci number series and the Golden ratio
The process of installing Atom and getting Python running
Make a note of the list of basic Pandas usage
Visualize the range of interpolation and extrapolation with python
[FSL] Measurement of segment and volume of the basal ganglia
Think about the next generation of Rack and WSGI
Referencing and changing the upper bound of Python recursion
I checked out the versions of Blender and Python
I checked the default OS and shell of docker-machine
Visualization of the connection between malware and the callback server
[Django 2.2] Sort and get the value of the relation destination
Personal notes about the integration of vscode and anaconda
[Introduction to Python] Basic usage of the library matplotlib
Roughly estimate the total memory usage of an object
Check the type and version of your Linux distribution
Animate the basics of dynamic programming and the knapsack problem