Can I compile a function with gcc and then use it with clang?

I am trying to use SSE4.2 intrinsics with clang/llvm but its not compiling, as I get cannot select intrinsic error from LLVM. On the other hand, the same code compiles flawlessly in gcc. So I thought, maybe I can compile that function with gcc, so as to have an object or library file, and then call that library function in my code, which is compiled by clang/llvm. Would that work?


It's possible to compile an object file with GCC in Linux and convert it to work in Visual Studio. I did this recently running Linux in Virtual Box on Windows converting-c-object-file-from-linux-o-to-windows-obj so this should be possible with Clang on Linux or Windows as well.

So not only can this be done cross compiler it can be done cross platform .

You need to get the calling conventions and the object file format correct (and for C++ the name mangling as well) . With GCC when you compile you can tell it which calling convention/API to use with mabi . Then, if going from Linux to Windows, you need an object file converter to convert from eg ELF on Linux to COFF on Windows. Of course, there are cases this probably won't work (eg if the module relies on a system call that is only in one platform). See the link above for more details.


For any more-or-less complicated c++ code, eg, one that compiles to vtable - the answer is a resounding NO. The two are NOT compatible.

To illustrate the above point, try to compile the Crypto++ library with g++ (gains about 40% speedup for AES/GCM) and then link your clang++-compiled code with it.


It may or it may not work. Some elements of the ABI can be expected to be the same. For example, I believe both g++ and clang use the Itanium ABI name mangling scheme. Others elements can not. So it depends on how complex the code you're compiling is.

Also, I would suggest opening an LLVM bug for the intrinsic that could not be selected. Clang and LLVM have a very active community, and it's possible someone will pick the bug up quickly.

链接地址: http://www.djcxy.com/p/71312.html

上一篇: 如何存储在数据库中并使用JSON传输?

下一篇: 我可以使用gcc编译一个函数,然后在clang中使用它吗?