libtclpy icon indicating copy to clipboard operation
libtclpy copied to clipboard

Compile on Windows

Open effelsberg opened this issue 9 years ago • 2 comments

This is a very basic but nevertheless working build instruction for libtclpy under Windows. My setup is a Windows Vista machine with Visual Studio 2008 Express, ActiveTcl 8.6 and Python 2.7 from python.org.

(Edit 2014-11-01: Added tcc support)

(Edit 2014-11-10: VStudio 2012 Express on Windows 7 works as expected with the same batch file)

About compiling on Windows, especially with the Visual Studio compiler

The Microsoft compiler is not fully C99 compliant, therefore I had to compile in C++ mode (the /TP option). Test for a defined _MSC_VER to detect the VStudio compiler or _WIN32 for Windows in general.

Adjustments to tclpy.c

.1. Generally replace the index function by strchr (even for Unix systems). You must assign to a const pointer for that.

.2. Line 415 needs a cast (at least for VStudio 2008)

Tcl_Interp *interp = (Tcl_Interp *)PyCapsule_Import("tclpy.interp", 0);

.3. Remove the #include <dlfcn.h> line and the dlopen line for a Windows build.

.4. The two exported functions must be written in this way:

#ifdef _WIN32
#define DLLEXPORT __declspec(dllexport)
#else
#define DLLEXPORT
#endif
#ifdef __cplusplus
extern "C" {
#endif
DLLEXPORT int Tclpy_Init(Tcl_Interp *interp);
DLLEXPORT int init_python_tclpy(Tcl_Interp* interp);
#ifdef __cplusplus
}
#endif

Compile script

This is a very basic script that is supposed to be placed in the project's root directory and called from a Visual Studio command line.

:: build.bat
:: Creates libtclpy.0.3.dll in this folder.
:: Adjust includes and libs if you don't have a default installation of Tcl and Python.
:: Run from a Visual Studio command line
:: > build vstudio
:: > build test
:: Or with tcc
:: > build tcc
:: > build test
setlocal

@if '%1' == 'vstudio'  goto vstudio
@if '%1' == 'tcc'      goto tcc
@if '%1' == 'test'     goto test
@if '%1' == 'clean'    goto clean

@echo USAGE  build ^<TARGET^>
@echo TARGET vstudio, tcc, test or clean
@goto end

:vstudio
set defines=/DPACKAGE_VERSION=\"0.3\"
set includes=/IC:\Tcl\include /IC:\Python27\include
set libs=/LIBPATH:C:\Tcl\lib /LIBPATH:C:\Python27\libs tcl86.lib python27.lib
cl generic\tclpy.c /TP /LD %defines% %includes% /link %libs% /OUT:libtclpy0.3.dll
echo package ifneeded tclpy 0.3 [list load [file join $dir libtclpy0.3.dll] tclpy]> pkgIndex.tcl
goto end

:tcc
set defines=-DPACKAGE_VERSION=\"0.3\"
set includes=-IC:\Tcl\include -IC:\Python27\include
set libs=-LC:\Tcl\bin -LC:\Python27\libs -ltcl86 -lpython27
tcc generic\tclpy.c %defines% %includes% %libs% -shared -olibtclpy0.3.dll
echo package ifneeded tclpy 0.3 [list load [file join $dir libtclpy0.3.dll] tclpy]> pkgIndex.tcl
goto end

:test
set TCLLIBPATH=.
tclsh tests\tclpy.test
goto end

:clean
echo cleaning
del tclpy*
del libtclpy*
goto end

:end

Result

All tests except for types-1.5 pass.

effelsberg avatar Oct 28 '14 20:10 effelsberg