# static lib

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

## Recommended Posts

Hello. I have created a project that consists of several header and source files. I want to create a static library out of those files that are used in the project. How can I do that? (VC++ 2008 EE)

##### Share on other sites
Here's an article: Visual C++ Walkthrough: Creating and Using a Static Library

The core point: instead of making an .exe, you alter the project properties so that it builds a .lib. Then you link against that library in the .exe project.

##### Share on other sites
I changed the project properties so that it creates a static lib but when I build I get :

##### Share on other sites
This is probably because you are trying to debug the program, how are you building? If you a just hitting F5 then this will be why, you can use F7 to build without debugging.

##### Share on other sites
Quote:
 Original post by _moagstar_This is probably because you are trying to debug the program, how are you building? If you a just hitting F5 then this will be why, you can use F7 to build without debugging.

Yeah I was doing that, Thanks :)

Does the file that includes "main" have to be included?

##### Share on other sites
Quote:
 Original post by sheep19Does the file that includes "main" have to be included?

No, a static lib doesn't have an entry point so you don't need to include that file for compilation. HTH

##### Share on other sites
Thanks for the help!

I have another question. My library uses SDL and boost. Will I have to include SDL and boost to use my .lib? Or everything will be built in the .lib file?

EDIT:

Ok I created the .lib file. Its size is 5116 KB :|
I also created a new project, put the lib file in the linker's input and tried to compile. I get:

1>------ Build started: Project: Reach The Summit!, Configuration: Release Win32 ------
1>MSVCRT.lib(crtexew.obj) : error LNK2001: unresolved external symbol _WinMain@16
1>G:\Minas\Documents\Visual Studio 2008\Projects\Reach The Summit!\Release\Reach The Summit!.exe : fatal error LNK1120: 1 unresolved externals
1>Build log was saved at "file://g:\Minas\Documents\Visual Studio 2008\Projects\Reach The Summit!\Reach The Summit!\Release\BuildLog.htm"
1>Reach The Summit! - 2 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

What's wrong?

That's the code by the way:
#include "Min_SpriteLib.h"int main(int argc, char *args[]){	return 0;}

[Edited by - sheep19 on June 22, 2009 9:23:44 AM]

##### Share on other sites
Quote:
 Original post by sheep19I have another question. My library uses SDL and boost. Will I have to include SDL and boost to use my .lib? Or everything will be built in the .lib file?

I would leave it so that any exe that links with your .lib also has to link with sdl and the boost libs you are using.

##### Share on other sites
Quote:
 Original post by sheep19What's wrong?

Looks like you're trying to build a Win32 application, is this definately the correct setting? Or should you be building a console application? If you are building a Win32 application, then your main function should be a WinMain function.

##### Share on other sites
Quote:
Original post by _moagstar_
Quote:
 Original post by sheep19I have another question. My library uses SDL and boost. Will I have to include SDL and boost to use my .lib? Or everything will be built in the .lib file?

I would leave it so that any exe that links with your .lib also has to link with sdl and the boost libs you are using.

How can I leave it the way you are suggesting? (or maybe the programmer who uses it only have to include SDL and not boost)

Is there a way to choose when I build the library if it is going to be a console or a windows app?

[Edited by - sheep19 on June 22, 2009 1:28:01 PM]

##### Share on other sites
Quote:
 Original post by sheep19Thanks for the help!I have another question. My library uses SDL and boost. Will I have to include SDL and boost to use my .lib? Or everything will be built in the .lib file?EDIT:Ok I created the .lib file. Its size is 5116 KB :|I also created a new project, put the lib file in the linker's input and tried to compile. I get:1>------ Build started: Project: Reach The Summit!, Configuration: Release Win32 ------1>Linking...1>MSVCRT.lib(crtexew.obj) : error LNK2001: unresolved external symbol _WinMain@161>G:\Minas\Documents\Visual Studio 2008\Projects\Reach The Summit!\Release\Reach The Summit!.exe : fatal error LNK1120: 1 unresolved externals1>Build log was saved at "file://g:\Minas\Documents\Visual Studio 2008\Projects\Reach The Summit!\Reach The Summit!\Release\BuildLog.htm"1>Reach The Summit! - 2 error(s), 0 warning(s)========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========What's wrong?That's the code by the way:*** Source Snippet Removed ***

Two things:

1) The code snippet you showed uses a main() function. If you're creating a Windows application, you'll need a WinMain() function. Your linker is complaining because it's expecting WinMain() for some reason, and it's not finding it. This could be because your project is set up as a Windows application, or because SDL requires WinMain(). In either case, switching main() to WinMain() should fix the problem. (Note: WinMain() requires different arguments than main(); its signature looks like int WINAPI WinMain(HINSTANCE, HINSTANCE, PSTR, int). Look up a quick Windows programming tutorial for more info.)

2) If you want, you can link your static library against all libraries necessary to use it. I think what happens in this case is that the linked libraries get "rolled in" with your new library. The end result is that users of your library won't have to link against all of the libraries it depends on; they can just link against yours, since it already includes the others. They'll still need to be able to find all your #include-ed files, though.

##### Share on other sites
Quote:
 Original post by AristeTwo things:1) The code snippet you showed uses a main() function. If you're creating a Windows application, you'll need a WinMain() function. Your linker is complaining because it's expecting WinMain() for some reason, and it's not finding it. This could be because your project is set up as a Windows application, or because SDL requires WinMain().

The thing is that SDL does not require WinMain, so I don't want my library to need to use it.

I found an option in settings:

I added the SDL libs there, but this doesn't seem to reduce the lib's file size at all It's still >5MB

EDIT: TO sum up because this is getting ugly:

I want to create a static library that is an add-on to SDL and uses boost. I created a test project using Microsoft Visual C++ 2008 Express Edtion, in which I developed the functions of the library (Min_SpriteLib). Then I removed the .cpp file that has main() from the project and changed the project properties so that it builds a .lib file. The size of the file is greater than 5MB! This might mean that I included SDL and boost in it, which is something I don't want. I then created a new project to use the library (even with the large file size). I chose empty project and then set the project properties like I do with every SDL project (settings by lazyfoo tutorials).
I get this:
1>------ Build started: Project: Reach The Summit!, Configuration: Release Win32 ------
1>MSVCRT.lib(crtexew.obj) : error LNK2001: unresolved external symbol _WinMain@16
1>G:\Minas\Documents\Visual Studio 2008\Projects\Reach The Summit!\Release\Reach The Summit!.exe : fatal error LNK1120: 1 unresolved externals
1>Build log was saved at "file://g:\Minas\Documents\Visual Studio 2008\Projects\Reach The Summit!\Reach The Summit!\Release\BuildLog.htm"
1>Reach The Summit! - 2 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
.

I'm not sure what is exactly happening. Can someone explain to me and tell me the best way to build that library file? Thanks a lot.

[Edited by - sheep19 on June 22, 2009 3:18:21 PM]

##### Share on other sites
Is the project you're linking the library into a Windows application project? If you're not sure, try making a new console project and linking up your library. Then write a main() and see if your linker problems go away.

By the way, why is it that you're so worried about the library's size? 5MB doesn't seem all that big, and it certainly shouldn't be causing any capacity issues on a modern hard drive.

##### Share on other sites
Quote:
 Original post by sheep19How can I leave it the way you are suggesting? (or maybe the programmer who uses it only have to include SDL and not boost)

When building the .lib, don't explicitly link with SDL or boost, i.e. ProjectSettings->Configuration Properties->Librarian->Additional Library Dependencies should be blank. Then any .exe that uses your lib will have to explictly link with SDL (probably not boost because of the auto-linking that boost uses). The fact that your lib is 5 meg doesn't mean you have linked with SDL or boost. Take a look at the .lib sizes of sdl and boost - most of the boost ones are well over 5 meg, so it doesn't sound like these are being included, not sure about sdl I don't know how big those libs are.

Quote:
 Original post by sheep19Is there a way to choose when I build the library if it is going to be a console or a windows app?

It's not the library that needs this option it's your exe. When you select a new project you can choose between Windows Application, Console Application, Static Lib and Dynamic Lib, just select the most appropriate one.

So you basically need :

1. .lib project that doesn't link with anything

2. Console application project that links with your .lib, and SDL (boost is taken care of through the auto linking)

You can change a Windows Application into a console one (ProjectSettings->Configuration Properties->Linker->System->SubSytem) but if I remember correctly this isn't the only option that needs to be changed, so you may be better off starting afresh with a new project.

##### Share on other sites
You do not need to console application if you are using SDL (unless, of course, you also want to have the console window)

You mentioned that you are creating an add-on for SDL. That means that your executable must be set up as a regular SDL application. That is, you must link to the same libraries you would if creating an application: SDL.lib, SDLmain.lib (this is where WinMain is defined. As you are not linking this one, the linker complains about missing symbols), SDL_image.lib, etc.

Of course you'll need to include the required headers. Even if you want to abstract the user of SDL behind your static library, you at last will require to include SDL_main.h where your main is defined, as this header has a macro that redefines your main() to SDL_main() (or something like that) so that SDLmain.lib's WinMain can call it. (This is also to help in cross-compiling, because in this way you need just a single main() for every platform, even if that platform doesn't use main(), as many posters already mentioned.)

So, to summarize, just set up a regular SDL application and than add your library to additional dependencies list.

##### Share on other sites
I did it :) It works :):)

I built the library again, using all the dependencies. (SDL.lib SDLmain.lib SDL_image.lib SDL_ttf.lib and Min_SpriteLib.lib)

I works just fine, even without including SDL_main.h.

This code displays a pic:

#include "Min_SpriteLib.h"using namespace Min;SDL_Surface *screen = 0;bool init();int main(int argc, char *args[]){	init();	Image background("background.png");	background.Blit(0, 0);	SDL_Flip(screen);	SDL_Delay(1000);	return 0;}bool init(){	// initialize SDL	if( SDL_Init(SDL_INIT_EVERYTHING) == -1 ) return false;	atexit(SDL_Quit);	// set up the screen	screen = SDL_SetVideoMode(640, 480, 32, SDL_SWSURFACE /*| SDL_FULLSCREEN*/);	if(!screen) return false;	SDL_EnableKeyRepeat(SDL_DEFAULT_REPEAT_DELAY / 10, SDL_DEFAULT_REPEAT_INTERVAL);	// if everything went fine	return true;}

I also get this warning:

1>Min_SpriteLib.lib(Min_Image.obj) : MSIL .netmodule or module compiled with /GL found; restarting link with /LTCG; add /LTCG to the link command line to improve linker performance
1>LINK : warning LNK4075: ignoring '/INCREMENTAL' due to '/LTCG' specification

Thanks everyone for your help! I have one more question. If I distribute this .lib file and .h files to another pc, will they need to have boost and the other sdl headers to work?

##### Share on other sites
[bump]
i made the project need to link to the libs.do i need to include the sdl and boost headers as well?