Find the answer to your Linux question:
Results 1 to 8 of 8
Hi All, I am new to linux I have created a library (mylib.a) in my own directory. I want to link this library file to my test application. My test ...
  1. #1
    Just Joined!
    Join Date
    Jul 2009
    Posts
    6

    library linking in a make file

    Hi All,

    I am new to linux
    I have created a library (mylib.a) in my own directory.
    I want to link this library file to my test application. My test application resides in the same directory.
    For this i am using make file which is giving some issues.
    I would like to know how to do this.
    Below is the snippet of my make file

    #compiler
    CC = gcc
    #include files directory
    INCLUDE = .
    #options for development
    CFLAGS = -g -Wall -ansi
    mymain: main.o mylib.a
    $(CC) -o -L mymain main.o -l mylib.a
    main.o: main.c
    $(CC) -I$(INCLUDE) $(CFLAGS) -c main.c

    This make file is giving issues and not able to link and generate executable file.
    help me in correcting make file

    thanks

  2. #2
    tpl
    tpl is offline
    Linux User
    Join Date
    Jan 2007
    Location
    cleveland
    Posts
    452
    did you try something like this?

    gcc -o mymain main.c -l./mylib.a

    where that is an "el" as in "-lm"
    the sun is new every day (heraclitus)

  3. #3
    Just Joined!
    Join Date
    Jul 2009
    Posts
    6
    i tried the above option.
    but it is not fine. still there are issues

  4. #4
    Linux Guru Lakshmipathi's Avatar
    Join Date
    Sep 2006
    Location
    3rd rock from sun - Often seen near moon
    Posts
    1,568

    Exclamation

    can you post the exact error messages ?
    - Lakshmipathi.G
    -------------------
    FOSS India Award winning ext3fs Undelete tool and tutorials www.giis.co.in
    First they criticize you,Then they laugh at you,Then they fight with you,Then you win. - M.K.Gandhi
    -------------------

  5. #5
    Linux Guru Lakshmipathi's Avatar
    Join Date
    Sep 2006
    Location
    3rd rock from sun - Often seen near moon
    Posts
    1,568
    Does this help ?

    gcc -o mymain main.c -L /absolute/path/to/mylib_directory

    or

    gcc -o mymain main.c /absolute/path/to/mylib.a
    - Lakshmipathi.G
    -------------------
    FOSS India Award winning ext3fs Undelete tool and tutorials www.giis.co.in
    First they criticize you,Then they laugh at you,Then they fight with you,Then you win. - M.K.Gandhi
    -------------------

  6. #6
    Just Joined!
    Join Date
    Jul 2009
    Posts
    6
    I am listing the make file of static library created
    #compiler
    CC = gcc
    #Lib command
    LIBCC = ar
    #Lib flags
    LIBFLAG = rv #rcs
    #include files directory
    INCLUDE = .
    #options for development
    CFLAGS = -g -Wall -ansi
    libmylib.a: global.o
    $(LIBCC) -$(LIBFLAG) libmylib.a
    global.o: global.c global.h
    $(CC) -I$(INCLUDE) $(CFLAGS) -c global.c

    With the help of above make file i am able to generate libmylib.a
    in the directory /root/user1/lib_ex
    -------------------------------------------------------------------------------------
    ************************************************** ******************

    I have sample test application (main.c in /root/user1/lib_ex) which will make use of functions defined in libmylib.a

    At the command prompt, options tried are

    Option 1: command at shell
    gcc main.c -L /root/user1/lib_ex -lmylib -o main

    Error
    -------
    /tmp/ccwKhJSz.o(.text+0x2f): In function `main':
    : undefined reference to `add'
    /tmp/ccwKhJSz.o(.text+0x59): In function `main':
    : undefined reference to `sub'
    /tmp/ccwKhJSz.o(.text+0x83): In function `main':
    : undefined reference to `mul'
    collect2: ld returned 1 exit status

    Option 2:
    I tried the above options and the error is same as above

    Option 3:
    With the make file for main

    #compiler
    CC = gcc
    #include files directory
    INCLUDE = .
    #options for development
    CFLAGS = -g -Wall -ansi
    mymain: main.o
    $(CC) -o mymain main.o -L/root/shobha/lib_ex -llibmylib
    main.o: main.c
    $(CC) -I$(INCLUDE) $(CFLAGS) -c main.c

    Error
    ------------
    gcc -o mymain main.o -L/root/shobha/lib_ex -llibmylib
    /usr/bin/ld: cannot find -llibmylib
    collect2: ld returned 1 exit status
    make: *** [mymain] Error 1

    Option 4:
    With the little change in the above make file

    #compiler
    CC = gcc
    #include files directory
    INCLUDE = .
    #options for development
    CFLAGS = -g -Wall -ansi
    mymain: main.o libmylib.a
    $(CC) -o mymain main.o -L/root/shobha/lib_ex -llibmylib
    main.o: main.c
    $(CC) -I$(INCLUDE) $(CFLAGS) -c main.c

    Error
    ------------
    gcc -o mymain main.o -L/root/shobha/lib_ex -llibmylib
    /usr/bin/ld: cannot find -llibmylib
    collect2: ld returned 1 exit status
    make: *** [mymain] Error 1

    Option 5
    The change in the make file includes,
    mymain: main.o mylib.a
    $(CC) -o mymain main.o -L/root/shobha/lib_ex -lmylib

    Error is
    --------------------
    make: *** No rule to make target `mylib.a', needed by `mymain'. Stop.

  7. #7
    Linux Guru Lakshmipathi's Avatar
    Join Date
    Sep 2006
    Location
    3rd rock from sun - Often seen near moon
    Posts
    1,568
    Hi uma_iyer,
    hmm..from makefile i can't figure out the issue..it looks ok..Have you tested your source and library ,without using make file?
    Doing so will help to check whether it's Makefile problem or problem with your custom library itself.

    if you successfully created library then check its functions using nm command.
    nm libmylib.a
    Do you have all necessary functions from above output?

    On main Makefile,what's

    -llibmylib
    - Lakshmipathi.G
    -------------------
    FOSS India Award winning ext3fs Undelete tool and tutorials www.giis.co.in
    First they criticize you,Then they laugh at you,Then they fight with you,Then you win. - M.K.Gandhi
    -------------------

  8. #8
    Just Joined!
    Join Date
    Jul 2009
    Posts
    58
    I'm confused, are you trying to link it or build it statically?

    If its linked the library, than your config has to tell the binary where to find it.

    Add the custom lib to a dir where ld will look for it, or configure ld to look in your dir.

    If you're not on a Linux system, LD_CONFIG the env variable controls where binary will look for the libraries it needs.

    You can verify that the binary can find the library using ldd:

    # ldd firefox-bin
    linux-gate.so.1 => (0x0054a000)
    libpthread.so.0 => /lib/libpthread.so.0 (0x006b9000)
    libxul.so => not found
    libmozjs.so => not found
    libxpcom.so => not found
    libplds4.so => /usr/lib/libplds4.so (0x0323f000)
    libplc4.so => /usr/lib/libplc4.so (0x030bc000)
    libnspr4.so => /usr/lib/libnspr4.so (0x030c2000)
    libdl.so.2 => /lib/libdl.so.2 (0x006b3000)

    vs.

    # ldd firefox-bin
    linux-gate.so.1 => (0x0051c000)
    libpthread.so.0 => /lib/libpthread.so.0 (0x006b9000)
    libxul.so => /opt/firefox-3.0b5/libxul.so (0x006d0000)
    libmozjs.so => /opt/firefox-3.0b5/libmozjs.so (0x00110000)
    libxpcom.so => /opt/firefox-3.0b5/libxpcom.so (0x001a3000)
    libplds4.so => /opt/firefox-3.0b5/libplds4.so (0x001a7000)
    libplc4.so => /opt/firefox-3.0b5/libplc4.so (0x001aa000)
    libnspr4.so => /opt/firefox-3.0b5/libnspr4.so (0x001f9000)
    libdl.so.2 => /lib/libdl.so.2 (0x006b3000)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...