Home |
RTTarget-32 Programming Manual Running Win32 Programs without Win32 Running a Program on the Target Compiling and Linking with On Time RTOS-32 Microsoft Visual C++ |
Microsoft Visual C++On Time RTOS-32's Libmsvc and Include directories must be added to the compiler's library and include file search paths. Microsoft's 32-bit linker has one serious bug: it does not strictly follow the order of libraries specified on the linker command line. Usually, C/C++ linkers will search for a symbol required by a program in all libraries in the order the libraries are supplied. With Microsoft's linker, the first occurrence of the symbol after its reference is used. Since RTKernel-32 and RTTarget-32 replace symbols usually present in run-time or Win32 system libraries, this can lead to the wrong modules being linked if that symbol is not referenced by any of the application's object file. For example, library Libc.lib of Visual C++ contains the function malloc and also references to malloc. However, in On Time RTOS-32 programs, this function might be supplied by Rttheap.lib, which is linked before Libc.lib. If the application itself does not reference malloc, the wrong version of malloc will be linked. Consider the following libraries linked in the given order: Rtk32.lib Drvrt32.lib Rttheap.lib Rtt32.lib Libc.lib None of the On Time RTOS-32 libraries contain references to malloc, so LINK will not look for it until Libc.lib is parsed. malloc is located in Rttheap.lib, but LINK missed that definition. Instead, it will link the version of malloc defined in Libc.lib. To solve this problem, it is recommended to force the inclusion of symbols redefined by RTFiles-32, RTKernel-32, or RTTarget-32 on LINK's command line using the /include option. It is sufficient to include _RTFileSystemList, EnterCriticalSection@4, and _malloc. The only reason not to reference these symbols would be programs known not to require them (e.g., programs not using a standard run-time system). Library Kernel32.lib should be excluded with option /nodefaultlib:Kernel32.lib to ensure that all required Win32 functions will be emulated by RTKernel-32 or RTTarget-32. Assuming On Time RTOS-32 is installed in directory C:\Ontime, the example program Hello.c can be compiled and linked using the following command line (enter on one line): cl /IC:\Ontime\Include /LC:\Ontime\Libmsvc /Fm /Zi Hello.c Rtt32.lib /link /nodefaultlib:Kernel32.lib /fixed:no /include:_malloc /include:_EnterCriticalSection@4 /include:_RTFileSystemList If you prefer to call the linker explicitly, you can compile and link the program in two separate steps: cl /c /IC:\Ontime\Include hello.c link /fixed:no /nodefaultlib:Kernel32.lib /include:_malloc /include:_EnterCriticalSection@4 /include:_RTFileSystemList Hello.obj C:\Ontime\Libmsvc\Rtt32.lib Demo program Hello2.c shows how to eliminate the run-time system with RTTarget-32's startup code C0rtt.obj. It can be compiled and linked using the following command line (enter on one line): cl Hello2.c C0rtt.obj Rtt32.lib /link /fixed:no /nodefaultlib:Kernel32.lib If Rtt32dll.dll is used instead of Rtt32.lib, any EXE or DLL which needs to call RTTarget-32 native API functions must link import library Rtt32dll.lib. Rtt32dll.lib is not required to call Win32 emulation functions. The Microsoft linker does not write a fixup table to .EXE files by default. Because such programs cannot be processed by RTLoc, linker option /fixed:no is required. PE files generated by Microsoft Visual C++ contain four sections that must be located: sections .text, .rdata, and .data. Section .text contains the executable part of the program and only requires read-only access. Section .rdata contains read-only data. Section .data contains initialized data and needs read/write access. If incremental linking is enabled, required section .textbss may also be present. If the program contains TLS (thread) variables, section .tls must also be included in the program image. If the program needs to access resources of the PE file at run-time, section .rsrc is also required. The Microsoft linker writes very precise information into PE files. Therefore, it is not necessary to use .MAP files to truncate unused information, and there is usually no need to specify any size information in the Locate NTSection or Locate Section commands. The default alignment is 4k, which is also RTLoc's default. Therefore, using command Locate NTSection is recommended instead of Locate Section. The latter should only be used if several sections must be located into different memory regions and you do not want to use virtual regions. If the run-time system is used, a heap with at least 64k is required. If no run-time system is used, no heap is needed. A stack with at least 16k (64k for VS 2008/2010/2012/2013/2015/2017/2019 and VC++ 9.0/10.0/11.0/12.0/14.0/15.0/16.0) is recommended. The run-time system of VS 2008/VC++ 9.0 and later requires about 16k stack space to write data to files (including stdout and stderr, the screen), so any main program and thread which may write to a file or stdout must have at least 20k stack space or more. A typical configuration file for a Visual C++ program is: #ifndef BOOT Reserve Monitor #endif Virtual VMem 1G FillRAM VMem Locate PageTable PageTable HighMem Locate Header Header HighMem Locate Stack Stack VMem 65k Locate Heap Heap VMem Locate NTSection PEHeader VMem->HighMem Locate NTSection .textbss VMem->HighMem Locate NTSection .text VMem->HighMem Locate NTSection .rdata VMem->HighMem Locate NTSection .data VMem->HighMem Locate NTSection .tls VMem->HighMem Locate DecompCode Expand HighMem Locate DecompData ExBuffer HighMem Locate Copy PageTable HighMem Locate Copy PEHeader HighMem Locate Copy .text HighMem Locate Copy .rdata HighMem Locate Copy .data HighMem For a program that doesn't use the run-time system, the Locate Heap command can be omitted. Section .textbss is only required by Microsoft Visual C++ 7.0 and higher if incremental linking is used. To enable source level debugging, compiler command line switch /Zi is required. If RTD32 is to be used, RTLoc option -g+ must be used. Debugging VC++ compiled programs with RTD32 is only supported for programs compiled with the VC++ 6.0 command line compiler. If RTLoc option -g+ is specified, RTLoc requires a DLL of the Visual C++ installation to read the debug symbol tables. The name of this DLL is Visual C++ version dependent:
The file will usually reside in the same directory as the Visual C++ compiler or IDE. Please make sure that the required DLL is on the system PATH or in RTTarget-32's Bin directory. Please refer to the examples under directory Demomsvc for numerous examples of building On Time RTOS-32 programs using command line tools. Examples for the Visual Studio IDE are given under directory Demomsdev. Compiling and Linking with On Time RTOS-32
|