User:Combuster/Frankenstein's MSVC
|
Ph'nglui mglw'nafh Cthulhu R'lyeh wgah'nagl fhtagn! The content of this article or section is Lovecraftian. |
Salvaged black magic rituals nobody seems to have tried in three years. Needs some cleanup; use at your own risk.
Introduction
Have you ever dreamt of using Microsoft C/C++ Optimizing Compiler on Linux? Did you have trouble programming in an unfamiliar GCC environment? This article is the answer to configuring the build environment that you dreamt of; it will solve any problems that you had with GCC!
Configuration
There are many ways that you can utilise Visual C++ (Microsoft C/C++ Optimizing Compiler) for OS development. Firstly, you could simply use Visual C++ on Windows with all Microsoft's tools. But, this poses a lot of problems. For example: Microsoft Linker (link.exe) does not support linker scripts. Since it may be critical that you can 'model' the final executable image, this may pose a problem. The available solutions are not too great in number:
- Use GCC and Binutils on Cygwin (don't use VC++ at all).
- Use Binutils with COFF support to link COFF object files created by cl.exe (ditch VC++ linker).
It's the second solution that is interesting. You may ask "why not try it the other way around?", but that would remove the benefit of having linker scripts, although it would offer you GCC's 'style'. If you were aiming for the latter, then it may be best for you to just build a GCC cross-compiler.
Prerequisites
- Windows system - In order to legally do this, you must have a valid Windows licence (this is due to the 'winetricks' procedure).
- Visual Studio (C++) - You need to have Visual C++ installed on your Windows system in order to get the compilers from it. If you don't, you can download an Express version from the Microsoft website.
- Linux system (or any other compatible system)
- Basic build environment - used to build Binutils.
- Wine - used to run Microsoft C/C++ Optimizing Compiler on Linux.
Last but not least, having an X Window system on your Linux box is recommended to make your life easier while doing the 'winetricks' procedure, but not required. You may also want to check the Wine Database to see what versions of Wine run which versions of the compiler.
Running Microsoft C/C++ Optimizing Compiler on Linux
- Go to your Visual C++ binary directory (ex: C:\Program Files (x86)\Microsoft Visual Studio 9.0\VC\bin).
- Copy all files into your Linux system (use FTP, SFTP, SMB or any equivalent file transfer methods). Tip: make sure to copy 1033 directory. 1033\clui.dll is used by cl.exe and must exist under (cl.exe Directory Path)\1033. If you are only willing to build i386 program, only copy the files in the binary directory and 1033 directory (you don't need to copy the files under x86_amd64, x86_ia64).
- Copy mspdb**.dll from your Visual Studio Common7 directory to Wine system32 directory (ex: C:\Program Files (x86)\Microsoft Visual Studio 9.0\Common7\IDE\mspdb80.dll to ~/.wine/drive_c/windows/system32).
- Try to run cl.exe and see if it works (ex: wine cl.exe). If it displays an error message (that is caused by a runtime DLL), do the following steps, otherwise skip to step 4.
- cd ~/.wine
- wget http://www.kegel.com/wine/winetricks (or any equivalent methods to download the specified file from the web.)
- sh winetricks corefonts vcrun6 vcrun2003 vcrun2005 vcrun2008 (install the runtime version that you need. see http://wiki.winehq.org/winetricks for more information.) Tip: make sure that you run this command under X-Window. Otherwise, you will not see the EULA user acceptance window.
- Go back to the directory where you saved the Microsoft C/C++ Optimizing Compiler executable files and try running cl.exe. It should work in this time. If it does not, make sure that you copied all required files from Visual Studio directory and 'winetricks' ran without any error messages.
- Enjoy
Configuring Binutils for Microsoft C/C++ Optimizing Compiler
- Download the latest version of Binutils source code
- Unzip (or untar, unxz, whatever) the source code
- mkdir&cd (Binutils Directory Name)-(Major Target BFD) (In this example, we will use i386-elf)
- ../(Binutils Directory Name)/configure --target=i386-elf --enable-targets=i386-coff (see (Binutils Directory Name)/ld/configure.tgt for other architecture)
- make all (check any errors and fix them.)
- make install
Building an OS image
Now, if you've followed all steps with no errors, you can build your Hello World image!
Test.cpp
char *VideoMemory = (char *)0xB8000;
extern "C" void Main()
{
const char *HelloWorldMessage = "Hello World!";
while (*HelloWorldMessage != '\0')
VideoMemory++ = HelloWorldMessage++;
}
TestLS.ld
ENTRY(_Main) BASEADDRESS = 0x100000; SECTIONS { . = BASEADDRESS; .text : AT(ADDR(.text) - BASEADDRESS) { *(.text) } .data : AT(ADDR(.data) - BASEADDRESS) { *(.data) } }
- Register the Microsoft C/C++ Optimizing Compiler executable file path in the PATH environment variable.
- Give x permission to all executable files (will automatically be run by Wine).
- cl.exe /Gd /Zl /Od /GS- /X /FAcs /FaTest.lst /FoTest.obj /c Test.cpp
- i386-elf-ld --format coff-i386 --oformat binary -o Test.x -T TestLS.ld -nostdlib Test.obj (Tip: --oformat can be anything you want; as long as the binutils that you built supports it, for ex: elf32-i386).
- Write a loader that loads the image file in Protected Mode at 0x100000 and run it!
FAQ
I've never heard of the Microsoft C/C++ Optimizing Compiler. What is it?
That's not true. You've always been hearing about it. It's the compiler that the Visual C++ IDE uses as default (comes with the C++ component of Visual Studio).
Why use it on Linux when there is GCC to do the job?
It's mainly because of one's preference. GCC has its own style, and so does Microsoft C/C++ Optimizing Compiler. If you like it, you might want to use it on Linux as well. In addition, if you have been using it for years, it's not strange to have a preference over another.