User:Combuster/Frankenstein's MSVC

From OSDev Wiki
Jump to: navigation, search


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.



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!


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:

  1. Use GCC and Binutils on Cygwin (don't use VC++ at all).
  2. 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.


  • 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

  1. Go to your Visual C++ binary directory (ex: C:\Program Files (x86)\Microsoft Visual Studio 9.0\VC\bin).
  2. 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).
  3. 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).
  4. 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.
    1. cd ~/.wine
    2. wget (or any equivalent methods to download the specified file from the web.)
    3. sh winetricks corefonts vcrun6 vcrun2003 vcrun2005 vcrun2008 (install the runtime version that you need. see for more information.) Tip: make sure that you run this command under X-Window. Otherwise, you will not see the EULA user acceptance window.
    4. 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.
  5. Enjoy

Configuring Binutils for Microsoft C/C++ Optimizing Compiler

  1. Download the latest version of Binutils source code
  2. Unzip (or untar, unxz, whatever) the source code
  3. mkdir&cd (Binutils Directory Name)-(Major Target BFD) (In this example, we will use i386-elf)
  4. ../(Binutils Directory Name)/configure --target=i386-elf --enable-targets=i386-coff (see (Binutils Directory Name)/ld/configure.tgt for other architecture)
  5. make all (check any errors and fix them.)
  6. make install

Building an OS image

Now, if you've followed all steps with no errors, you can build your Hello World image!


char *VideoMemory = (char *)0xB8000;
extern "C" void Main()
    const char *HelloWorldMessage = "Hello World!";
    while (*HelloWorldMessage != '\0')
        VideoMemory++ = HelloWorldMessage++;



BASEADDRESS = 0x100000;


    .text : AT(ADDR(.text) - BASEADDRESS)

    .data : AT(ADDR(.data) - BASEADDRESS)

  1. Register the Microsoft C/C++ Optimizing Compiler executable file path in the PATH environment variable.
  2. Give x permission to all executable files (will automatically be run by Wine).
  3. cl.exe /Gd /Zl /Od /GS- /X /FAcs /FaTest.lst /FoTest.obj /c Test.cpp
  4. 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).
  5. Write a loader that loads the image file in Protected Mode at 0x100000 and run it!


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.

Personal tools