Thread: Building program with shared library

  1. #1
    Registered User
    Join Date
    Jul 2021
    Posts
    3

    Building program with shared library

    Hi folks!

    I'm trying to build a (multi-file) sample program with a shared library for demonstration purposes. But unfortunately, I'm getting something wrong. Anybody here to help?

    Code:
    $ make
    gcc -Wall -g -o mylib.o -c mylib.c
    gcc -Wall -g -o libmylib.so -shared mylib.o
    gcc -Wall -g -o prog -L. -lmylib wrapLibraryFunc.c prog.c
    wrapLibraryFunc.c:4:6: warning: ‘wrapLibraryFunc’ defined but not used [-Wunused-function]
        4 | void wrapLibraryFunc(char *message)
          |      ^~~~~~~~~~~~~~~
    In file included from prog.c:1:
    prog.h:1:13: warning: ‘wrapLibraryFunc’ used but never defined
        1 | static void wrapLibraryFunc(char *message);
          |             ^~~~~~~~~~~~~~~
    /usr/bin/ld: /tmp/ccg3TTFt.o: in function `wrapLibraryFunc':
    /home/user/src/cprogramming/wrapLibraryFunc.c:6: undefined reference to `printMessage'
    /usr/bin/ld: /tmp/ccpTloxp.o: in function `main':
    /home/user/src/cprogramming/prog.c:5: undefined reference to `wrapLibraryFunc'
    collect2: error: ld returned 1 exit status
    make: *** [Makefile:11: prog] Error 1
    Attached Files Attached Files

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    The normal convention is that libraries go at the end of the command line.
    Code:
    gcc -Wall -g -o prog wrapLibraryFunc.c prog.c  -L. -lmylib
    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.

  3. #3
    Registered User
    Join Date
    Jul 2021
    Posts
    3
    Ok, didn't know that.
    But I'm still getting the same errors.

  4. #4
    Registered User
    Join Date
    Dec 2017
    Posts
    1,626
    The problem is that you make wrapLibraryFunc static in the header file.
    This causes it to not be seen outside of it's translation unit.
    Get rid of the static.
    Also, your char* parameters should really be const char *.
    A little inaccuracy saves tons of explanation. - H.H. Munro

  5. #5
    Registered User
    Join Date
    Oct 2019
    Posts
    82
    Gets me thinking of this.
    Code:
    #include <stdio.h>
    
    int main(int argc, const char **argv) 
    
    {     
         printf(" %s\n", argv[0]);
         return 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. compiling shared library
    By django in forum C Programming
    Replies: 18
    Last Post: 08-24-2011, 03:48 PM
  2. shared library for QNX
    By ReeV in forum C Programming
    Replies: 3
    Last Post: 05-06-2009, 10:58 AM
  3. Help building/linking to static library
    By Kernel Sanders in forum C++ Programming
    Replies: 19
    Last Post: 08-17-2008, 04:35 PM
  4. Building a GUI Library
    By djnorthyy in forum C++ Programming
    Replies: 3
    Last Post: 04-01-2008, 03:44 PM
  5. Shared Library
    By Kinasz in forum Linux Programming
    Replies: 5
    Last Post: 10-07-2002, 09:25 AM

Tags for this Thread