On 15.01.2012 20:10, Win32 Programming wrote:
> Hello everyone,
> I am new to Win32 Api programming and I want to know
> if there is a way to compile and execute single Win32 programmes without the
> use of projects.
> I have tried using GCC but I keep getting the linker
> error of not being able to locate the 'GetStockObject' procedure for the
> brushes.
You need to link in [user32.dll]. The easiest way with g++ is to use the
semidocumented option
-mwindows
You can also just say
g++ blah.cpp -std=c++0x -pedantic -Wall -luser32
> I have also tried using Visual C++'s cl.exe (after making
> neccessary changes to the environment variables) but I keep getting the
> error of windows.h no include path defined.
Use the console window that you get a menu item for in the Start menu
folder for Visual C++.
It sets up PATH, INCLUDE and LIB environment variables.
> I am new to WIN32 API thus have to write a lot of
> programmes for practice, each VC++ project is about 14 MB with only one .cpp
> file containing the programme. If there is a way of compiling and linking
> single and trivial programmes it would make things a lot easier for me.
Here's an example.
<code>
#undef UNICODE
#define UNICODE
#undef NOMINMAX
#define NOMINMAX
#include <windows.h>
int main()
{
MessageBox(
0,
L"Just click OK...",
L"My message box!",
MB_ICONINFORMATION | MB_SETFOREGROUND
);
}
</code>
<example1 compiler="g++">
[D:\dev\test]
> g++ foo.cpp -s -mwindows
[D:\dev\test]
> dir | find ".exe"
15.01.2012 20:30 8 192 a.exe
[D:\dev\test]
> a
[D:\dev\test]
> _
</example1>
<example2 compiler="g++">
[D:\dev\test]
> del a.exe
[D:\dev\test]
> g++ foo.cpp -s -luser32 -Wl,-subsystem,windows
[D:\dev\test]
> dir | find ".exe"
15.01.2012 20:32 8 192 a.exe
[D:\dev\test]
> a
[D:\dev\test]
> _
</example2>
<example3 compiler="cl">
[D:\dev\test]
> del *.exe
[D:\dev\test]
> set CL=/nologo /EHsc /GR /W4
[D:\dev\test]
> cl foo.cpp /link user32.lib /subsystem:windows /entry:mainCRTStartup
foo.cpp
[D:\dev\test]
> dir | find ".exe"
15.01.2012 20:35 31 744 foo.exe
[D:\dev\test]
> foo
[D:\dev\test]
> _
</example3>
<example4 compiler="cl">
[D:\dev\test]
> set MSVC_GUI=/subsystem:windows /entry:mainCRTStartup
[D:\dev\test]
> cl foo.cpp /link user32.lib %MSVC_GUI%
foo.cpp
[D:\dev\test]
> dir | find ".exe"
15.01.2012 20:36 31 744 foo.exe
[D:\dev\test]
> _
</example>
Cheers & hth.,
- Alf
PS: Perhaps check out my Work In Progress tutorial, at <url:
http://learnwinapi.wordpress.com/>? It does not go into command line
details, though.