Komnomnomnom / swigibpy

Third party Interactive Brokers Python API generated from TWS C++ API using SWIG.
http://github.com/Komnomnomnom/swigibpy/
Other
155 stars 36 forks source link

MFC compilation error on Windows using Visual Studio compiler #17

Closed BCSharp closed 11 years ago

BCSharp commented 11 years ago

This is actually a bug in the Interactive Brokers code. They have mixed up _MFC_VER with _MSC_VER. The default distutils setup uses the VS compiler and this does not compile the IB code (issue mentioned in README.1st) since the IB code insists on linking with MFC. I don't think that linking a Python package with MFC is a good idea.

The fix is actually easy (see patch below) but I don't know whether you want to apply it since it modifies 3rd party code. Recently, I have submitted the patch directly to IB, let's see whether they accept it.

For myself, I am keeping it in a Mercurial queue and applying it on every new API release. The problem dates from many API releases ago.

swigibpy: 0.3 python: 3.3.0 C/C++ compiler: VS 10.0 (aka Visual Studio 2010, aka _MSC_VER 1600) TWS: 931.3 OS: Windows 7 64-bit

diff --git a/IB_966/Shared/EClientSocketBaseImpl.h b/IB_966/Shared/EClientSocketBaseImpl.h
--- a/IB_966/Shared/EClientSocketBaseImpl.h
+++ b/IB_966/Shared/EClientSocketBaseImpl.h
@@ -415,7 +415,7 @@
 }

-#ifdef _MSC_VER
+#ifdef _MFC_VER
 static IBString errMsg(CException *e) {
        // return the error associated with this exception
        char buf[1024];
@@ -2105,7 +2105,7 @@
                beginPtr = ptr;
                return processed;
        }
-#ifdef _MSC_VER
+#ifdef _MFC_VER
        catch( CException* e) {
                m_pEWrapper->error( NO_VALID_ID, SOCKET_EXCEPTION.code(),
                        SOCKET_EXCEPTION.msg() + errMsg(e));
@@ -3199,7 +3199,7 @@
                return processed;
        }

-#ifdef _MSC_VER
+#ifdef _MFC_VER
        catch( CException* e) {
                m_pEWrapper->error( NO_VALID_ID, SOCKET_EXCEPTION.code(),
                        SOCKET_EXCEPTION.msg() + errMsg(e));
diff --git a/IB_966/Shared/StdAfx.h b/IB_966/Shared/StdAfx.h
--- a/IB_966/Shared/StdAfx.h
+++ b/IB_966/Shared/StdAfx.h
@@ -7,7 +7,9 @@
 #define assert ASSERT
 #define snprintf _snprintf

+#ifdef _MFC_VER
 #include <afxwin.h>
+#endif

 #endif
Komnomnomnom commented 11 years ago

Thanks @Koneski. I'm not that familiar with building on Windows, I work on Linux and OSX myself but others reported what worked so I included it in the README.

I'll have a think about maybe adding a patching step to the swigibpy build process, I'm kinda hesitant though as it's a bit of a slippery slope and I want to avoid altering the TWS code if at all possible.

BCSharp commented 11 years ago

I understand your hesitation. Besides, the patch has to be modified each API release to be applied without fuzz as the code lines move with each release. I posted the patch here just in case somebody wants to compile/install swigibpy on Windows using MSVC.

I am moving my swigibpy-related development from Windows to OS X (development) and Linux (production) environment. I still compile IB-API on Windows (with the patch) for my .NET-based application.

Komnomnomnom commented 11 years ago

I was forced to include a couple of TWS patches recently (to fix compilation under gcc 4.7 and to support shared pointers #7) so I've decided to include this as well.

I think the stance on patches will be that they will be considered for inclusion if:

  1. they are trivial fixes
  2. they are easy to maintain from release to release

Thanks again for the patch @Koneski!