views:

72

answers:

3

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

A: 

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, [email protected])
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
Vijay Mathew
+4  A: 

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.

Brian Campbell
A: 

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

jinal
Hi, Jinal. You appear to be new here, so I'll give you some advice on how to use the site. A clarification like this should be done as an edit to your original question, not as an answer to your own question. Look at the bottom left of your question; you should see a small "edit" link, which you can click, and then edit your question to provide any clarifications. Also, any source code should be indented 4 spaces; this will cause it to be formatted as source code, and not interpreted as styled text. You can select an entire block of code and press the `101010` button to indent it.
Brian Campbell
I've already done the edit for you; experienced users can edit other people's questions and answers to help out with issues like this. If you don't think that your question has be adequately answered, you might also respond to let people who have answered know why their answers are not good enough or are not working for you. The answer I provided should work for the exact situation that you describe here (I even tested it with an example project), so it would help me provide you with a better answer if you could tell me what part isn't working for you, or where my answer is unclear.
Brian Campbell