Compiling via bitcode on Windows

I’m trying to use Clang on Windows to compile C++ to bitcode and then to an executable (with a view to carrying out some experiments on whole program optimization of the bitcode).

When I use clang.exe, even with -D_HAS_EXCEPTIONS=0 -fms-compatibility -fms-extensions, I get errors:

main-621e97.o : error LNK2019: unresolved external symbol _Unwind_Resume referenced in function main
main-621e97.o : error LNK2001: unresolved external symbol __gxx_personality_v0
main-621e97.o : error LNK2019: unresolved external symbol __cxa_call_unexpected referenced in function “public: class std::_Vector_iterator<class std::_Vector_val<struct std::_Simple_types<class std::basic_string<char,struct std::char_traits,class std::allocator > > > > __cdecl std::vector<class std::basic_string<char,struct std::char_traits,class std::allocator >,class std::allocator<class std::basic_string<char,struct std::char_traits,class std::allocator > > >::begin(void)” (?begin@?$vector@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@V?$allocator@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@2@@std@@QEAA?AV?$_Vector_iterator@V?$_Vector_val@U?$_Simple_types@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@std@@@std@@@2@XZ)
a.out : fatal error LNK1120: 3 unresolved externals

clang-cl.exe avoids those errors and can generate an executable from C++, but it doesn’t seem to have -emit-llvm or an equivalent option.

What am I missing?

The references to _Unwind_Resume and __gxx_personality_v0 are surprising, but the rest make sense to me. If you don’t need any EH, just pass -fno-exceptions to fix the issue. For more info, read on. :slight_smile:

The references to _Unwind_Resume and __gxx_personality_v0 are surprising,
but the rest make sense to me. If you don't need any EH, just pass
-fno-exceptions to fix the issue. For more info, read on. :slight_smile:

That fixed it, thanks!

clang-cl currently turns on -fexceptions and not -fcxx-exceptions, while
clang will enable both by default. Long term, to implement the semantics of
MSVC's /EHs, we want to disable C++ dtor cleanups while leaving SEH (__try
/ __except) enabled.

And in the even longer term, implement the /EHsc option?

Yes. Strangely enough, our current IR generation is doing something like
/EHa, which is not a priority. =/ Should be fixed soon.

clang-cl.exe avoids those errors and can generate an executable from C++,
but it doesn't seem to have -emit-llvm or an equivalent option.

FYI, you can use this with clang-cl:
clang.cl.exe -Xclang -emit-llvm /c ...

If you don't need to use Microsoft back end stuff, I've had good luck with using mingw-64 headers and libraries with clang/LLVM to compile for Windows. I've even used it to compile itself for Windows. clang/LLVM, binutils, and gdb all build just fine. A side benefit is that you can also compile for various Linux targets using the same tool chain on Windows. Binary downloads for both 32 and 64 bit Windows systems and various Linux hosts are available from http://ellcc.org/blog/?page_id=20295 The downloads include pre-built libraries for all the target systems.

-Rich