# How do you use static libraries?

## 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 on other sites
You should be able to just #include your library headers, and link your
library in with the rest of the project.

##### Share on other sites
Are you including both the .lib files in your linker AND the headers with #include in your code?

##### 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 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.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)

##### 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 on other sites
sounds like you're missing opengl32.lib and gdi32.lib

##### 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 on other sites
Quote:
 Original post by knallbuntsounds 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 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 on other sites
Quote:
 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?

Its just the Win32 GDI. Im not sure why you only need to link it for debug builds though..

What libraries does your release build use?

##### Share on other sites
The debug build uses the libraries:
opengl32
gdi32
ProjectEngine (a library I've made, which doesn't link to any other libraries)

The release build uses:
opengl32
ProjectEngine (the same one I made)

However, i poked around in the project options and everything (I'm new to the IDE so I'm not sure what it's doing without me knowing), and I think I found why.

Do you think that since it changes from a Console application to a GUI application that it then automatically links to the gdi32 library?

[I'm using CodeBlocks]

##### Share on other sites
Probably because Console apps dont use the gdi.
You can also try to create a new console application and a new windows
application and check the difference.

##### Share on other sites
thanks, ill look at that. I wish it told me what it was doing behind my back... that would make life easier

## Create an account or sign in to comment

You need to be a member in order to leave a comment

## Create an account

Sign up for a new account in our community. It's easy!

Register a new account

• ### Forum Statistics

• Total Topics
628357
• Total Posts
2982225

• 10
• 9
• 24
• 11
• 9