CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 11 of 11
  1. #1
    Join Date
    Sep 2003
    Posts
    815

    question about lib

    Hello,

    Can someone please explain to me what is the meaning of a library (*.lib) application?

    How do I make one? does it have a main?
    what is the entry point, how do I call a functions that are inside this lib

    Thanks
    avi123

  2. #2
    Join Date
    Oct 2003
    Location
    Pacific North West
    Posts
    1

    lib

    Hi avi123,

    I typically use libraries for separating certain functionality of my application without cluttering things up within the main source code. I have libraries for stricly custom GUI's as well as communication. Considering I keep the libraries resident in memory during the application, so I don't bother using the dynamic version of my libraries (.dll), and having to load them when needed.

    My libraries are typically a bunch of classes that can work in conjunction with any other library or application I maybe using at the time. The library (.lib) makes things nice when I move onto a different project, and don't want to rewrite everything. I simply link in the .lib, and make the calls.

    There is no need for an entry point, similar to a .dll.

    In order to use a library, or the functionality,all you need to do is link the .lib with your main application or code base.

    Once you have linked the library, you can simply make function calls or direct access like you would anything else, granted you've included your #include<> directive.

    Hope that helps.
    nosplinters

  3. #3
    Join Date
    Jul 2001
    Location
    Netherlands
    Posts
    751
    Libraries can be loaded at runtime with LoadLibrary in windows or dlopen on linux.
    exported functions can then be reached by loading them with GetProcAddress on windows and dlsym on linux.
    They can have an unlimited number of entry points where executables only have one (main).
    It's pretty easy to write libraries that export entire objects.
    On linux libraries usually have a .so extension and are compiled with the -shared flag.
    you export functions by enclosing them with extern "C"{} tags.
    There's plenty of examples in the linux source to show you the way.

  4. #4
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    12
    Hi All

    I facing some difficulties with my application regarding implicitly and explicitly loading dynamic lib, hope you guys can help...

    1. How can i implicitly link the xxxx.lib written in VC++ into Borland C++. My applicaiton in Borland C just couldnt link the xxxx.lib written in VC++, can anyone tell me what is the problem.

    2. Because of this reason, i resort to explicitly load the dynamic DLL on run time via calling the API eg. loadlibrary("Xxxx.dll) , instead of linking the xxxx.lib after compilation.

    By this way, i can call the library function written in my VC++ from Borland C++. However, i find this method is kind of troublesome and somehow do conflict with my other applicaiton in run time.

    Is it possible to link xxxx.lib written in different C++ compiler, suchas Borland and VC? Thanks


    BestRegards,
    james goh

  5. #5
    Join Date
    May 2000
    Location
    KY, USA
    Posts
    18,652
    Originally posted by James Goh
    Is it possible to link xxxx.lib written in different C++ compiler, suchas Borland and VC? Thanks
    Take a look at this thread...

  6. #6
    Join Date
    Aug 2003
    Location
    Greece
    Posts
    206
    Well, your problem is called "name mangling". Every compiler in order to provide unique names to the various symbols (variables, classes, functions, enums etc.) within an object file, renames the symbol in a type-specific way, producing this way unique names for symbols of the same type. This allows you to use the name "foo" for both a method and a variable. These mangled names are used for resolving symbols during the linking phase - static or dynamic.

    Attempting to export and then link / locate a symbol under the same compiler environment is no problem since the same mangling rules are used. In your case the Borland compiler is searching for the specified symbol using a different mangling scheme compared to this used during the library's compilation - performed by the VC compiler.

    The link provided by Andreas provides various solutions to your problem. The .def file is a set of directives to the compiler in order to avoid using its internal mangling scheme and use the supplied name for certain symbols - making it possible to locate them through another compilation enviroment.

    Regards,

    --harizak

  7. #7
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    12
    Originally posted by harizak
    Well, your problem is called "name mangling". Every compiler in order to provide unique names to the various symbols (variables, classes, functions, enums etc.) within an object file, renames the symbol in a type-specific way, producing this way unique names for symbols of the same type. This allows you to use the name "foo" for both a method and a variable. These mangled names are used for resolving symbols during the linking phase - static or dynamic.

    Attempting to export and then link / locate a symbol under the same compiler environment is no problem since the same mangling rules are used. In your case the Borland compiler is searching for the specified symbol using a different mangling scheme compared to this used during the library's compilation - performed by the VC compiler.

    The link provided by Andreas provides various solutions to your problem. The .def file is a set of directives to the compiler in order to avoid using its internal mangling scheme and use the supplied name for certain symbols - making it possible to locate them through another compilation enviroment.

    Regards,

    --harizak

    Thanks alot for reading through and spending times answering to my doubts. I really appreciate ur expanation and will try out the mentioned method . Have a Good Day!

    BestRegards,
    James Goh

  8. #8
    Join Date
    Sep 2003
    Posts
    815
    nosplinters

    How do I link the .lib with my main?

    Thanks

  9. #9
    Join Date
    Apr 2000
    Location
    Frederick, Maryland
    Posts
    507
    In VC++ you can do it in 3 ways.

    1. Using IDE, write the name of lib file in project setting link tab.
    2. give the lib file name to linker when invoke compiler and linker at command prompt like

    link glut32.lib, Opengl32.lib, glu32.lib MyProj.obj

    3. Use pragma in you code like

    Code:
    #pragma comment (lib, "glut32.lib")
    #pragma comment (lib, "Opengl32.lib")
    #pragma comment (lib, "glu32.lib")
    I prefer the 3rd option.

    Hope it helps.

  10. #10
    Join Date
    Sep 2003
    Posts
    815
    if I use option 3:
    where is glut32.lib should be located (where should I put my lib)

    about option 1:
    what is IDE?
    Where can I found the project properties (I'm using VC .NET)

    about option 2:
    give the lib file name to linker when invoke compiler and linker at command prompt like

    How do I do that:
    I mean how do I give the linker the file name, how do I invoke compiler/linker?

    Thanks

  11. #11
    Join Date
    Jul 2003
    Location
    Maryland
    Posts
    762
    an IDE is a package that includes a compiler, editor as well as anything else into 1 application.

    VisualC++ is an IDE because it included a compiler AND editor.

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured