Windows Programming/Programming Windows with OSS Tools
Getting an Open Source Compiler[edit | edit source]
With a fast internet connection, gcc — a free compiler that can compile C, C++, and Fortran, as well as other languages and works on Windows — can be obtained through Cygwin. When installing Cygwin, select to install the "devel/gcc" package and optionally also "graphics/opengl", depending on what type of program you're planning to write. Another Windows port of the GCC compiler collection is MinGW (Minimalist GNU for Windows), an approximately 10MB download 1/10th the size of Cygwin. You could also download DJGPP, but it is designed more for 32-bit console programs. An alternative to obtaining the gcc compiler via porting is to compile from command line within a virtualized version of Linux, which not only comes with gcc, but whose kernel "... is written in the version of the C programming language supported by GCC..."
Command Line GCC Illustration[edit | edit source]
To compile, open a command-line interface and use the commands:
c:\cygwin\bin\gcc -s -Os -mno-cygwin -o <outputfilename> <inputfilename>
for command-line programs and
c:\cygwin\bin\gcc -s -Os -mwindows -mno-cygwin -o <outputfilename> <inputfilename> -lopengl32 -lwinmm
for GUI programs.
Description of command-line flags:
- -s: makes the output program smaller by stripping out information that is useful for debugging crashed applications
- -Os: optimizes for size (-O3 for execution speed)
- -mno-cygwin: makes the program work without any external nonstandard .dll-files
- -mwindows: makes it a GUI application
- -lopengl32: optionally lets you use OpenGL for graphics
- lwinmm: optionally adds some multimedia support such as sound, joysticks, and high resolution timers
One compiler must be in the bin directory of Cygwin in order to compile with Cygwin, whereas in Linux gcc is already aliased to work in all directories.
Graphical Integrated Development Environments[edit | edit source]
For a more user-friendly environment, like Visual Studio, an excellent open source IDE for Windows is Bloodshed's Dev-C++. It has a well-organised Graphical User Interface, and comes with everything necessary for setting up a Windows C/C++ development environment, installing and using the Mingw port of GCC by default (though it can be configured to work in combination any other GCC-based compiler such as Cygwin). Its source (written in Delphi) is available from the Bloodshed Website, as are the pre-compiled binaries. Dev-C++ has not been updated in quite some time, though, and alternatives like Code::Blocks or CodeLite have since emerged. Netbeans is an IDE developed by Sun Microsystems as the official IDE for Java however Netbeans now supports multiple languages and is very extensible via its plugin system. Another open source IDE is Eclipse, which was initially developed by IBM. One more IDE, created specifically for C++, is Ultimate++.
GCC, DJGPP, MinGW and others[edit | edit source]
These are three well-known FOSS development toolsets for compiling applications, although they function in a way slightly different to Microsoft and Borland's Windows programming tools.
GCC[edit | edit source]
GCC stands for the GNU Compiler Collection. Although it provides support for compiling sources in several programming languages, it is most commonly used for C and C++ code. Because it is a compiler and not an IDE, approaches such as DJGPP wrap some more arcane details away from the user, easing the creation of programs, as well as providing other features, described below.
DJGPP[edit | edit source]
DJGPP is an Integrated Development Environment (IDE) developed by DJ Delorie since 1989. It is a DOS application with 32-bit awareness and allows the creation of software with DPMI support. As a combined port of GCC and other GNU utilities, it is commonly used to write, compile and debug console applications. It can make use of libraries such as Allegro, in order to create applications with graphics, Other add-ons extend its functionality. It has support for cross-platform development, meaning that you may create software without needing a physical instance of a target machine type or operating system in order to build the executable that will be shipped to that configuration.
MinGW[edit | edit source]
MinGW stands for the Minimalist GNU for Windows and includes the needed header files for using the Win32 API; it supports creation of native Windows executables. As of late December 2006, Wikipedia defines the goal of MinGW as to "provide only a free compiler and toolchain, prioritizing performance". This contrasts the Cygwin objective of creating a POSIX compliant development environment. Like DJGPP, it can be used for cross-platform development. Because MinGW is available for other systems outside of Windows (such as Linux), even developers lacking a copy of Windows can create Win32 API applications. Furthermore, PE executables (.exe files) can be run using the WINE program (which translates Windows API calls to the corresponding UNIX system functions) to get a taste of what the application will look like - without actually having to install Windows.
The original MinGW project (from mingw.org) has been forked and there is now a MinGW64 project. The original project provides only a 32 bit compiler. The forked project provides both 32 and 64 bit compilers. The goals of the projects differ. The original MinGW project strives to work with only Windows APIs that are publicly documented. The MinGW64 project maintains that they've reverse engineered missing information using a clean room technique and so it's not violating copyright to use the APIs.
Dev-C++[edit | edit source]
Dev-C++ is a free graphical IDE that comes packaged with MinGW and can also use the Cygwin compiler.
llvm[edit | edit source]
llvm is an alternative Open Source compiler. One of its goals is improved optimization of programs. It can be used in conjunction with other compiler suites such as Microsoft's C/C++ compiler or MinGW64. As of this writing, it requires a separate linker (such as the one's offered by the GNU compiler suite via MinGW64).
midipix[edit | edit source]
If you only want to use the gcc for Windows development (cross-compiling to Windows) on a Linux machine another option is the midipix project. It uses the lightweight musl C library along with the gnu compiler suite. They're creating a POSIX compatibility layer to port musl to Windows using ntdll. This avoids requiring the Windows C runtime (which MinGW needs). The project already has several Open Source applications ported and running using their version of the GNU gcc compiler.
Other tools[edit | edit source]
GCC, DJGPP and MinGW do not have support for graphic-oriented resource and GUI editing commonly needed for Windows programming.
MinGW supplies the w32api package which includes the headers and a way to link to the DLLs that support the native WIN32 API. Windows OpenGL and DirectX can also be used with MinGW. The MinGW versions of DirectX headers are not as complete as the official Microsoft headers.
However open source libraries such as QT, GTK+, wxWidgets and FLTK provide user interface frontends as well as simplified models wrapping around the complexities of the Windows API. The minimal trade-off is that these may require special licensing and the bundling of runtime DLLs.
One can also use graphics libraries such as SDL, Allegro, SFML to create user interfaces or draw graphics.
Console based text user interface libraries such as PDCurses and ncurses also work on Windows. There is a port of PDCurses with a SDL backend that will allow developers to integrate graphics with a text user interface. Nano-X can be built with an Allegro or SDL backend on Windows and can be used to build some X Windows compatible applications.
All of the above GUI and graphics libraries mentioned work with MinGW. Versions of PDCurses, Nano-X, FLTK and Allegro work with DJGPP.
Cygwin[edit | edit source]
Cygwin is an environment made for Windows that emulates Unix behavior. It can be obtained from cygwin.com via a small setup.exe package that when run, asks for a selection of features that are then fetched. A fast internet connection is suggested, since Cygwin comprises many non-trivial shells, programs, and even GUIs.
The setup program allows the user to download the following parts, among others:
- Bash shell
- Development tools, with debugger, several languages supported by gcc compilers, as well as AWK, SED, YACC and Bison support
- Network tools, such as ssh, lynx web browser, nmap, and others
- Programmer text editors, such as GNU Emacs, nano and vi
- X, which is a very difficult environment to fetch for free on Windows otherwise (unless built from source)
The software, after selected, is downloaded and installed to a folder at the root of the user's hard drive. It does not repartition the drive, require installation of any Unix system nor use a disk image. Files are simply installed in a Unix hierarchy within the cygwin folder.
To run Cygwin, the user will have a Start Menu program or Desktop shortcut called Cygwin Bash Shell. Upon opening it, a DOS-like window opens and displays a classic bash prompt. It is color coded, unlike its DOS underpinnings, and can interact with DOS by being aware of path settings and other system variables, so be aware of the fact that your Windows applications may be located from your system path variable and conflict with a folder's current filename because of DOS's implicit EXE extension expansion.
Directory information[edit | edit source]
As an added detail, if you need access to different drives in your computer, instead of using /mnt/, you have to cd into /cygdrive/ to see your drives listed. Seeing your C: drive's root, then, is just a matter of typing cd /cygdrive/c at the command line.
Emacs editor information[edit | edit source]
When using Emacs, the normal key combination to exit the program has no effect, by default, though adept users will probably find an Emacs keybinding trick to overcome this issue. Thus, when you want to exit the editor from command line mode, you may end up getting tired, and suspend the program to do a later kill of the process at the command line. Instead of pressing C-x C-c, to leave emacs, you must press F10 to get the menu. Once there, press f and then e. This summons the File Menu and later its Exit option, returning to the command line and prompting for the appropriate saving when some changed files need to be committed to disk.
Compiling without Cygwin library bindings[edit | edit source]
Compiling with g++, as stated in a previous section, needs to include the appropriate flags. gcc will normally bind the executable code to the cygwin1.dll file, unless your gcc flags explicitly include "-mno-cygwin" as a switch. To solve this, you can add this and all other switches to your .bashrc file, by using a known Unix option:
Open the .bashrc file at your Cygwin home directory in any editor. Look for the lines starting with "alias". There, you will see some shell commands tweaked to have specific options. Under the line containing
alias l='ls -CF'
you may add:
alias g++='g++ -mno-cygwin' #do not depend on cygwin1.dll on target system
In case you need to add more fine tuning tags, you may either add them to that same line after the -mno-cygwin switch, or just re-alias the g++ command so that the shell stacks up the current meaning of g++ to further tags. For example, you may add
alias g++='g++ -Wno-deprecated' #silence deprecation talk.
To keep the g++ compiler from reminding you about the standard practice of including <string> instead of the now deprecated <string.h> header.
No root nor su switching[edit | edit source]
Cygwin was created with a few differences in mind. It does not have a different root account, though it may claim files are owned by the Administrator user. Though this may be beyond the scope of the book, it is worth a line or two to say that you won't easily be able to experiment with programs that run under different user rights, because it does not seem that Cygwin tries to enforce account security. You may read any file in the OS that Windows allows Cygwin to access.
Running X[edit | edit source]
Other details[edit | edit source]
GNU Tools[edit | edit source]
This page of the Windows Programming book is a stub. You can help by expanding it.