-
Makefile for a library
Hi,
I want to make a library in c that any user program can include
I know that the make file of the library should be:
all: a.c b.c
gcc –c a.c –o a.o
gcc –c b.c –o b.o
ar rcs libuthreads.a a.o b.o
and the makefile of the user program is:
gcc test.c -L. -libuthreads.a
The problem:
I want the main() to be in the library - in a.c
so when i run ./test.c it will start in main() in a.c
and the main to call umain() witch is the user main
umain() is defined in a.h ( in the library) but implemented by the user test.c
when I run the library makefile all is ok
but when i run the user makefile i get a msg that umain() that is called in a.c (in main()) in not defined.
I dont know how to tell the library that umain() is defined outside of the library.
I tryed exteren but it didn't work.
and i dont want to have one make file for the library and the user files. :confused:
-
> I want the main() to be in the library - in a.c
Why do you need this?
There is only one main(), and that will be in test.c. It makes no sense for a library to have a main().
What it your project linked with two libraries, each with their own main(), what then (chaos).
If you've got some vital initialisation function which must be called first before the rest of the library will work, then create an initialise function and say that it should be called first.
-
I'm a student and it's a project i have to do
the library should implement a user threads library
and umain() should be given by the user and run as thread number 0.
in test.c (user) there is no main.
I have only one main in the library a.c .
but i dont know how to compile it so that in main() it will know that umain() is implemented outside of the library
-
Code:
$# files
$ cat *.c *.h
#include "a.h"
int main ( ) {
return umain();
}
#include <stdio.h>
#include "a.h"
int umain ( ) {
printf( "Hello from umain\n" );
return 0;
}
#ifndef A_H_INCLUDED
#define A_H_INCLUDED
int umain ( );
#endif
$# make the library
$ gcc -c a.c
$ ar rcs libfoo.a a.o
$# use the library
$ gcc test.c -L. -lfoo
$ ./a.out
Hello from umain
-
# use the library
$ gcc test.c -L. -libfoo.a <- Why just -lfoo?
./test <- Run the user file and not the lib file
Hello from umain
When i try to run the makefile of the user i get:
"umain() in a.c is unknown"
-
> $ gcc test.c -L. -libfoo.a <- Why just -lfoo?
Because the 'lib' prefix and '.a' suffix are assumed by the -l option to the linker.