Makefile for a library

This is a discussion on Makefile for a library within the Linux Programming forums, part of the Platform Specific Boards category; Hi, I want to make a library in c that any user program can include I know that the make ...

  1. #1
    Registered User
    Join Date
    Jun 2006
    Posts
    3

    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.

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,751
    > 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.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #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

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,751
    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.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #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"

  6. #6
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,751
    > $ 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.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Library troubles with makefile
    By mslate in forum Linux Programming
    Replies: 17
    Last Post: 07-23-2009, 05:43 PM
  2. cannot find library in makefile
    By steve1_rm in forum C Programming
    Replies: 8
    Last Post: 01-28-2009, 06:33 AM
  3. very weird .h problem
    By royuco77 in forum C++ Programming
    Replies: 1
    Last Post: 09-11-2005, 08:55 AM
  4. Resource ICONs
    By gbaker in forum Windows Programming
    Replies: 4
    Last Post: 12-15-2003, 07:18 AM
  5. Borland 5.5 Makefile woes...
    By gprogga in forum C++ Programming
    Replies: 3
    Last Post: 06-01-2003, 03:20 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21