![]() |
| | #1 |
| Registered User Join Date: Jun 2006
Posts: 3
| Makefile for a library 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. |
| sirmoreno is offline | |
| | #2 |
| and the hat of vanishing Join Date: Aug 2001 Location: The edge of the known universe
Posts: 21,214
| > 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.
__________________ If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut. Up to 8Mb PlusNet broadband from only £5.99 a month! |
| Salem is offline | |
| | #3 |
| Registered User Join Date: Jun 2006
Posts: 3
| 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 |
| sirmoreno is offline | |
| | #4 |
| and the hat of vanishing Join Date: Aug 2001 Location: The edge of the known universe
Posts: 21,214
| 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
__________________ If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut. Up to 8Mb PlusNet broadband from only £5.99 a month! |
| Salem is offline | |
| | #5 |
| Registered User Join Date: Jun 2006
Posts: 3
| # 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" |
| sirmoreno is offline | |
| | #6 |
| and the hat of vanishing Join Date: Aug 2001 Location: The edge of the known universe
Posts: 21,214
| > $ 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.
__________________ If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut. Up to 8Mb PlusNet broadband from only £5.99 a month! |
| Salem is offline | |
![]() |
| Thread Tools | |
| Display Modes | |
|
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Library troubles with makefile | mslate | Linux Programming | 17 | 07-23-2009 04:43 PM |
| cannot find library in makefile | steve1_rm | C Programming | 8 | 01-28-2009 06:33 AM |
| very weird .h problem | royuco77 | C++ Programming | 1 | 09-11-2005 07:55 AM |
| Resource ICONs | gbaker | Windows Programming | 4 | 12-15-2003 07:18 AM |
| Borland 5.5 Makefile woes... | gprogga | C++ Programming | 3 | 06-01-2003 02:20 AM |