Jesus 'n Jim

A mainly PC (some Mac) site w/Software, Computer Repair Info, How-To's on Using Computers
Technical Support 1-360-521-2060 (my business line cell)

commonly used compiler switches

 

[ Borland C++ 5.5.1 (BC++5.5 free) ] [ Borland C++ 5.82 (Borland Turbo C++ Explorer free) ] [ Microsoft Visual C++ 6.0 ] [ Microsoft Visual C++ 2008 Express (free) ] [ GNU gcc/gpp ] [ adding a manifest to an exe/dll for windows 7/vista ]

Borland C++ 5.5.1 (BC++5.5 free)

  • -P (compile all files as C++)
  • -5 (586)
  • -a1 (align on 1-byte boundary for structs - very important!)
  • -O1 (optimization for size)
  • -O2 (optimization for speed)
  • -Oc (Eliminate duplicate expressions within basic blocks and functions)
  • -Od [Disable all optimizations (useful for debugging?)]
  • -OS (Pentium instruction scheduling)
  • -Ov (Enable loop induction variable and strength reduction)
  • -y (produce Line Number Debug Info)
  • -v (source Level Debugging)
  • -tW [Target is a Windows application (same as -W)]
  • -tWC [Target is a console application (same as -WC)]
  • -tWD [Generate a .DLL executable (same as -WD)]
  • -tWM [Generate a 32-bit multi-threaded target (same as -WM)]
  • -tWR [Target uses the dynamic RTL (same as -WR)]
  • -tWV [Target uses the VCL]
  • -Ic:\gnuwin32\getgnuwin32\gnuwin32\include;c:\borland\bcc55\include (path to your gnuwin32 is probably different. this is for C++ version of flex output)
  • -Lc:\borland\bcc55\lib

Borland C++ 5.82 (Borland Turbo C++ Explorer free)

  • -P (compile all files as C++)
  • -5 (586)
  • -a1 (align on 1-byte boundary for structs - very important!)
  • -O1 (optimization for size)
  • -O2 (optimization for speed)
  • -Oc (Eliminate duplicate expressions within basic blocks and functions)
  • -Od [Disable all optimizations (useful for debugging?)]
  • -OS (Pentium instruction scheduling)
  • -Ov (Enable loop induction variable and strength reduction)
  • -y (produce Line Number Debug Info)
  • -v (source Level Debugging)
  • -tW [Target is a Windows application (same as -W)]
  • -tWC [Target is a console application (same as -WC)]
  • -tWD [Generate a .DLL executable (same as -WD)]
  • -tWM [Generate a 32-bit multi-threaded target (same as -WM)]
  • -tWR [Target uses the dynamic RTL (same as -WR)]
  • -tWV [Target uses the VCL]
  • -I"c:\gnuwin32\getgnuwin32\gnuwin32\include;C:\Program Files\Borland\BDS\4.0\include" (path to your gnuwin32 is probably different. this is for C++ version of flex output)
  • -L"C:\Program Files\Borland\BDS\4.0\lib"
  • -Q (extended compiler error info)
  • -R (include browser information in .OBJ files)
  • -d (merge duplicate strings)

Borland knowledgebase is awful. nothing about their compilers, just some developer support apps they make. Borland help is awful except in version 5.5. can't even find out the details on the -tW/-W switch. when you search their site for bcc32 or -W or compiler options, you get nothing - well, maybe 1 entry: Jbuilder 2006.

Microsoft Visual C++ 6.0

  • /G5 (586)
  • /O1 (maximize space)
  • /O2 (maximize speed)
  • /GA (optimize for windows application)
  • /GD (optimize for windows DLL)
  • /GZ (enable runtime debug checks)
  • /Zi (enable debugging info)
  • /ZI (enable edit and continue debugging info)
  • /ZP1 (pack structs on 1-byte boundary - important to prevent holes in structures like MBR!)
  • /link user32.lib gdi32.lib
  • /TP (compile all files as C++)
  • /I<dir> (include search path)

packing pragmas (how to make sure there are no holes/gaps in your structures during execution).

Microsoft Visual C++ 2008 Express (free)

  • /G5 (586
  • /O2 (maximize speed)
  • /O1 (maximize space)
  • /GA (optimize for windows application)
  • /GD (optimize for windows DLL)
  • /Ge (force stack checking for all funcs)
  • /GZ Enable stack checks (/RTCs)
  • /Zi (enable debugging info)
  • /ZI (enable edit and continue debugging info)
  • /ZP1 (pack structs on 1-byte boundary - important to prevent holes in structures like MBR!)
  • /FC (use full pathnames in diagnostics)
  • /link user32.lib gdi32.lib
  • /TP (compile all files as C++)
  • /I<dir> (include search path)
  • /AI<dir> (add to assembly search path)

packing pragmas (how to make sure there are no holes/gaps in your structures during execution).

Note: with this version, you must copy C:\Program Files\Microsoft Visual Studio 9.0\Common7\IDE\mspdb80.dll to C:\Program Files\Microsoft Visual Studio 9.0\VC\bin in order to run cl in the bin directory. In factr, this may apply to any of the files in the bin directory. and the bin directory is not put in your PATH - you must do that yourself.

GNU g++/gcc/gpp (includes djgpp, MinGW)

  • -x c (compile as ANSI C only)
  • -x c++ (compile as C++ only)
  • -s (strip default included debug and other info - makes 2/3 smaller executeables!)
  • -O (highest optimization that generates good code)
  • -O2 (high optimization, but generates bad code)
  • -O3 (highest optimization, but generates bad code)
  • -fpack-struct (align on 1-byte boundary for structs - very important! - no holes) WARNING: this switch currently causes errors when used with any of the C++ iostreams (ftream, ostream, istream)
  • -g (produce debug info)
  • -mtune=i386 (used with MinGW, not really necessary)
  • -mtune=x86-64 (used w/ MinGW, not really necessary)
  • -march=i386 (used w/ MinGW, not really necessary)
  • -march=x86-64 (used w/ MinGW, not really necessary)
  • -shared (used with MinGW)
  • -W (useful warnings, usually used with -Wall)
  • -Wall (all warnings, usually used with -W)
  • -l <libraryBaseName> (include lib<library>.a in compilation). I find that in mingw and mingw-w64, this switch does not work.
  • -nodefaultlibs (Do not use the standard system libraries when linking. this is not a common option, but may be useful if you are trying to bootstrap the compiler.)
  • -ansi (allow variable-sized arrays such as n=argc;double x[n];)
  • -fstack-check (stack checking. this is off by default)
  • -fast (available on Mac OSX platform, not sure about other platforms. agressively unrolls loops, inlines functions, etc.)
  • -mthreads (enable mingw-specific threads support)
  • -mdll (generate code for a dll)
  • -m128bit-long-double (sizeof(long double) is 16)
  • -m80387 (Use hardware fp)
  • -mwindows (compile for windows [ANSI, not UNICODE]. uses A rather than W system calls. note that if this option is used, will no longer work on nt/XP/2000/vista/7/2003/2008.)
  • -mconsole (compile for console. nt/XP/2000/vista/7/2003/2008.)
  • -shared (Produce shared object [DLL] which can then be linked with other objects [DLL's] to form an executable. Not all systems support this option. For predictable results, you must also specify the same set of options that were used to generate code (-fpic, -fPIC, or model suboptions) when you specify this option.) this option I *think* is useless in DJGPP.
  • -static-libstdc++ (When the g++ program is used to link a C++ program, it will normally automatically link against libstdc++. If libstdc++ is available as a shared library, and the -static option is not used, then this will link against the shared version of libstdc++.) this option is useless in mingw and DJGPP.
  • -static (for mingw-w64. makes monolithic exe, but sacrifices throwing over dll's). On systems that support dynamic linking, this prevents linking with the shared libraries. On other systems, this option has no effect.
  • -static-libgcc (for mingw-w64. makes monolithic exe, but sacrifices throwing over dll's). libgcc is the runtime library DLL the EXE loads by default.
  • -std=c++0x (adds TR1 Standard C++ Library Extensions)
  • -std=gnu++0x (adds TR1 Standard C++ Library Extensions)

everything about this compiler is manual labor intensive. at least it's free. The best way to handle large quantity of files for gcc and g++/gpp is to use response files. for instance g++ -o something.exe @myfiles.lst and this is for MinGW, it will produce a useable executeable. djgpp does not prouce a useable executeable. gpp -o something.exe @myfiles.lst for djgpp you must EXE2COFF a.exe which results in the file "a", copy /b CWSDSTUB.EXE+a something.exe and the resulting file new.exe will be a useable executeable for DOS and will run under windows.

packing pragmas (how to make sure there are no holes/gaps in your structures during execution).

adding a manifest for vista/7 compatibility

I have written a batch file for adding a manifest resource to the compilation for the mingw compiler. I have not yet figured out how to do this for the Borland compiler because I was having a hard time figuring out how to put in the resource into the EXE. I later learned that you compile the .rc file and link in the resulting .res file like it was a library.

check out this manifest xml reference and this article and this post and article on using microsoft's manifest tool mt (documents the 1 or 2 thing) and article about manifest, clickonce, and elevated UAC levels like admin, and the processorarchitecture reference.

Manifests are normal windows resources, just like embedded icons, dialogs and so on.

  1. Create some xml file containing your manifest, e.g. app.dll.manifest
  2. 1 for exe, 2 for dll. #include "winuser.h"
    2 RT_MANIFEST "app.dll.manifest"
    should be added to your .rc file, compiled to a .o (.obj for borland). I have seen 1 24 "app.exe.manifest", but all it does is cause windres to crash/GPF.
  3. Using windres you compile the .rc into an object file (.o)
  4. link the object file (.o) together with all the other object files to your final executable using gcc or g++. if you are compiling a dll, use the -shared switch
windres --input=app.rc --input-format=rc --output=app.res --output-format=coff
g++ -shared -o app.dll app.cpp app.res -lmsvcr90

or

windres --input=app.rc --input-format=rc --output=app.res --output-format=coff
g++ -o app.exe app.cpp app.res -lmsvcr90

the msvcr90 microsoft runtime I don't know about, it seems to be only needed for the python language, from what I have seen on google. It is probably best to leave it out (it does not come with mingw).

The filesystem hooks will query the virtual store first (And only read the "real" location [c:\program files\whatever] if the file is not found in the virtual store) for applications without a manifest. (quote from someone in bug tracker in sf.net)

link to where gw.cmd is, info on mingw compiler and where to get it in 64-bit and both 32-bit versions.

make sure when you download that you save the file rather than open/run it.

to compile a .rc file in, simply specify rc somefilename.rc, for example, call gw 32 -manifest default rc somefilename.rc someapp exe someapp.cpp. the -manifest default tells the batch file to generate a manifest and a resource file and to compile it in coff format, and compile it into the exe. this makes it compatible with windows vista/7 I am told.

to skip the rc file and just include the manifest, call gw 32 -manifest default someapp exe someapp.cpp

if you want to pack structures so there are no holes, use pack. but it won't work with iostream or locale (gcc bug they won't fix, borland and microsoft compilers do not have this problem)

if you want debug code, use debug.

if you want a manifest, use manifest or -manifest or --manifest followed by either the word default or defaults, which chooses executionLevel=asInvoker and uiaccess=false, or the -manifest switch can be followed by executionlevel and uiaccess. executionlevel values are: asInvoker, highestAvailable, requireAdministrator. uiaccess possible values are: true, false. You will be prompted for the Application Title and the Assembly Version. I suggest you echo these strings to yourself for cut and paste when you make your build batch file.

if you want help, use no arguments, or use /? or /h or -? or -help or --help or /help or any other variation.

if you want 64-bit code, just specify 64.

if you want 32-bit code, just specify 32. this is the default.

for a specification document on manifest xml files, articles on compiling them into your executables using cygwin/mingw, and a list of commonly used compiler switches, refer to here.

for a list of useful #defines you can use for making your source code compiler independent, refer to here.

to compile a 32-bit app using mingw 5.1.6, app named df manually

df.manifest.rc:

#include "winuser.h"
1 RT_MANIFEST "df.exe.manifest"

manifest xml windows resource df.dll.manifest:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<assembly xmlns="urn:schemas-microsoft-com:asm.v1" manifestVersion="1.0">
	<assemblyIdentity version="2.8.0.0"
     processorArchitecture="x86"
     name="df"
     type="win32"/>
	<description>show disk free space and usage in SI units with graph or in bytes</description>
	<!-- Identify the application security requirements. -->
	<trustInfo xmlns="urn:schemas-microsoft-com:asm.v2">
		<security>
			<requestedPrivileges>
				<requestedExecutionLevel level="asInvoker" uiAccess="false"/>
			</requestedPrivileges>
		</security>
	</trustInfo>
</assembly>

build. please note that it is required to set the path to the compiler's bin directory before using the compiler's executables.

SET PATH=c:\mingw\bin;%PATH%

c:\mingw\bin\windres.exe --input=df.manifest.rc --input-format=rc --output=df.manifest.res --output-format=coff

c:\mingw\bin\g++.exe -Wall -W -O -s -fstack-check  -IC:/libpq/;c:/libpq/server/libpq/;c:/mingw/include -Lc:/libpq/;c:/mingw/lib -o df.exe df.cpp prsinum.cpp df.manifest.res

where the program's name is df.exe version 2.8.0.0 (enforce the 0.0.0.0 version format in microsoft's sample or you will end up with an executable that will not execute even on XP) and it is a 32-bit application. asInvoker and uiAccess should probably not be changed if you want oneclick install capability.

to compile a 64-bit app with mingw-w64 (32-bit version), app named df manually

df.manifest.rc:

#include "winuser.h"
1 RT_MANIFEST "df64.exe.manifest"

manifest xml windows resource df.dll.manifest:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<assembly xmlns="urn:schemas-microsoft-com:asm.v1" manifestVersion="1.0">
	<assemblyIdentity version="2.8.0.0"
     processorArchitecture="AMD64"
     name="df64"
     type="win32"/>
	<description>show disk free space and usage in SI units with graph or in bytes</description>
	<!-- Identify the application security requirements. -->
	<trustInfo xmlns="urn:schemas-microsoft-com:asm.v2">
		<security>
			<requestedPrivileges>
				<requestedExecutionLevel level="asInvoker" uiAccess="false"/>
			</requestedPrivileges>
		</security>
	</trustInfo>
</assembly>

build. please note that it is required to set the path to the compiler's bin directory before using the compiler's executables. use the hand-built compiler.

SET PATH=c:\mingw-w32-bin_i686-mingw_20091224_sezero\mingw64-w32\bin;%PATH%

c:\mingw-w32-bin_i686-mingw_20091224_sezero\mingw64-w32\bin\windres.exe --input=df64.manifest.rc --input-format=rc --output=df64.manifest.res --output-format=coff

c:\mingw-w32-bin_i686-mingw_20091224_sezero\mingw64-w32\bin\g++.exe -Wall -W -O -s -fstack-check  -IC:/libpq/;c:/libpq/server/libpq/;c:/mingw-w32-bin_i686-mingw_20091224_sezero/mingw64-w32/include -Lc:/libpq/;c:/mingw-w32-bin_i686-mingw_20091224_sezero/mingw64-w32/lib -o df64.exe df.cpp prsinum.cpp df64.manifest.res c:\mingw-w32-bin_i686-mingw_20091224_sezero\mingw64-w32\lib\gcc\i686 -w64-mingw32\4.4.3\libgcc.a

where the program's name is df64.exe version 2.8.0.0 (enforce the 0.0.0.0 version format in microsoft's sample or you will end up with an executable that will not execute even on XP) and it is a 32-bit application. asInvoker and uiAccess should probably not be changed if you want oneclick install capability.

Valid manifest XML values

Valid values for processorArchitecture are:

  • IA64:the intel processor used on some 64-bit servers.
  • AMD64:64-bit AMD processor
  • x86:32-bit intel architecture. I could be wrong, but it could also include the core i7 processor as well (someone please verify?).
  • MSIL:I don't know what this is.

Valid values for requestedExecutionLevel uiAccess attribute are:

  • false:apps that are not providing accessibility.
  • true:must be Authenticode signed. should be only used for UI Assistive Technology apps.

Valid values for requestedExecutionLevel level attribute are:

  • asInvoker:standard user app. runs with same access token as parent process. recommend you go with this one for most apps.
  • highestAvailable:runs with highest privs obtainable. for mixed-mode apps.
  • requireAdministrator:app runs only for administrators. recommended for admin-only apps. internal elevation points are not needed.