Compiling for Win32 Using MS VC

only for RuBoard - do not distribute or recompile

Compiling for Win32 Using MS VC++

This section covers how to use the same (or nearly the same) code used to create the Linux application to compile and run the same application under MS Windows .

Before we get started here, let me take a moment to say that I don t intend in any way to be an expert on MS Visual C++, and that if you have any pointers for me on how to improve its usage in cases like this, feel absolutely free to send them to me. The compile and link below using VC++ is an admitted hack; however, the focus of this book is that Linux is the primary client platform, and Windows is viewed in a somewhat lesser status. Therefore, this section assumes that the target audience consists primarily of Linux clients , but that there might be a few Windows holdouts to be taken care of.

Also, note that the code for this application was developed on a dual-boot Red Hat 6.2 / Windows 98 machine, which made it simple to keep track of the code and the application. As with all things software, Your mileage may vary. Visual C++ 6.0 Standard was used (less than $100 cost).

Gathering the Necessary Files

First you will need to download a number of files. You will need the MySQL for Windows files. At the time of this writing, the current file was:

 mysql-3.23.32-win.zip 

That file, or its current release at the time you read this, is the file you ll need.When you install it, be sure to select a custom install and to include libraries and include files. This will install not only a MySQL server on your machine (which is actually not needed here), but also the header files, libraries, and dll files.

Next , you will need the GTK+ files for Windows. These can be downloaded from http:// user .sgic.fi/~tml/gimp/win32/.

The files you will need are

 glib-dev-20001226.zip or newer (glib files)  libiconv-dev-20001007.zip or newer (Libiconv, used by Glib)  gtk+-dev-20001226.zip or newer (GTK+ headers, dll files, and import libraries) 

After you have downloaded them, extract to a directory of your choosing.

Next, you will need a file from mingw; mingw is a gcc port to Win32. Fetch the latest version of gcc for mingw and the msvcrt runtime, which are currently available from:

 ftp://ftp.nanotech.wisc.edu/pub/khan/gnu-win32/mingw32/snapshots/gcc-2.95.2-1/ 

Then download the following file:

 mingw-msvcrt-19991107.zip (or newest release) 

In this case, you don t necessarily need to unzip the file; you will only need one file from it, and that will be covered shortly.

Configuring the Project and Compiling

Before diving into this section, you should read the sidebar Using Visual C++ for information on how and why Visual C++ is used here and other options you need to be aware of.

Using Visual C++

In this section, I commit a bit of Visual C++ heresy. The way I use Visual C++ for the include files and libraries is incorrect, or rather, it s not the way Visual C++ is meant to be used.

Specifically, instead of adding the /I and /libpath paths in this section as shown, do the following. From the Tools menu, select Options. In the Options dialog, select the Directories tab and make the directory entries here for the include files, library files, executable, files, and source files.

I do it this way primarily because I prefer the visibility provided by adding the /I and /libflags the way I have done in this chapter. That is, the *.dsw, or workspace, file (the one presented at the end of this chapter) is a plain text file, and from my experience, that is helpful. Let me give an example.

At a client site, a failure in controlling the development environment lead to two different versions of the same include file being on the primary build machine. I decided the more recent of the two was the desired file. However, more than a dozen different applications were regularly compiled on the target machine, and I didn t know which applications compiled with which include file. Having the /I and /libpath in a plain text *.dsw file made it easy to parse the *.dsw files on the development machine to find all the files and line numbers for a specific text string. A person could manually examine a few applications, or even a dozen, in a reasonable amount of time, but if you have a few extremely complicated applications or even dozens of simple ones, being able to quickly and easily parse, sort , and examine the files used in the build process can aid the development process significantly.

Such are my reasons for doing this project in Visual C++ as I did; you will have to judge which approach works best for you. As one of my professors at UWF was fond of saying, this is a point on which men of goodwill could disagree .

Open main.c in Visual C++ and try to compile. It will ask you if you want to create a default workspace. Answer Yes and save to the directory of your choice.

In Project Settings C/C++ Project Options, add

 /I "Drive:\path\to\gtk+"  /I "Drive:\path\to\glib"  /I "Drive:\path\to\gtk"  /I "Drive:\path\to\gdk"  /I "Drive:\path\to\mysql\include"  /I "Drive:\path\to\mysql\debug" 

where /I is the compiler include directive, and Drive is C: or D: (or whatever), and path is wherever you put gtk.h , gdk.h , mysql.h , and so on. Don t put trailing backslashes on.

Go to Project Add to Project Files and add all *.c and *.h files for this project. Those files should now show in the file view pane (see Figure 10.10).

Figure 10.10. Adding the project files to this workspace.
graphics/10fig10.gif

Try to compile, but not to link. At this point, you should get a clean compile ”that is, no errors and no warnings. If you do not, review your steps and take any new steps necessary to get a clean compile on the project.

Copy all dll files and libs to a single place. Add that path to Project Settings Link Project Options as follows :

 /libpath:"Drive:\path\to\files" 

Again, don t put a trailing backslash on. In my project, I included several libpaths:

 /libpath:"d:\GLIB\src\glib"  /libpath:"d:\GTK\src\gtk+\gtk"  /libpath:"d:\GTK\src\gtk+\gdk"  /libpath:"D:\MySQL\win32\lib\debug" 

In Project Settings Link Object/Library Modules, add:

 gtk-1.3.lib (or newest equivalent)  glib-1.3.lib  gdk-1.3.lib  libmysql.lib  zlib.lib 

Do not add mysqlclient.lib.

From mingw.zip, extract the file unistd.h and put it in the gtk\ directory (where gtk.h is). Actually, you could put it any number of places, just as long as the compiler finds it.

In ddc.c and comm_utils.c , add the following line:

 #include <windows.h> 

You might also want to comment that the above line is for Windows only and will need to be commented back out in order to compile under Linux. This line is necessary because these are the only two files in the project that access the MySQL library, and the MySQL library on Win32 makes use of windows.h .

Edit support.c as outlined here:

  • Change ..\pixmpaps to ../pixmpaps , or else the compiler will think it is an escape character.

  • remove && S_ISREF(s.st_mode) . This is a macro defined in stat.h , and the MS version of stat.h is different from the mingw version.

It is important to note here that we are violating one of the rules-of-thumb that will help us at a later date if we stick to it: That is, we are editing support.c , which GLADE will overwrite the next time it is used to modify the UI for this project. This is just something to keep in mind; obviously two changes are not a terribly big deal.

At this point, you should be able to link the compiled files without errors or warnings.

Finally, to run the application, copy the following files to your Windows system directory (in my case, I just used C:\Windows\), or to somewhere in your PATH, or in the same directory as the executable:

 gtk-1.3.dll  glib-1.3.dll  gdk-1.3.dll  iconv.dll  gmodule.1.3.dll  gnu-intl.dll  libmysql.dll 

Now you should be able to run the compiled application on your local (development) machine. (Of course you could also have altered your target machines PATH variables to look in the directory where dll files are stored rather than copying them.) Figure 10.11 shows the Windows version of the application. Note the default GTK icon. Nice touch, guys.

Figure 10.11. The same source code compiled and running under Windows. Note that this is on the same machine that did the development, so it can t actually connect to the MySQL server on einstein .
graphics/10fig11.gif

Listing 10.8 at the end of this chapter displays the workspace file for this project under Visual C++; you can see the above settings reflected there for further reference.

Deploying the Windows Executable

Now that you have the executable, you need to get it up and running on the target client.

Copy the seven dll files from above to a location target machine s PATH , and copy the executable to the desired location. (On the machine in Figure 10.11, it was C:\Win95.) Figure 10.12 shows the Windows executable operating on a Windows machine. The MySQL database that it is using is running on a Linux server, and the network is TCP/IP. Note that in Figure 10.12, einstein is a Samba share on my network (see the sidebar titled About Samba ).

Figure 10.12. The WCA deployed to a Windows PC; this instance of the application is connected to the Linux server einstein.
graphics/10fig12.gif

About Samba

In order to run Windows client/Linux server, the Linux server has to have a simple Samba share available and must broadcast so that the Windows machine can resolve the server name in the database connectivity code of the application to an IP address. This is neither the time nor the place to cover Samba; just let me say that it makes your Linux box look like a Windows box to all the other Windows machines on the network. Also, I set up my first (very simple) Samba share in five minutes by reading the /etc/smb.conf file, commenting in, and changing a few lines to conform to my network. Then it was off and running! I was amazed. In Figure 10.12, you can see the einstein server in the Network Neighborhood; einstein is the Linux server.

Note that, of course, you could overcome this by substituting the IP address for the server name in the code.

The Project Workspace File

Listing 10.8 is the *.dsw file for the Worldwide Commissions compile and build on Windows. The dsw file is the file MS Visual C++ uses to set the workspace for the project. I have included it so you can see the final results of the previous section.

Listing 10.8 Workspace File for WCA.exe When You Compile Under MS VC++
 # Microsoft Developer Studio Project File - Name="Main" - Package Owner=<4>  # Microsoft Developer Studio Generated Build File, Format Version 6.00  # ** DO NOT EDIT **/  # TARGTYPE "Win32 (x86) Console Application" 0x0103CFG=  Main - Win32 Debug  !MESSAGE This is not a valid  makefile  . To build this project using  NMAKE  ,  !MESSAGE use the  Export Makefile  command and run  !MESSAGE  !MESSAGE  NMAKE /f "Main.mak"  .  !MESSAGE  !MESSAGE You can specify a csconfiguration when running NMAKE  !MESSAGE by defining the macro CFG on the command line:  !MESSAGE  !MESSAGE NMAKE /f "Main.mak" CFG="Main - Win32 Debug"  !MESSAGE  !MESSAGE Possible choices for configuration are:  !MESSAGE  !MESSAGE "Main - Win32 Release" ((based on "Win32 (x86) Console Application")  !MESSAGE "Main - Win32 Debug" ((based on "Win32 (x86) Console Application")  !MESSAGE  # Begin Project  # PROP AllowPerConfigDependencies 0  # PROP Scc_ProjName ""  # PROP Scc_LocalPath ""  CPP=cl.exe  RSC=rc.exe  !IF "$(CFG)" == "Main - Win32 Release"  # PROP BASE Use_MFC 0  # PROP BASE Use_Debug_Libraries 0  # PROP BASE Output_Dir "Release"  # PROP BASE Intermediate_Dir "Release"  # PROP BASE Target_Dir ""  # PROP Use_MFC 0  # PROP Use_Debug_Libraries 0  # PROP Output_Dir "Release"  # PROP Intermediate_Dir "Release"  # PROP Target_Dir ""  # ADD BASE CPP /nologo /W3 /GX /O2 /D "WIN32" /D "NDEBUG" /D "_CONSOLE" /D "_MBCS"  /YX /FD /c  # ADD CPP /nologo /W3 /GX /O2 /D "WIN32" /D "NDEBUG" /D "_CONSOLE" /D "_MBCS" /YX  /FD /c  # ADD BASE RSC /l 0x409 /d "NDEBUG"  # ADD RSC /l 0x409 /d "NDEBUG"  BSC32=bscmake.exe  # ADD BASE BSC32 /nologo  # ADD BSC32 /nologo  LINK32=link.exe  # ADD BASE LINK32 kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib  advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib  /nologo /subsystem:console /machine:I386  # ADD LINK32 kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib  advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib  /nologo /subsystem:console /machine:I386  !ELSEIF "$(CFG)" == "Main - Win32 Debug"  # PROP BASE Use_MFC 0  # PROP BASE Use_Debug_Libraries 1  # PROP BASE Output_Dir "Debug"    # PROP BASE Intermediate_Dir "Debug"  # PROP BASE Target_Dir ""  # PROP Use_MFC 0  # PROP Use_Debug_Libraries 1  # PROP Output_Dir "Debug"  # PROP Intermediate_Dir "Debug"  # PROP Ignore_Export_Lib 0  # PROP Target_Dir ""  # ADD BASE CPP /nologo /W3 /Gm /GX /ZI /Od /D "WIN32" /D "_DEBUG" /D "_CONSOLE" /D  "_MBCS" /YX /FD /GZ /c  # ADD CPP /nologo /W3 /Gm /GX /ZI /Od /I "D:\GTK\src\gtk+" /I "D:\GLIB\src\glib"  /I "D:\GTK\src\gtk+\gdk" /I "D:\GTK\src\gtk+\gtk" /I "D:\MySQL\Win32\include" /I  "D:\MySQL\Win32\lib\debug" /D "_DEBUG" /D "_CONSOLE" /D "_MBCS" /FR /YX /FD /GZ /c  # ADD BASE RSC /l 0x409 /d "_DEBUG"  # ADD RSC /l 0x409 /d "_DEBUG"  BSC32=bscmake.exe  # ADD BASE BSC32 /nologo  # ADD BSC32 /nologo  LINK32=link.exe  # ADD BASE LINK32 kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib  advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib  /nologo /subsystem:console /debug /machine:I386 /pdbtype:sept  # ADD LINK32 glib-1.3.lib gtk-1.3.lib gdk-1.3.lib libmysql.lib zlib.lib  kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib  shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib /nologo  /subsystem:console /debug /machine:I386 /nodefaultlib:"library" /out:"WCA.exe"  /pdbtype:sept /libpath:"d:\GLIB\src\glib" /libpath:"d:\GTK\src\gtk+\gtk"  /libpath:"d:\GTK\src\gtk+\gdk" /libpath:"D:\MySQL\win32\lib\debug"  # SUBTRACT LINK32 /pdb:none /nodefaultlib  !ENDIF  # Begin Target  # Name "Main - Win32 Release"  # Name "Main - Win32 Debug"  # Begin Source File  SOURCE=.\callbacks.c  # End Source File  # Begin Source File  SOURCE=.\callbacks.h  # End Source File  # Begin Source File  SOURCE=.\comm_utils.c  # End Source File  # Begin Source File  SOURCE=.\comm_utils.h  # End Source File  # Begin Source File  SOURCE=.\Ddc.c  # End Source File  # Begin Source File  SOURCE=.\Ddc.h  # End Source File  # Begin Source File  SOURCE=.\interface.c  # End Source File  # Begin Source File  SOURCE=.\interface.h  # End Source File  # Begin Source File  SOURCE=.\Main.c  # End Source File  # Begin Source File  SOURCE=.\Support.c  # End Source File  # Begin Source File  SOURCE=.\Support.h  # End Source File  # End Target  # End Project 
only for RuBoard - do not distribute or recompile


MySQL Building User Interfaces
MySQL: Building User Interfaces (Landmark)
ISBN: 073571049X
EAN: 2147483647
Year: 2001
Pages: 119

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net