views:

534

answers:

5

Is it possible for gcc to link against a library that was created with Visual C++? If so, are there any conflicts/problems that might arise from doing so?

+1  A: 

I would guess not. Usually c++ compilers have quite different methods of name-mangling which means that the linkers will fail to find the correct symbols. This is a good thing by the way, because C++ compilers are allowed by the standard to have much greater levels of incompatibility than just this that will cause your program to crash, die, eat puppies and smear paint all over the wall.

Usual schemes to work around this usually involve language independent techniques like COM or CORBA. A simpler sanctified method is to use C "wrappers" around your C++ code.

1800 INFORMATION
+1  A: 

It is not possible. It's usually not even possible to link libraries produced by different versions of the same compiler.

Xavier Nodet
A: 

No. Plain and simple :-)

Maximilian
+2  A: 

Some of the comments in the answers here are slightly too generalistic.

Whilst no, in the specific case mentioned gcc binaries won't link with a VC++ library (AFAIK). The actual means of interlinking code/libraries is a question of the ABI standard being used.

An increasingly common standard in the embedded world is the EABI (or ARM ABI) standard (based on work done during Itanium development http://www.codesourcery.com/cxx-abi/). If compilers are EABI compliant they can produce executables and libraries which will work with each other. An example of multiple toolchains working together is ARM's RVCT compiler which produces binaries which will work with GCC ARM ABI binaries.

(The code sourcery link is down at the moment but can be google cached)

tonylo
A: 

Yes, if you make it a dynamic link and make the interface c-style. lib.exe will generate import libraries which are compatible with the gcc toolchain.

That will resolve your linking problems. However that is just the start of the problem.

Your larger problems will be things like exceptions, and memory allocation.

  • You must ensure that no exception cross from VC++ to gcc code, there are no guarantees of compatibility.
  • Every object from the VC++ library will need to live on the heap because:
  • Do not mix gcc new/delete with anything from VC++, bad things will happen. This goes for object construction on the stack too. However, if you make an interface like create_some_obj()/delete_some_obj() you do not end up using gcc new to construct VC++ objects. Maybe make a small handler object that handles construction and destruction. This way you preserve RAII, but still use the c-interface for the true interface.
  • Calling convention must be correct. In VC++ there is cdecl and stdcall. If gcc tried to call an imported function with the wrong calling type, bad things will happen.

The bottom line is keep a simple interface that is ANSI C compliant, and you should be fine. The fact that crazy C++ goes on behind is okay, as long as it is contained.

Oh and make sure all the code is re-entrant, or you risk opening a whole nother can-o-worms.