Menu

For those wanting to compile on windows

Help
2006-09-23
2012-09-19
  • Daniel Eloff

    Daniel Eloff - 2006-09-23

    I put together these notes a while ago when I compiled mysql-python with visual studio 2003.

    I just used it now to build 1.2.2b1 for python 2.5, I had a working build in under 5 minutes. Since it has been so useful to me, I wish to share it here.

    Compiling mysql-python using Visual Studio 2003
    You should first add both the python and mysql include and libs directories to your project
    I have them in my global settings for visual studio, so I don't document that here.

    I'll document all the errors I get in the process and how I fix them below.

    Added these at the top:

    define version_info "(1,2,1,'final',2)"

    define version "1.2.1_p2"

    Or for 1.2.2b1:

    define version_info "(1,2,2,'beta',1)"

    define version "1.2.2b1"

    Got this error still:
    _mysql.c(43): fatal error C1083: Cannot open include file: 'my_config.h': No such file or directory

    Did a bit of digging, windows doesn't have this file anymore, it should suffice to include "my_globals.h". Also on windows you HAVE to include files in this order:

    include <my_global.h>

    include <m_string.h>

    include "mysql.h"

    In light of this I changed the #ifdef MS_WIN32 to:

    ifdef MS_WIN32

    include <my_global.h>

    include <m_string.h>

    include "mysql.h"

    include <windows.h>

    else

    include "mysql.h"

    include "my_config.h"

    endif / MS_WIN32 /

    (and removed the lines:

    include "mysql.h"

    include "my_config.h"

    )

    Now asside from a dozen warnings I get:
    mysqldb fatal error LNK1104: cannot open file 'python24_d.lib'

    A quick check turns up that python24_d.lib doesn't exist in my python install. If I don't have it, others probably don't either, I'm using an activestate distro. Digiing turns up that it's referenced in pyconfig.h for MSVC debug builds. That's a small oversight to reference it but not include it..., easy way around is to copy python24.lib and rename the copy to python24_d.lib.

    Now I have to add mysqlclient.lib and wsock32.lib to the build and remove libcmt.lib (because it seems both it and the debug version are being included somehow?)

    Left with 4 unresolved externals:
    mysqldb error LNK2019: unresolved external symbol impPy_Dealloc referenced in function mysql_Exception
    mysqldb error LNK2019: unresolved external symbol
    imp
    Py_NegativeRefcount referenced in function
    mysql_Exception
    mysqldb error LNK2001: unresolved external symbol imp_Py_RefTotal
    mysqldb error LNK2019: unresolved external symbol impPy_InitModule4TraceRefs referenced in function _init_mysql

    Ah more digging turns up that without a debug version of the python24.lib and of the interpreter you cannot create a debug build. Changed to release build, and it works.

     
    • Andy Dustman

      Andy Dustman - 2006-09-25

      The 1.2 branch in subversion has a fix for compiling with Windows so that you don't need to add the version information.

      http://svn.sourceforge.net/viewvc/mysql-python/branches/MySQLdb-1.2/

      This will be in 1.2.2b2 sometime in the near future.

       
    • Martin v. Löwis

      >That's a small oversight to reference it but not
      >include it..., easy way around is to copy
      >python24.lib and rename the copy to python24_d.lib.

      python24_d.lib is only referenced if _DEBUG is defined, i.e. if you are building a debug version. You should then also link with python24_d.dll (which will import all modules in their _d version); all these aren't shipped.

      For the release builds, you just shouldn't define _DEBUG, and then python24_d.lib won't be referenced. If you want to do a debug build, you have to recompile Python yourself.

       
    • Kael Fischer

      Kael Fischer - 2006-10-01

      Daniel and Andy,

      Any chance we could get Daniel's binary up on the sorceforge download area? I'll be able to compile it tomorrow when I get to work, but I'm sure it would help a lot of people.

      Regarding Windows compilers: despite what the 1.2.1_p2 README says, I think (?) that since Python2.4 gcc is not supported for compiling python extensions.

      Kael

       
    • Nikalren

      Nikalren - 2006-10-05

      I was able to fix the version information problem by making the flag triple quoted; this makes the MSVC linker interpret it as a string. That way, you don't need to add a #define in the source for the version info.

      Change extra_compile_args on lines 86 and 87 of setup.py from
      extra_compile_args.append("-Dversion_info=\&quot;%s\&quot;" % metadata['version_info'])
      extra_compile_args.append("-Dversion=\&quot;%s\&quot;" % metadata['version'])
      to
      extra_compile_args.append('-Dversion_info="""%s"""' % metadata['version_info'])
      extra_compile_args.append('-Dversion="""%s"""' % metadata['version'])

      I also had to change the headers to use my_global as you mentioned; my current headers are just

      include "pymemcompat.h"

      include "structmember.h"

      include "my_global.h"

      include "mysql.h"

      include "mysqld_error.h"

      include "errmsg.h"

      Compiling against MySQL 4.1 worked with just these modifications. MySQL 5.0 was a bit more compilated -- I had to add "extra_link_args: /NODEFAULTLIB:msvcrt" to site.cfg to remove a conflict with libcmt, and add user32 to the library list.

      I've got binary installers working for MySQL 4.1 and MySQL 5.0 on Python 2.4 and Python 2.5. Is there somewhere I can upload them so that others can use them?

       
      • elpargo

        elpargo - 2006-10-09
        I was able to fix the version information problem by making the flag triple
        quoted; this makes the MSVC linker interpret it as a string. That way, you don't
        need to add a #define in the source for the version info.
        
        Change extra_compile_args on lines 86 and 87 of setup.py from
        extra_compile_args.append(&quot;-Dversion_info=\&quot;%s\&quot;&quot; % metadata['version_info'])
        extra_compile_args.append(&quot;-D__version__=\&quot;%s\&quot;&quot; % metadata['version'])
        to
        extra_compile_args.append('-Dversion_info=&quot;&quot;&quot;%s&quot;&quot;&quot;' % metadata['version_info'])
        extra_compile_args.append('-D__version__=&quot;&quot;&quot;%s&quot;&quot;&quot;' % metadata['version'])
        

        Huh? I though that went I build the last version this was fix.
        if you put a raw string in there it will also fix it, that is how I made it compile last time (can't remenber the version but is the last binary)

        I also had to change the headers to use my_global as you mentioned; my current
        headers are just
        
        #include &quot;pymemcompat.h&quot;
        #include &quot;structmember.h&quot;
        #include &quot;my_global.h&quot;
        #include &quot;mysql.h&quot;
        #include &quot;mysqld_error.h&quot;
        #include &quot; errmsg.h&quot;
        

        if I recall that shouldn't be necesary.

        Compiling against MySQL 4.1 worked with just these modifications. MySQL 5.0
        was a bit more compilated -- I had to add &quot;extra_link_args: /NODEFAULTLIB:msvcrt&quot;
        to site.cfg to remove a conflict with libcmt, and add user32 to the library
        list.
        

        great I never got it to compile agains mysql 5, can you give andy a patch so this will be less of a pain

        I've got binary installers working for MySQL 4.1 and MySQL 5.0 on Python 2.4
        and Python 2.5. Is there somewhere I can upload them so that others can use
        them?
        

        send an email to andy he will take care of it.

         
        • Andy Dustman

          Andy Dustman - 2006-10-09

          No, do NOT mail fixes to me. Put in a bug/patch report through the tracker.

           

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.