开发者

autotools for 2 programs

开发者 https://www.devze.com 2022-12-17 13:11 出处:网络
I have a program main.c which calls header.c with the help of header.h in its program. I know how to compile it in GCC but now i would like to use autotools for it. I would like to know what should be

I have a program main.c which calls header.c with the help of header.h in its program. I know how to compile it in GCC but now i would like to use autotools for it. I would like to know what should be written in 开发者_运维技巧Makefile.am to compile main.c?

so for example if i have two c files main.c and header.c as given below

main.c:-

#include<stdio.h>
#include"header.h"
int main(int argc, char* argv[])
{

   printf("\n Hello");
  function1();

   return 0;
}

and my header.c file contains

#include<stdio.h>
void function1()
{
  printf("\n Hi");
}

so my header.h file will contain

void function1();

then in this case what should be written in the makefile.am and configure.ac


Here is a minimal example of what you need for the situation that you describe.

You need a makefile.am containing the name of the binary to build, and the source files used to build it (you do not have to list header files, they will be detected automatically):

bin_PROGRAMS = example
example_SOURCES = main.c header.c

And you need a configure.ac. Here you set up the name and version number of the program, initialize Automake with the foreign argument so that it won't complain to you about missing files that the GNU project requires, tell it that you require a C compiler, tell it to build your Makefile, and finally tell it to output the results of the previous configuration.

AC_INIT([example], [1.0])
AM_INIT_AUTOMAKE([foreign])
AC_PROG_CC
AC_CONFIG_FILES([Makefile])
AC_OUTPUT

If your existing program has any kind of library dependencies, you can run autoscan to detect possible dependencies. It produces a file configure.scan, which contains a template that can be used to help build your configure.ac; but if your program is simple, you can skip this step and use the minimal example above.

Now run autoreconf --install to copy in some files that are necessary and build Makefile.in and configure from the above configuration files. Then, run ./configure to configure your script, generating a Makefile. Finally, run make to build your program.

Once you have done these steps, the Makefile that you have generated will detect changes to your makefile.am and run the steps again, so from now on, you should be able to just run make without having to go through all these steps again.

See the Automake and Autoconf manuals for more information.


A minimal Makefile.am:

SHELL = /bin/sh

prefix = /usr/local
exec_prefix = @prefix@
bindir = ${exec_prefix}/bin

AM_CFLAGS = -I./

bin_PROGRAMS = your_program_name

niue_SOURCES = main.c


install-exec-local:
    cp ./your_program_name ${bindir}

uninstall-local:
    rm ${bindir}/your_program_name

You may need a configure.ac as well:

# Process this file with autoconf to produce a configure script.

AC_PREREQ(2.59)
AC_INIT(your_program_name, 0.1, you@yourdomain.com)
AM_INIT_AUTOMAKE
AC_CONFIG_SRCDIR([main.c])
AC_CONFIG_HEADER([config.h])

# Checks for programs.
AC_PROG_CC

# Checks for additional libraries.
# AC_CHECK_LIB([pthread], [pthread_create])

# Checks for additional header files.
# AC_CHECK_HEADERS([getopt.h])

# Checks for typedefs, structures, and compiler characteristics.
AC_HEADER_STDBOOL
AC_C_CONST
AC_TYPE_SIZE_T
AC_HEADER_TIME
AC_C_VOLATILE

# Checks for library functions.
AC_HEADER_STDC
AC_FUNC_SELECT_ARGTYPES
#AC_CHECK_FUNCS([getopt_long])

AC_CONFIG_FILES([Makefile])
AC_OUTPUT


so for example if i have two c files main.c and header.c as given below

main.c:-

include

include"header.h"

int main(int argc, char* argv[]) {

printf("\n Hello"); function1();

return 0; }

and my header.c file contains

include

void function1() { printf("\n Hi"); }

so my header.h file will contain void function1();

then in this case what should be written in the makefile.am and configure.ac

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号