---
# System prepended metadata

title: Autotool  (`configure.ac` & `Makefile.am`)
tags: [Autotool]

---

# Autotool  (`configure.ac` & `Makefile.am`)
###### tags : `Autotool`

## Steps in order
### 0. autoscan
### 1. modify configure.scan
```shell= 
 //to create configure.scan
 $ autoscan
 // modify content and file name of configure.ac (I will introduce this later)
 $ mv configure.scan configure.ac
```
### 2. autoheader (create `config.h.in`)
### 3. aclocal (create aclocal.m4)
### 4. autoconf (`configure.ac` is necessary for autoconf)

### 3. automake --add-missing (`makefile.am` is necessary for this step)
### 4. ./configure (generate Makefile)
### 5. make

## How to weite `configure.ac`
```
#                                               -*- Autoconf -*-
# Process this file with autoconf to produce a configure script.

AC_INIT(obj_detector,1.0)
AM_INIT_AUTOMAKE(foreign)
AC_CONFIG_SRCDIR([src/main.cpp])


# Checks for programs.
AC_PROG_CXX
AC_PROG_CC

# Checks for libraries.

# Checks for header files.


# Checks for typedefs, structures, and compiler characteristics.
AC_CHECK_HEADER_STDBOOL
AC_TYPE_SIZE_T

# Checks for library functions.

# Checks for library functions.
AC_CONFIG_FILES([Makefile])
AC_CONFIG_HEADERS([config.h])
AC_OUTPUT
```
## How to write `Makefile.am`
```
#AUTOMAKE_OPTIONS = subdir-objects foreign

link = -I/usr/local/include/opencv4
pkglib = `pkg-config opencv4 --cflags --libs`

all : obj_detector

obj_detector: src/obj_extarctor.o src/main.o
	g++ src/obj_extarctor.o src/main.o $(link) $(pkglib) -O3 -o obj_extarctor
src/main.o: src/main.cpp
	g++ -c src/main.cpp $(link) $(pkglib) -O3 -o src/main.o

src/obj_extarctor.o: src/obj_extarctor.cpp src/obj_extarctor.h src/shape.h
	g++ -c src/obj_extarctor.cpp $(link) $(pkglib) -O3 -o src/obj_extarctor.o

```

## Create shell script for user
```shell=
$ vim autoconfig.sh
$ chmod +x autoconfig.sh
```
> `autoconfig.sh`
```
#!/bin/bash
autoheader
aclocal
autoconf
automake --add-missing
```
## For project author
> 1. Prepare `Makefile.am` 
> 2. Prepare `configure.ac` 
> 3. wirte `autoconfig.sh`

## For user 
```shell=
$ ./autoconfig.sh (or autoreconf -i)
$ ./configure [option]mak
$ make
```


## Backup: Examples of errors
>The wrong example of makefile (ie. undefined reference to ...)
```shell=
link = -I/usr/local/include/opencv4
pkglib = `pkg-config opencv4 --cflags --libs`

all: obj_extarctor 

# Compile error
# it need to be modify to
# g++ obj_extarctor.o main.o $(link) $(pkglib) -O3 -o obj_extarctor
obj_extarctor: obj_extarctor.o main.o
	g++ $(link) $(pkglib) -O3 -o obj_extarctor  obj_extarctor.o main.o

main.o: main.cpp
	g++ -c main.cpp $(link) $(pkglib) -O3 

obj_extarctor.o: obj_extarctor.cpp obj_extarctor.h shape.h
	g++ -c obj_extarctor.cpp $(link) $(pkglib) -O3 

clean:
	rm -f obj_extarctor *.o

```
