{"id":1794,"date":"2016-10-19T15:45:32","date_gmt":"2016-10-19T13:45:32","guid":{"rendered":"http:\/\/blog.rabahi.net\/?page_id=1794"},"modified":"2016-10-19T16:36:14","modified_gmt":"2016-10-19T14:36:14","slug":"automake-autoconf","status":"publish","type":"page","link":"https:\/\/blog.rabahi.net\/?page_id=1794","title":{"rendered":"automake \/ autoconf"},"content":{"rendered":"<div id=\"toc_container\" class=\"no_bullets\"><p class=\"toc_title\">Contents<\/p><ul class=\"toc_list\"><li><a href=\"#Build_Hello_World_example\"><span class=\"toc_number toc_depth_1\">1<\/span> Build Hello World example<\/a><ul><li><a href=\"#Makefileam\"><span class=\"toc_number toc_depth_2\">1.1<\/span> Makefile.am<\/a><\/li><li><a href=\"#configureac\"><span class=\"toc_number toc_depth_2\">1.2<\/span> configure.ac<\/a><\/li><li><a href=\"#helloc\"><span class=\"toc_number toc_depth_2\">1.3<\/span> hello.c<\/a><\/li><li><a href=\"#Now_build_your_program\"><span class=\"toc_number toc_depth_2\">1.4<\/span> Now build your program<\/a><\/li><\/ul><\/li><li><a href=\"#Create_library_and_test_it_using_google_test\"><span class=\"toc_number toc_depth_1\">2<\/span> Create library and test it using google test<\/a><ul><li><a href=\"#Makefileam-2\"><span class=\"toc_number toc_depth_2\">2.1<\/span> Makefile.am<\/a><\/li><li><a href=\"#configureac-2\"><span class=\"toc_number toc_depth_2\">2.2<\/span> configure.ac<\/a><\/li><li><a href=\"#helloworldh\"><span class=\"toc_number toc_depth_2\">2.3<\/span> helloworld.h<\/a><\/li><li><a href=\"#helloworldcpp\"><span class=\"toc_number toc_depth_2\">2.4<\/span> helloworld.cpp<\/a><\/li><li><a href=\"#helloworldtestcpp\"><span class=\"toc_number toc_depth_2\">2.5<\/span> helloworldtest.cpp<\/a><\/li><li><a href=\"#Now_build_your_program-2\"><span class=\"toc_number toc_depth_2\">2.6<\/span> Now build your program<\/a><\/li><li><a href=\"#Now_test_your_program\"><span class=\"toc_number toc_depth_2\">2.7<\/span> Now test your program<\/a><\/li><\/ul><\/li><li><a href=\"#Example_configureac\"><span class=\"toc_number toc_depth_1\">3<\/span> Example configure.ac<\/a><\/li><li><a href=\"#Example_Makefileam\"><span class=\"toc_number toc_depth_1\">4<\/span> Example Makefile.am<\/a><\/li><\/ul><\/div>\n<h1><span id=\"Build_Hello_World_example\">Build Hello World example<\/span><\/h1>\n<h2><span id=\"Makefileam\">Makefile.am<\/span><\/h2>\n<p>Create the Makefile.am file :<\/p>\n<pre lang=\"ini\">\r\nbin_PROGRAMS=hello\r\nhello_SOURCES=hello.c\r\n<\/pre>\n<h2><span id=\"configureac\">configure.ac<\/span><\/h2>\n<pre lang=\"ini\">\r\nAC_INIT([myhelloworldpackage], [1.0])\r\nAM_INIT_AUTOMAKE\r\nAC_PROG_CC\r\nAC_CONFIG_FILES([Makefile])\r\nAC_OUTPUT\r\n\r\n# check headers present :\r\nAC_CHECK_HEADERS([stdio.h])\r\n\r\n<\/pre>\n<h2><span id=\"helloc\">hello.c<\/span><\/h2>\n<pre lang=\"c\">\r\n#include <stdio.h>\r\nint main(int argc, char* argv[])\r\n{\r\n  printf(\"Hello, world!\\n\");\r\n  return 0;\r\n}\r\n<\/pre>\n<h2><span id=\"Now_build_your_program\">Now build your program<\/span><\/h2>\n<pre lang=\"bash\">\r\n# generate configure:\r\nautoreconf -vfi\r\n\r\n# create makefile\r\n .\/configure\r\n\r\n# build\r\nmake\r\n<\/pre>\n<h1><span id=\"Create_library_and_test_it_using_google_test\">Create library and test it using google test<\/span><\/h1>\n<h2><span id=\"Makefileam-2\">Makefile.am<\/span><\/h2>\n<pre lang=\"ini\">\r\n# library :\r\nlib_LTLIBRARIES = libhelloworld.la\r\nlibhelloworld_la_SOURCES = helloworld.cpp\r\n\r\n# tests :\r\nhelloworldtest_SOURCES = helloworldtest.cpp\r\nhelloworldtest_CXXFLAGS =-lgtest -lgtest_main -lpthread -lhelloworld\r\nhelloworldtest_CPPFLAGS=-I\/usr\/include\/c++\/4.8.2 -I\/usr\/include\/c++\/4.8.2\/x86_64-redhat-linux\r\n\r\ncheck_PROGRAMS = helloworldtest\r\nTESTS = $(check_PROGRAMS)\r\n<\/pre>\n<h2><span id=\"configureac-2\">configure.ac<\/span><\/h2>\n<pre lang=\"ini\">\r\nAC_INIT([libhelloworld], [1.0], [user@myemail.com])\r\nAM_INIT_AUTOMAKE([-Wall -Werror foreign])\r\nAC_PROG_CC\r\nAC_PROG_CXX\r\nAC_PROG_LIBTOOL\r\nAC_CONFIG_FILES([Makefile])\r\nAC_OUTPUT\r\n<\/pre>\n<h2><span id=\"helloworldh\">helloworld.h<\/span><\/h2>\n<pre lang=\"cpp\">\r\n#ifndef HELLOWORLD_H_\r\n#define HELLOWORLD_H_\r\n\r\n#include <stdio.h>\r\n#include <string>\r\n\r\nclass HelloWorld {\r\n  public :\r\n    static std::string sayHello();\r\n\r\n};\r\n\r\n\r\n#endif  \/\/ HELLOWORLD_H_\r\n<\/pre>\n<h2><span id=\"helloworldcpp\">helloworld.cpp<\/span><\/h2>\n<pre lang=\"cpp\">\r\n#ifndef HELLOWORLD_CPP_\r\n#define HELLOWORLD_CPP_\r\n\r\n#include helloworld.h\r\n\r\nstd::string HelloWorld::sayHello()\r\n{\r\n  return \"Hello World!!\";\r\n}\r\n\r\n#endif  \/\/ HELLOWORLD_CPP_\r\n<\/pre>\n<h2><span id=\"helloworldtestcpp\">helloworldtest.cpp<\/span><\/h2>\n<pre lang=\"cpp\">\r\n#ifndef HELLOWORLDTEST_CPP_\r\n#define HELLOWORLDTEST_CPP_\r\n\r\n#include <gtest\/gtest.h>\r\n#include \"helloworld.h\"\r\n\r\nTEST(HelloWorld,sayHello){\r\n  EXPECT_EQ(\"Hello World!!\",HelloWorld::sayHello());\r\n}\r\n\r\n\r\nint main(int argc, char **argv) {\r\n    testing::InitGoogleTest(&argc, argv);\r\n    return RUN_ALL_TESTS();\r\n}\r\n\r\n#endif  \/\/ HELLOWORLDTEST_CPP_\r\n<\/pre>\n<h2><span id=\"Now_build_your_program-2\">Now build your program<\/span><\/h2>\n<pre lang=\"bash\">\r\n# generate configure:\r\nautoreconf -vfi\r\n \r\n# create makefile\r\n .\/configure\r\n \r\n# build\r\nmake\r\n<\/pre>\n<h2><span id=\"Now_test_your_program\">Now test your program<\/span><\/h2>\n<pre lang=\"bash\">\r\nmake check\r\n<\/pre>\n<h1><span id=\"Example_configureac\">Example configure.ac<\/span><\/h1>\n<pre lang=\"ini\">\r\nAC_INIT([pkg-name], [1.0], [user@myemail.com])\r\nAM_INIT_AUTOMAKE([-Wall -Werror foreign])\r\nAC_PROG_CC                                  # enable build for *.c files\r\nAC_PROG_CXX                                 # enable build for *.cpp files\r\nAC_PROG_LIBTOOL                             # enable build for libraries (*.so, *.a, *.la)\r\nAC_CONFIG_FILES([Makefile])                 # Makefile in the same directory\r\nAC_CONFIG_FILES([subdir1\/Makefile])         # Makefile in the subdir1 directory\r\nAC_CONFIG_FILES([subdir2\/Makefile])         # Makefile in the subdir1 directory\r\nAC_OUTPUT\r\n\r\n\r\n####################################\r\n# AC_CHECK_FUNCS\r\n####################################\r\n# Check if some functions are presents on the system.\r\nAC_CHECK_FUNCS([pow])\r\nAC_CHECK_FUNCS([sqrt])\r\n\r\n\r\n####################################\r\n# AC_CHECK_HEADERS\r\n####################################\r\n# Check if some headers a present on the system.\r\nAC_CHECK_HEADERS([float.h])\r\nAC_CHECK_HEADERS([stdio.h])\r\n\r\n####################################\r\n# AC_CHECK_LIB\r\n####################################\r\n# Check if some libraries a present on the system.\r\n\r\nAC_CHECK_LIB([pthread], [main],[], [\r\n         echo \"pthread devel library is missing. pthread is required for this program\"\r\n         exit -1])\r\n\r\nAC_CHECK_LIB([gtest], [main],[], [\r\n         echo \"gtest devel library is missing. gtest is required for this program\"\r\n         exit -1])\r\n\r\n<\/pre>\n<h1><span id=\"Example_Makefileam\">Example Makefile.am<\/span><\/h1>\n<pre lang=\"ini\">\r\n\r\n####################################\r\n#    BIN \/ LIB DEFINITION\r\n####################################\r\n\r\n# set binaries to build:\r\nbin_PROGRAMS = \\\r\n               myprog1 \\\r\n               myprog2\r\n\r\n# sources files for myprog1:\r\nmyprog1_SOURCES = \\\r\n               src1.cpp \\\r\n               src2.cpp\r\n\r\n# sources files for myprog2:\r\nmyprog2_SOURCES = \\\r\n               src3.cpp \\\r\n               src4.cpp\r\n\r\n# set libraries to build :\r\nlib_LTLIBRARIES = libMYLIB1.la libMYLIB2.la\r\n\r\n# sources files for myprog1:\r\nlibMYLIB1_la_SOURCES = $(myprog1_SOURCES)\r\n\r\n# sources files for myprog2:\r\nlibMYLIB2_la_SOURCES = $(myprog2_SOURCES)\r\n\r\n####################################\r\n#    FLAGS\r\n####################################\r\n\r\n# 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. :\r\n# you can set specific C preprocessor. example : myprog1_CPPFLAGS=$(AM_CPPFLAGS) -Dprog1\r\nAM_CPPFLAGS = \\\r\n  -I src\/include\/path1 \\\r\n  -I src\/include\/path2 \\\r\n  -Ddef1 \\\r\n  -Ddef2\r\n\r\n#  additional C compiler flags\r\n# you can set specific C compiler flags. example : myprog1_CFLAGS=$(AM_CFLAGS) --static\r\nAM_CFLAGS = \\\r\n      -O2 \\\r\n      -march=x86_64\r\n\r\n\r\n# additional linker flags :\r\n# you can set specific linker flags. example : myprog1_LDFLAGS=$(AM_LDFLAGS) -lpthead\r\nAM_LDFLAGS = \\\r\n  -l lMYLIBNAME1 \\ \r\n  -l lMYLIBNAME2\r\n\r\n# C++ Preprocessor\r\n# you can set specific C++ preprocessor. example : myprog1_CXXFLAGS=$(AM_CXXFLAGS) -Dprog1\r\nAM_CXXFLAGS = \\\r\n  -I src\/include\/path1 \\\r\n  -I src\/include\/path2 \\\r\n  -Ddef1 \\\r\n  -Ddef2\r\n\r\n####################################\r\n#    TESTING\r\n####################################\r\n\r\ncheck_PROGRAMS = progtest1 progtest2\r\nTESTS = $(check_PROGRAMS)\r\n\r\n# sources for progtest1:\r\nprogtest1_SOURCES = progtest1.cpp\r\n# can also define specific cflags, ldflags, cppflags, cxxflags.\r\n\r\n# sources for progtest2:\r\n# can also define specific cflags, ldflags, cppflags, cxxflags.\r\nprogtest1_SOURCES = progtest2.cpp\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Contents1 Build Hello World example1.1 Makefile.am1.2 configure.ac1.3 hello.c1.4 Now build your program2 Create library and test it using google test2.1 Makefile.am2.2 configure.ac2.3 helloworld.h2.4 helloworld.cpp2.5 helloworldtest.cpp2.6 Now build your program2.7 Now test your program3 Example configure.ac4 Example Makefile.am 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]) [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":443,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-1794","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/blog.rabahi.net\/index.php?rest_route=\/wp\/v2\/pages\/1794","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.rabahi.net\/index.php?rest_route=\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/blog.rabahi.net\/index.php?rest_route=\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/blog.rabahi.net\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.rabahi.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1794"}],"version-history":[{"count":11,"href":"https:\/\/blog.rabahi.net\/index.php?rest_route=\/wp\/v2\/pages\/1794\/revisions"}],"predecessor-version":[{"id":1805,"href":"https:\/\/blog.rabahi.net\/index.php?rest_route=\/wp\/v2\/pages\/1794\/revisions\/1805"}],"up":[{"embeddable":true,"href":"https:\/\/blog.rabahi.net\/index.php?rest_route=\/wp\/v2\/pages\/443"}],"wp:attachment":[{"href":"https:\/\/blog.rabahi.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1794"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}