Question

I’m trying to create ./configure + make set for building C codes in following structure by using autotools. drive.c uses function in mylib.c

[mylib]
 +mylib.c
 +mylib.h
[src]
 +drive.c

More details are here.

[mylib.c]

#include <stdio.h>
#include "mylib.h"

int main(){
    mylib();
    return 0;
}
void
mylib(void)
{
  printf ("Hello world! I AM mylib \n");
}

[mylib.h]

void mylib(void);

[drive.c]

#include <mylib.h>

int
main (int argc, char **argv)
{
  mylib();
  return 0;
}

Actually I’ve given main() both mylib.c and drive.c.

If I make them on CentOS process is noremally finished however If I make them on MINGW an error message multiple definition ofmain'` is shown

How can I make them on MINGW even if they have multiply have main()?

And those followings are config files for autotools.

[confiugre.ac]

AC_PREREQ([2.69])
AC_INIT([libmylib], [1], [admin@localhost])
AC_CONFIG_SRCDIR([mylib/mylib.c])
AC_CONFIG_HEADERS([config.h])
AC_CONFIG_MACRO_DIR([m4])
AM_INIT_AUTOMAKE([foreign])
LT_INIT
AC_PROG_CC
AC_CONFIG_FILES([mylib/Makefile
                 src/Makefile
                 Makefile])
AC_OUTPUT

[Makefile.am]

SUBDIRS = mylib src
ACLOCAL_AMFLAGS = -I m4

[Makefile.am@src]

bin_PROGRAMS = drive
drive_SOURCES = drive.c
LDADD = ../mylib/libmylib.la
AM_CPPFLAGS = -I../mylib

[Makefile.am@mylib]

lib_LTLIBRARIES = libmylib.la
libmylib_la_SOURCES = mylib.c
include_HEADERS = mylib.h
Was it helpful?

Solution

You are confusing things, the idea of having multiple main() is fundamentally wrong. Libraries never ever contain a main() function.

(With the exception of Windows DLLs that contain a DllMain, but that means something different.)

If you want to provide a test case for your library, you make the test case as a separate project which includes the library. The test code should not be inside the library itself, neither should main().

Also, I very much doubt you are able to build a program with several function definitions that have the same name, be it main() or something else. If you believe you have managed this, I would either suspect that you haven't linked the files correctly, or that the linker is crap.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top