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 ...
- 02-10-2008 #1Just 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.
- 02-11-2008 #2
If you use
this means that the library must be named "libmikrofb.a" or "libmikrofb.so" and (normally) must be found in /lib or /usr/lib.Code:-lmikrofb
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.
- 02-11-2008 #3Just 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.
- 02-11-2008 #4
This
does not contain a -L switch.Code:gcc framebuffer.o fb_main.o -lstdc++ -lmikrofb -o fb_main
--
Bill
Old age and treachery will overcome youth and skill.
- 02-11-2008 #5Linux 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:
RegardsCode:g++ framebuffer.o fb_main.o -L./Lib -lmikrofb -o fb_main
- 02-11-2008 #6Just 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


Reply With Quote