Thread: Dinamic Link

  1. #1
    Registered User PutoAmo's Avatar
    Join Date
    Mar 2002
    Posts
    72

    Smile Dynamic Link

    Hello all.

    I just have a simple question. How do I compile a function library to be accessed dinamically?

    Let's say I have these 3 files:

    main.c
    Code:
    #include "mylib.h"
    
    int main( void )
    {
     int a = 2;
     int b = 3;
    
     sum( a, b );
     div( a, b );
    
     return 0;
    }
    mylib.c
    Code:
    #include "mylib.h"
    
    void sum( int a, int b )
    {
     a + b;
    
     return;
    }
    
    void div( int a, int b )
    {
     if ( b != 0 )
      a / b;
    
     return;
    }
    mylib.h
    Code:
    void sum( int, int );
    void div( int, int );
    What would the gcc command look like if I just wished the library to be dinamically linked.

    Thanks in advance !
    Last edited by PutoAmo; 10-21-2002 at 05:43 AM.

  2. #2
    xlord
    Guest
    As far as linking.. from what i know.. you can use..
    cc blah.c blah.c blah.o.... read up on the cc manuals... like the man page or something

  3. #3
    Registered User PutoAmo's Avatar
    Join Date
    Mar 2002
    Posts
    72
    Thank you, xlord.

    Could you, please, use the filenames I have given as examples instead of blah-files?

  4. #4
    xlord
    Guest
    Code:
     cc -c main.c               /* creates file1.o */
     cc -c mylib.c               /* creates file2.o */
    
    and for the header file...
    
     cc -I$PATH/include/mylib.h       /* add the directory with -I */
    if im not mistaking...( lets hope im not hehe| and this is what you want..)

  5. #5
    Registered User
    Join Date
    Dec 2001
    Posts
    227
    btw.... cc -o run main.o mylib.o
    will create the executable program, run

    good luck
    Only the strong survives.

  6. #6
    Registered User PutoAmo's Avatar
    Join Date
    Mar 2002
    Posts
    72
    Would that make mylib.c a dynamic library?
    Will my main program and any other program using that library just load those functions needed?

  7. #7
    Registered User
    Join Date
    Aug 2002
    Posts
    351
    i think thre's a difference between static links and dynamic library links.

    for example:

    Code:
    gcc main.c mylib.c -o output
    will compile all the code staticly into one output file.

    i would presume dynamic library links occur mid execution - is this right?

  8. #8
    Registered User PutoAmo's Avatar
    Join Date
    Mar 2002
    Posts
    72
    That's what I also wonder about. I want mylib.c to become a dynamically linked library so I can use it with any program just loading those functions needed, when needed.Instead of linking the whole mylib.c into the executable file.

  9. #9
    Registered User
    Join Date
    Aug 2002
    Posts
    351

  10. #10
    Registered User
    Join Date
    Aug 2002
    Posts
    351
    also this

    http://www.mcsr.olemiss.edu/cgi-bin/man-cgi?dlopen+3

    seems like dlopen is a function that can be used to link to libraries at run time.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Linked List, Please Help!
    By CodeMonkeyZ in forum C Programming
    Replies: 5
    Last Post: 02-17-2009, 06:23 AM
  2. I'm confused about link lists (again)
    By JFonseka in forum C Programming
    Replies: 4
    Last Post: 06-13-2008, 08:13 PM
  3. Function to check memory left from malloc and free?
    By Lechx in forum C Programming
    Replies: 4
    Last Post: 04-24-2006, 05:45 AM
  4. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  5. Undefined Structure in Link List
    By _Cl0wn_ in forum C Programming
    Replies: 1
    Last Post: 03-22-2003, 05:57 PM