Find the answer to your Linux question:
Results 1 to 6 of 6
Hi I am trying to link with a c library, that I have written, against my c++ application. In the h-file for the c library I have included the extern ...
  1. #1
    Just Joined!
    Join Date
    Feb 2008
    Posts
    3

    How to link c library in to a c++ application?

    Hi

    I am trying to link with a c library, that I have written, against my c++ application.
    In the h-file for the c library I have included the extern declaration

    #ifndef MIKRO_FB_LIB_H
    #define MIKRO_FB_LIB_H

    #ifdef __cplusplus
    extern "C" {
    #endif

    #include <stdlib.h>
    .....
    ..
    .....
    ...

    #ifdef __cplusplus
    }
    #endif

    #endif /* MIKRO_FB_LIB_H */

    I then compile with the commands

    g++ -c -Wall -L./lib -I./include framebuffer.cc -o framebuffer.o
    g++ -c -Wall -L./lib -I./include fb_main.cc -o fb_main.o

    and link with the command
    gcc framebuffer.o fb_main.o -lstdc++ -lmikrofb -o fb_main

    the error I get is

    /usr/bin/ld: cannot find -lmikrofb
    collect2: ld returned 1 exit status
    make: *** [fb_main] Error 1

    I have managed to link the library with a c application so it believe the library is correct. Any ideas of how I should solve this.

  2. #2
    Linux Engineer wje_lf's Avatar
    Join Date
    Sep 2007
    Location
    Mariposa
    Posts
    1,192
    If you use
    Code:
    -lmikrofb
    this means that the library must be named "libmikrofb.a" or "libmikrofb.so" and (normally) must be found in /lib or /usr/lib.

    I'm guessing that this situation doesn't apply. Tell me more about mikrofb. Was it compiled in one compilation, so it's a .o file? Is it sitting in the same directory in which you're compiling? What is the full name of that file?
    --
    Bill

    Old age and treachery will overcome youth and skill.

  3. #3
    Just Joined!
    Join Date
    Feb 2008
    Posts
    3
    hi wje_lf,
    and thanks for your replay.
    The library that I have created is called libmikrofb I have created both .a and .so library I have put the library in a locale dir called lib, in the development dir, that is way I am using the flag -L./lib. I have managed to link a c application with libmikrofb so I think my problem is how to link the c library with my c++ application. I have also tried to link agaist the .o files instead of the library but that didnot work so my conclusion is that my problem is the linking of c code to a c++ application.

  4. #4
    Linux Engineer wje_lf's Avatar
    Join Date
    Sep 2007
    Location
    Mariposa
    Posts
    1,192
    This
    Code:
    gcc framebuffer.o fb_main.o -lstdc++ -lmikrofb -o fb_main
    does not contain a -L switch.
    --
    Bill

    Old age and treachery will overcome youth and skill.

  5. #5
    Linux Enthusiast
    Join Date
    Aug 2006
    Posts
    631
    You should always use g++ to link a program that contains C++ code, even if it also contains C code, try something like:

    Code:
    g++ framebuffer.o fb_main.o -L./Lib -lmikrofb -o fb_main
    Regards

  6. #6
    Just Joined!
    Join Date
    Feb 2008
    Posts
    3
    so I tried this and it worked

    g++ -c -Wall -L./lib -I./include framebuffer.cc -o framebuffer.o
    g++ -c -Wall -L./lib -I./include fb_main.cc -o fb_main.o
    g++ -L./lib -lmikrofb framebuffer.o fb_main.o -o fb_main


    Thanks for your help

Posting Permissions

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