User forums > Using Code::Blocks
How to build a static lib ?
leeya:
Thanks for your wonderful solution~~~ :D :D
You are the best!
leeya:
I switch compiler from gcc to vc7.1 in C::B;
I rebuild the project again and get error message, the build log as following:
.......
link.exe /nologo /LIBPATH:D:\CodeWorld\RSWL2VC\lib\Debug /LIBPATH:C:\VCToolkit2003\lib /out:bin\Debug\UnitTest.exe libRSWL2GCCD.lib obj\Debug\precompiled.o obj\Debug\Main.o
LIBC.lib(crt0.obj) : error LNK2019: unresolved external symbol _main referenced in function _mainCRTStartup
bin\Debug\UnitTest.exe : fatal error LNK1120: 1 unresolved externals
Process terminated with status 1120 (0 minutes, 31 seconds)
2 errors, 903 warnings
The error reason was the same as gcc up!
The program entry point was in libRSWL2GCCD.lib, in fact the link could not find the program entry point in libRSWL2GCCD.lib;
can you help me ?
TDragon:
I'm not at a PC with MSVC installed right now, but I think the reason for that error is that link.exe needs an extra option to let it know to create a Win32 app with WinMain as the entry point rather than a console app with main as the entry point.
I believe this should be fixed by adding "/SUBSYSTEM:WINDOWS" in the "Other linker options" box of your project's Linker settings.
leeya:
It is successful for linking the static lib by adding "/SUBSYSTEM:WINDOWS", but the Win::Main could not run properly in Main.cpp. The file was defined as following:
#include "precompiled.h"
#include <iostream>
int Win::Main (HINSTANCE hInst, char const * cmdParam, int cmdShow)
{
std::cout << "Hello World!" << std::endl;
std::cout << "OK!!!!!!!!!!!!!" << std::endl;
return 0;
}
It could not print the statements "Hello World!" and "OK!!!!!!!!!!!!!" .
The static lib WinMain was defined:
#include <WinLibBase.h>
int WINAPI WinMain
(HINSTANCE hInst, HINSTANCE hPrevInst, LPSTR cmdParam, int cmdShow)
{
return Win::Main (hInst, cmdParam, cmdShow);
}
TDragon:
Microsoft's compiler takes the view that if you create a program with WinMain() as the entry point, you want to create a Windows GUI program, without a console for text I/O, and that if you create a program with main() as the entry point, you want to create a console program. This is generally true for the most part; in fact I can't see why you would need to use both WinMain() and std::cout. In other words, if all your program does is print to the console, why not use main()? Or if your program is going to be a GUI program, why not use other methods of displaying output?
Nevertheless, helpful guy that I am, I do know a way to keep a console open when WinMain is the entry point.
- Change "/SUBSYSTEM:WINDOWS" to "/SUBSYSTEM:CONSOLE"
- Add, to the same text field, "/ENTRY:WinMainCRTStartup"
Navigation
[0] Message Index
[#] Next page
[*] Previous page
Go to full version