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 impPy_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.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
>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.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
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
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
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=\"%s\"" % metadata['version_info'])
extra_compile_args.append("-Dversion=\"%s\"" % 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?
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
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=\"%s\"" % metadata['version_info'])
extra_compile_args.append("-D__version__=\"%s\"" % metadata['version'])
to
extra_compile_args.append('-Dversion_info="""%s"""' % metadata['version_info'])
extra_compile_args.append('-D__version__="""%s"""' % 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 "pymemcompat.h"
#include "structmember.h"
#include "my_global.h"
#include "mysql.h"
#include "mysqld_error.h"
#include " errmsg.h"
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.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
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 impPy_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.
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.
>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.
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
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=\"%s\"" % metadata['version_info'])
extra_compile_args.append("-Dversion=\"%s\"" % 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?
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)
if I recall that shouldn't be necesary.
great I never got it to compile agains mysql 5, can you give andy a patch so this will be less of a pain
send an email to andy he will take care of it.
No, do NOT mail fixes to me. Put in a bug/patch report through the tracker.