My linux world » automake / autoconf

automake / autoconf


Contents

Build Hello World example

Makefile.am

Create the Makefile.am file :

bin_PROGRAMS=hello
hello_SOURCES=hello.c

configure.ac

AC_INIT([myhelloworldpackage], [1.0])
AM_INIT_AUTOMAKE
AC_PROG_CC
AC_CONFIG_FILES([Makefile])
AC_OUTPUT
 
# check headers present :
AC_CHECK_HEADERS([stdio.h])

hello.c

#include <stdio.h>
int main(int argc, char* argv[])
{
  printf("Hello, world!\n");
  return 0;
}

Now build your program

# generate configure:
autoreconf -vfi
 
# create makefile
 ./configure
 
# build
make

Create library and test it using google test

Makefile.am

# library :
lib_LTLIBRARIES = libhelloworld.la
libhelloworld_la_SOURCES = helloworld.cpp
 
# tests :
helloworldtest_SOURCES = helloworldtest.cpp
helloworldtest_CXXFLAGS =-lgtest -lgtest_main -lpthread -lhelloworld
helloworldtest_CPPFLAGS=-I/usr/include/c++/4.8.2 -I/usr/include/c++/4.8.2/x86_64-redhat-linux
 
check_PROGRAMS = helloworldtest
TESTS = $(check_PROGRAMS)

configure.ac

AC_INIT([libhelloworld], [1.0], [user@myemail.com])
AM_INIT_AUTOMAKE([-Wall -Werror foreign])
AC_PROG_CC
AC_PROG_CXX
AC_PROG_LIBTOOL
AC_CONFIG_FILES([Makefile])
AC_OUTPUT

helloworld.h

#ifndef HELLOWORLD_H_
#define HELLOWORLD_H_
 
#include <stdio.h>
#include <string>
 
class HelloWorld {
  public :
    static std::string sayHello();
 
};
 
 
#endif  // HELLOWORLD_H_

helloworld.cpp

#ifndef HELLOWORLD_CPP_
#define HELLOWORLD_CPP_
 
#include helloworld.h
 
std::string HelloWorld::sayHello()
{
  return "Hello World!!";
}
 
#endif  // HELLOWORLD_CPP_

helloworldtest.cpp

#ifndef HELLOWORLDTEST_CPP_
#define HELLOWORLDTEST_CPP_
 
#include <gtest/gtest.h>
#include "helloworld.h"
 
TEST(HelloWorld,sayHello){
  EXPECT_EQ("Hello World!!",HelloWorld::sayHello());
}
 
 
int main(int argc, char **argv) {
    testing::InitGoogleTest(&argc, argv);
    return RUN_ALL_TESTS();
}
 
#endif  // HELLOWORLDTEST_CPP_

Now build your program

# generate configure:
autoreconf -vfi
 
# create makefile
 ./configure
 
# build
make

Now test your program

make check

Example configure.ac

AC_INIT([pkg-name], [1.0], [user@myemail.com])
AM_INIT_AUTOMAKE([-Wall -Werror foreign])
AC_PROG_CC                                  # enable build for *.c files
AC_PROG_CXX                                 # enable build for *.cpp files
AC_PROG_LIBTOOL                             # enable build for libraries (*.so, *.a, *.la)
AC_CONFIG_FILES([Makefile])                 # Makefile in the same directory
AC_CONFIG_FILES([subdir1/Makefile])         # Makefile in the subdir1 directory
AC_CONFIG_FILES([subdir2/Makefile])         # Makefile in the subdir1 directory
AC_OUTPUT
 
 
####################################
# AC_CHECK_FUNCS
####################################
# Check if some functions are presents on the system.
AC_CHECK_FUNCS([pow])
AC_CHECK_FUNCS([sqrt])
 
 
####################################
# AC_CHECK_HEADERS
####################################
# Check if some headers a present on the system.
AC_CHECK_HEADERS([float.h])
AC_CHECK_HEADERS([stdio.h])
 
####################################
# AC_CHECK_LIB
####################################
# Check if some libraries a present on the system.
 
AC_CHECK_LIB([pthread], [main],[], [
         echo "pthread devel library is missing. pthread is required for this program"
         exit -1])
 
AC_CHECK_LIB([gtest], [main],[], [
         echo "gtest devel library is missing. gtest is required for this program"
         exit -1])

Example Makefile.am

 
####################################
#    BIN / LIB DEFINITION
####################################
 
# set binaries to build:
bin_PROGRAMS = \
               myprog1 \
               myprog2
 
# sources files for myprog1:
myprog1_SOURCES = \
               src1.cpp \
               src2.cpp
 
# sources files for myprog2:
myprog2_SOURCES = \
               src3.cpp \
               src4.cpp
 
# set libraries to build :
lib_LTLIBRARIES = libMYLIB1.la libMYLIB2.la
 
# sources files for myprog1:
libMYLIB1_la_SOURCES = $(myprog1_SOURCES)
 
# sources files for myprog2:
libMYLIB2_la_SOURCES = $(myprog2_SOURCES)
 
####################################
#    FLAGS
####################################
 
# The contents of this variable are passed to every compilation that invokes the C preprocessor; it is a list of arguments to the preprocessor. For instance, -I and -D options should be listed here. :
# you can set specific C preprocessor. example : myprog1_CPPFLAGS=$(AM_CPPFLAGS) -Dprog1
AM_CPPFLAGS = \
  -I src/include/path1 \
  -I src/include/path2 \
  -Ddef1 \
  -Ddef2
 
#  additional C compiler flags
# you can set specific C compiler flags. example : myprog1_CFLAGS=$(AM_CFLAGS) --static
AM_CFLAGS = \
      -O2 \
      -march=x86_64
 
 
# additional linker flags :
# you can set specific linker flags. example : myprog1_LDFLAGS=$(AM_LDFLAGS) -lpthead
AM_LDFLAGS = \
  -l lMYLIBNAME1 \ 
  -l lMYLIBNAME2
 
# C++ Preprocessor
# you can set specific C++ preprocessor. example : myprog1_CXXFLAGS=$(AM_CXXFLAGS) -Dprog1
AM_CXXFLAGS = \
  -I src/include/path1 \
  -I src/include/path2 \
  -Ddef1 \
  -Ddef2
 
####################################
#    TESTING
####################################
 
check_PROGRAMS = progtest1 progtest2
TESTS = $(check_PROGRAMS)
 
# sources for progtest1:
progtest1_SOURCES = progtest1.cpp
# can also define specific cflags, ldflags, cppflags, cxxflags.
 
# sources for progtest2:
# can also define specific cflags, ldflags, cppflags, cxxflags.
progtest1_SOURCES = progtest2.cpp

Copyright © 2024 My linux world - by Marc RABAHI
Design by Marc RABAHI and encelades.