Jump to content
  • Advertisement
Sign in to follow this  
Cornstalks

How do you use static libraries?

This topic is 4112 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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?

Share this post


Link to post
Share on other sites
Advertisement
You should be able to just #include your library headers, and link your
library in with the rest of the project.

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
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.cpp
Linking console executable: bin\Debug\Fractal Creator.exe
C:\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)

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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!

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!