2

I'm trying to change the compiler collection of a project which uses a lot of exceptions (try/catch blocks) from Microsoft Visual C++ to LLVM, but ran into compilation issues.

Used tool chain:

  • Visual Studio 2015 Enterprise (MSVC 14)
  • LLVM 3.7 Windows/x64

Here is some test source code:

#include <iostream>
int main()
{
    try
    {
        throw 20;
    }
    catch (int e)
    {
        std::cout << "An exception occurred. Exception Nr. " << e << '\n';
    }
    return 0;
}

Compiling the code with the built-in Visual Studio compiler collection (v140), this are the command line results:

ClCompile:
  C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\bin\CL.exe
      /c /ZI /nologo /W3 /WX- /sdl /Od /Oy- /D _MBCS /Gm /EHsc /RTC1 /MDd /GS /fp:precise
      /Zc:wchar_t /Zc:forScope /Zc:inline /Fo "Debug\\" /Fd"Debug\vc140.pdb" /Gd /TP
      /analyze- /errorReport:queue Main.cpp
  Main.cpp
Link:
  C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\bin\link.exe
      /ERRORREPORT:QUEUE /OUT:"C:\Users\jurocha\Desktop\ClangExcept\Debug\ClangExcept.exe"
      /INCREMENTAL /NOLOGO kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib
      advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib
      /MANIFEST /MANIFESTUAC:"level='asInvoker' uiAccess='false'" /manifest:embed /Debug
      /PDB:"C:\Users\jurocha\Desktop\ClangExcept\Debug\ClangExcept.pdb" /TLBID:1
      /DYNAMICBASE /NXCOMPAT
      /IMPLIB:"C:\Users\jurocha\Desktop\ClangExcept\Debug\ClangExcept.lib"
      /MACHINE:X86 Debug\Main.obj

Compilation succeeds.

Now, changing the compilers to LLVM (LLVM-vs2014):

ClCompile:
  C:\Program Files\LLVM\3.7\msbuild-bin\CL.exe
      /c /ZI /nologo /W3 /WX- /sdl /Od /Oy- /D _MBCS /Gm /EHsc /RTC1 /MDd /GS /fp:precise
      /Zc:wchar_t /Zc:forScope /Zc:inline /Fo"Debug\\" /Fd"Debug\vc140.pdb" /Gd /TP
      /analyze- /errorReport:queue -m32 -fmsc-version=1900 Main.cpp
clang-cl.exe : warning : argument unused during compilation: '/ZI' [ClangExcept.vcxproj]
clang-cl.exe : warning : argument unused during compilation: '/Gm' [ClangExcept.vcxproj]
clang-cl.exe : warning : argument unused during compilation: '/GS' [ClangExcept.vcxproj]
Main.cpp(7,3): error : cannot use 'throw' with exceptions disabled [ClangExcept.vcxproj]
                  throw 20;
                  ^
Main.cpp(5,2): error : cannot use 'try' with exceptions disabled [ClangExcept.vcxproj]
          try
          ^
  2 errors generated.

According to this document, http://clang.llvm.org/docs/MSVCCompatibility.html: "Exceptions and SEH: Partial". But I can't make enough sense of it.

Has anyone been able to achieve this?

juniel_katarn
  • 330
  • 4
  • 11
  • 1
    You have C++ exceptions disabled in project settings, change the settings. – user1 Oct 01 '15 at 10:08
  • I actually did, but the compiler didn't seem to care. If you take a look at the answer below, my guess is the compiler hard-codes this option so exceptions can't be used for the time being. – juniel_katarn Oct 05 '15 at 04:09
  • This question looks similar to http://stackoverflow.com/questions/24197773/c-program-not-compiling-with-clang-and-visual-studio-2010-express – juniel_katarn Oct 05 '15 at 04:11

2 Answers2

4

The LLVM page you link to says

Exceptions and SEH: Partial. C++ exceptions (try / catch / throw) and structured exceptions (__try / __except / __finally) mostly work on x64. 32-bit exception handling support is being worked on.

But in your link parameters for VC++ you have /MACHINE:X86, and you also pass -m32 to LLVM, which indicates that you try to build a 32-bit application - the model that doesn't work.

Change your settings to build a 64-bit executable instead.

Bo Persson
  • 90,663
  • 31
  • 146
  • 203
  • Thanks. Sadly, I needed specifically 32 bit, but your interpretation makes sense. I gues I'll have to wait to port the code to LLVM. – juniel_katarn Oct 05 '15 at 04:08
  • @Bo - Do you know what version(s) of LLVM Build Tool for Windows supports exceptions for both 32-bit and 64-bit? Each version I tried from the [LLVM downloads page](http://releases.llvm.org/) produces an error, including 3.9.1. – jww Jan 10 '17 at 03:11
  • @jww - Sorry, I don't have any additional information. – Bo Persson Jan 10 '17 at 07:39
0

Your example with exceptions now compile and even run in 32 bits with visual 2015 Community Update 1 using LLVM-3.9.0svn-r258602-win64.exe which can be found here http://sourceforge.net/projects/clangonwin/

tbozo
  • 126
  • 4