Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Compiling and executing without the use of projects???

3 views
Skip to first unread message

Win32 Programming

unread,
Jan 15, 2012, 2:10:57 PM1/15/12
to
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. 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.
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.
Thank You.


Alf P. Steinbach

unread,
Jan 15, 2012, 2:38:35 PM1/15/12
to
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.

Jongware

unread,
Jan 16, 2012, 5:25:46 AM1/16/12
to
On 15-Jan-12 20:38 PM, Alf P. Steinbach wrote:
> On 15.01.2012 20:10, Win32 Programming wrote:
>> 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>

Just to confirm:

>C:\BCC55\Bin\bcc32.exe test.cpp
Borland C++ 5.5.1 for Win32 Copyright (c) 1993, 2000 Borland
C:\Users\Jongware\Documents\test.cpp:
Turbo Incremental Link 5.00 Copyright (c) 1997, 2000 Borland

Tool completed successfully
>dir test.exe
(..stuff..)
16-Jan-12 11:18 AM 47,616 test.exe
>test
(The message box appears.)

The executable is a bit large :) Perhaps because I didn't use any
optimization switches, perhaps it's because this is a decade-old compiler.

[Jw]

Alf P. Steinbach

unread,
Jan 16, 2012, 6:11:06 AM1/16/12
to
When you write "Just to confirm", I do not understand what you're trying
to confirm.

Anyway, be aware that Borland C++ 5.5 is a very old and regarding C++
pre-standard product. It's not suitable for modern Windows programming.
Or for anything, really, except learning about computing history.

You built the program as a Windows console subsystem application.

That means e.g. that the command interpreter waits for the program to
complete, and that an ugly console window pops up when you run the
program by double-clicking the file in Windows Explorer.

To build the program as as a GUI subsystem application, like the g++ and
Visual C++ builds that I gave examples of, you just need to add the
relevant options. The first example below builds as a console subsystem
application, and then checks the subsystem of the [.exe] file. The
second example below builds as a GUI subsystem application, like the g++
and Visual C++ build that I gave examples of in my earlier posting:


<example5 compiler="bcc32">
[D:\dev\test]
> bcc32 -q foo.cpp
foo.cpp:

[D:\dev\test]
> tdump foo.exe | find /i "subs"
Subsystem Version 4.0
Subsystem 0003 [ Windows character ]
<example5>

<example6 compiler="bcc32">
[D:\dev\test]
> bcc32 -q -laa foo.cpp
foo.cpp:

[D:\dev\test]
> tdump foo.exe | find /i "subs"
Subsystem Version 4.0
Subsystem 0002 [ Windows GUI ]

[D:\dev\test]
> _
</example6>


Cheers & hth., -- and do get yourself a more modern compiler! :-),

- Alf

Jongware

unread,
Jan 16, 2012, 8:04:22 AM1/16/12
to
Thanks for your further explanation. All I wanted to confirm is that
your code compiles & runs error-free -- even with a decade-old compiler!
(You are correct about the console, as I couldn't remember the correct
Windows flags off-hand.)

Don't worry, I only use BCC at work to write the odd dirty hack program.
At home I'm entertaining myself with XCode 4.2 on OSX (apparently it's
now using Clang rather than ye olde trusty gcc).

[Jw]
0 new messages