views:

45

answers:

2

say I built an application called App1 with a lot of classes in a single project. And I decide that I want to build another project called App2 and I want to use those same classes so I decide to turn App1 into a lib file. Must I remove the WinMain function to do so or can I leave it and the compiler will ignore the winMain in the lib file. ??

+1  A: 

Particularly in Visual Studio your static library won't cause troubles in any case. Even though you can add your WinMain (or console main) to a static library, your only chance to use it in your second project is by explicitly seeting Linker-System-Subsystem to appropriate type.

In this case if you match the routine (WinMain for windows subsystem and main for console), then you can actually compile your second project without defining the main routine in it and main would be linked from your library.

Still, in this situation, if you declare that main and ALSO link your library, your local main will have priority and will be called, so that main in library will be ignored.

So, there is actually no difference for you if you export you function to the library or not, you can leave it and it won't be used unless you make what I've pointed to earlier.

Kotti
Ok, so if I did decide to use the Linker-System-Subsystem. In order for this to even be effective, would I have to place a Method inside the WinMain and just override it from the second application ??
numerical25
Well, first, you actually can't override some method which is called from a function that is already compiled in the static library (this can't be done, because that method is likely to be compiled in the same static library and there is no way you can redefine it)
Kotti
If I were you, I would make a header for my class, let's say `class.h`. Now you have some ways to make it easier for you. First, if the class implementation (let's say `class.cpp` file) is not large, I would simply add it to my new project (so that one file is actually referenced in one project - you could also, for example, put these projects in one folder). Second alternative is building a templated class (so that code can be written in `.h` file only), or simply writing all of the code in that `.h` file (bad, but suitable for some apps)
Kotti
The third alternative actually involves this static library method, then everything is pretty simple, but not portable. So, you define your first project as a static library (you still have your `class.h` file, because you have to know how does your class act), then link this library to a new project (via dependecies if your projects are together or just by adding a .lib file if they aren't) and then you can safely use your lib. Note here, that C++ static library built using MSVS can be only used by MSVS compiler (with some hacks and tips on some other compilers too, but the idea is clear)
Kotti
+1  A: 

It just doesn't make any sense to do this. Spin the classes off in their own library project that builds a .lib, have both apps use the library.

Hans Passant