Compiler

If a program have to rely mainly on windows native dll, it is often preferable not to use the GNU Tools.

In Windows, a dynamic-link library (.dll) file has no dangling references. Instead, an access to functions or data goes through a lookup table. So the DLL code does not have to be fixed up at runtime to refer to the program's memory; instead, the code already uses the DLL's lookup table, and the lookup table is modified at runtime to point to the functions and data.

In Windows, there are two types of library, a static library and an import library (both called .lib). A static library is like a Unix .a file; it contains code to be included as necessary. An import library is basically used only to reassure the linker that a certain identifier is legal, and will be present in the program when the .dll is loaded. So the linker uses the information from the import library to build the lookup table for using identifiers that aren't included in the .dll. When an application or a .dll is linked, an import library may be generated, which will need to be used for all future .dll's that depend on the symbols in the application or .dll.

Every exported function needs to be preceded by: __declspec(dllexport)

If there are not functions marked for export, the liker will not generate an import library.

See /EXPORT

Windows vs. Unix: Linking dynamic load modules

CL

  • /TP: Compiles all files as C++ source files
  • /GX: Enables C++ exceptions
  • /EH: Exception Handling Model
    • s: Specifies the synchronous exception-handling model
    • a: Specifies the asynchronous exception-handling model
    • c: Sets the compiler to assume that extern C functions never throw an exception
  • /GR: Enables Run Time Type Information (RTTI)
  • /MD: Creates a multithreaded DLL using MSVCRT.lib (or MSVCR80.dll). NOTE: the executable will have a further dependency to be resolved at runtime, possibly requiring a manifest file)

C/C++ Building Reference

Setting environment for using Microsoft Visual C++ tools:
    C:\Program Files\Microsoft Visual Studio\VC98\Bin\VCVARS32.BAT
    C:\Program Files\Microsoft Visual Studio 8\VC\bin\vcvars32.bat

Microsoft compiler toolchain (Win32 native)

A good reference about the MS toolchain.

Example

The following makefiles:

  1. Generate a dll and an import library
  2. Link an application to the import library

CC=cl
CC_FLAGS=/TP /GR /EHsc

SRC_EXT=c
OBJ_EXT=obj

INCLUDE=/I"C:\Program Files\Microsoft SDKs\Windows\v6.0\Include" \
/I"C:\Program Files\Microsoft SDKs\Windows\v6.0\VC\INCLUDE"

LIB=/LIBPATH:"C:\Program Files\Microsoft SDKs\Windows\v6.0\Lib" \
/LIBPATH:"C:\Program Files\Microsoft SDKs\Windows\v6.0\VC\LIB"

LIBS=WinMM.Lib

MODULES=pa_convert.obj \
pa_trace.obj \
pa_win_wmme.obj \
pa_lib.obj

TARGET=portaudio

all: $(TARGET).dll

clean:
	rm -rf $(MODULES) $(TARGET).dll $(TARGET).lib $(TARGET).exp

$(TARGET).dll: $(MODULES)
	link $(LIB) $(LIBS) $(MODULES) /dll /out:$(TARGET).dll
	cp $(TARGET).dll ..
	cp $(TARGET).lib ..

%.$(OBJ_EXT): %.$(SRC_EXT)
	$(CC) $(CC_FLAGS) $(INCLUDE) /c $<

LIBS=portaudio.Lib

MODULES=patest_sine.obj

TARGET=test.exe

all: $(TARGET)

clean:
	rm -rf $(MODULES) $(TARGET)

$(TARGET): $(MODULES)
	link $(LIB) $(LIBS) $(MODULES) /out:$(TARGET)
	rm -rf $(TARGET).manifest


%.$(OBJ_EXT): %.$(SRC_EXT)
	$(CC) $(CC_FLAGS) $(INCLUDE) /c $<

Instead of using the /I and the /LIBPATH: directives it is often preferable to set the environmental variables:

  • INCLUDE
  • LIB

See Environment Variables for CL.exe and Link.exe

Dynamic Linking

Useful Tools

Dependency Walker

Dependency Walker is a free utility that scans any 32-bit or 64-bit Windows module (exe, dll, ocx, sys, etc.) and builds a hierarchical tree diagram of all dependent modules. For each module found, it lists all the functions that are exported by that module, and which of those functions are actually being called by other modules. Another view displays the minimum set of required files, along with detailed information about each file including a full path to the file, base address, version numbers, machine type, debug information, and more.

Process Explorer

Ever wondered which program has a particular file or directory open? Now you can find out. Process Explorer shows you information about which handles and DLLs processes have opened or loaded.

Process Explorer

DebugView

DebugView is an application that lets you monitor debug output on your local system, or any computer on the network that you can reach via TCP/IP. It is capable of displaying both kernel-mode and Win32 debug output, so you don't need a debugger to catch the debug output your applications or device drivers generate, nor do you need to modify your applications or drivers to use non-standard debug output APIs.

DebugView

Visual Studio

windows/compiler.txt · Last modified: 2010/09/30 by emilmont
CC Attribution-Noncommercial-Share Alike 3.0 Unported
Valid CSS Driven by DokuWiki Recent changes RSS feed Valid XHTML 1.0