Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Compile on Windows #1

Open
effelsberg opened this issue Oct 28, 2014 · 2 comments
Open

Compile on Windows #1

effelsberg opened this issue Oct 28, 2014 · 2 comments

Comments

@effelsberg
Copy link

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.

@MarcDorval
Copy link

MarcDorval commented Mar 24, 2021

My own fork, compiling using gcc under Windows.
I'm sad I didn't see @effelsberg comments before, I only saw it once done.
It could have saved me 2 days.
https://github.com/MarcDorval/libtclpy

@aidanhs
Copy link
Owner

aidanhs commented Apr 19, 2021

@MarcDorval if you make a PR I'm more than happy to merge it!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants