How do you use static libraries?

Started by
12 comments, last by Cornstalks 17 years, 1 month ago
First, I'm using C++ and I switch between Dev-C++ and Code::Blocks, although I think I'm starting to like Code::Blocks more the more I use it. I use them to create a static library (.lib) and then I create a project that uses the library. Now for the question, I've made a library like thing that is filled with classes and their member functions. But whenever I try to create a project that uses the .lib there are always linker errors that say functions aren't defined that have been defined in the library project. How do I successfully create a static library project, and then how do I successfully use that library in another project?
[size=2][ I was ninja'd 71 times before I stopped counting a long time ago ] [ f.k.a. MikeTacular ] [ My Blog ] [ SWFer: Gaplessly looped MP3s in your Flash games ]
Advertisement
You should be able to just #include your library headers, and link your
library in with the rest of the project.
Are you including both the .lib files in your linker AND the headers with #include in your code?
Comrade, Listen! The Glorious Commonwealth's first Airship has been compromised! Who is the saboteur? Who can be saved? Uncover what the passengers are hiding and write the grisly conclusion of its final hours in an open-ended, player-driven adventure. Dziekujemy! -- Karaski: What Goes Up...
Do you know how to tell the linker to use your library?

In MS Visual C++, just put
#pragma comment(lib, "yourlib.lib")
somewhere at the top of your code.

Or go to [Your Project]>Properties>Linker>Additional Dependencies and stick the name of your library in the top entry.

If you already knew that, sorry for assuming you didn't. I'm still a noob, but I can fake sounding experienced pretty well, so maybe you weren't as experienced as you sounded...[grin]
Yes, I include all the headers in my code, and I also add the .lib in the build options for the project. And it will try to link but something goes wrong. Here is the list of errors it gives:

Compiling: main.cppLinking console executable: bin\Debug\Fractal Creator.exeC:\Documents and Settings\Michael\My Documents\C++\Fractal Creator\libProjectEngine.a(RenderWindow.o)(.text+0xfbf):RenderWindow.cpp: undefined reference to `GetStockObject@4'C:\Documents and Settings\Michael\My Documents\C++\Fractal Creator\libProjectEngine.a(RenderWindow.o)(.text+0x22b4):RenderWindow.cpp: undefined reference to `ChoosePixelFormat@8'C:\Documents and Settings\Michael\My Documents\C++\Fractal Creator\libProjectEngine.a(RenderWindow.o)(.text+0x260c):RenderWindow.cpp: undefined reference to `SetPixelFormat@12'C:\Documents and Settings\Michael\My Documents\C++\Fractal Creator\libProjectEngine.a(RenderWindow.o)(.text+0x42de):RenderWindow.cpp: undefined reference to `SwapBuffers@4'

This leads me to believe that the error comes from the .lib, but I don't know. The file RenderWindow.cpp is a part of the .lib, and I don't get any errors when I compile it. I'm guessing it's not linking correctly to some win32 library that is needed, but I don't know how to fix this.

EDIT: Hehe sorry I keep saying the library is a .lib file, but it's actually a .a file (but it's still the same thing, just a different extension)
[size=2][ I was ninja'd 71 times before I stopped counting a long time ago ] [ f.k.a. MikeTacular ] [ My Blog ] [ SWFer: Gaplessly looped MP3s in your Flash games ]
Do you think it would be because of Debug/Release compiling settings? I noticed the main project (the one useing the library) compiles and runs fine if I have it set as a Release build. But if I set it as a Debug build then I get the errors. It doesn't matter what the library build is, both the Release and Debug work. Why won't the main project work if it is a Debug build?
[size=2][ I was ninja'd 71 times before I stopped counting a long time ago ] [ f.k.a. MikeTacular ] [ My Blog ] [ SWFer: Gaplessly looped MP3s in your Flash games ]
sounds like you're missing opengl32.lib and gdi32.lib
Might be the naming of the function in the exported function list inside the a.
The compiler is searching for a name signature that doesn't exist int the lib file. So my guess is to try to play with the linker options.

Cheers.
Quote:Original post by knallbunt
sounds like you're missing opengl32.lib and gdi32.lib

I've linked to the opengl32.lib, I've already set that. But I have never heard of the gdi32.lib so when I added that to the project options, it worked! I don't know why I'd need to add it to the Debug build and not the Release build though. Can someone please expand on this? And thanks a bunch!
[size=2][ I was ninja'd 71 times before I stopped counting a long time ago ] [ f.k.a. MikeTacular ] [ My Blog ] [ SWFer: Gaplessly looped MP3s in your Flash games ]
Does anyone know what the gdi32.lib library is and why I only need to include it in Debug builds and not in Release builds?
[size=2][ I was ninja'd 71 times before I stopped counting a long time ago ] [ f.k.a. MikeTacular ] [ My Blog ] [ SWFer: Gaplessly looped MP3s in your Flash games ]

This topic is closed to new replies.

Advertisement