C python extensions, swig

Started by
6 comments, last by Bozebo 13 years, 9 months ago
I have tried to do this a few months ago and I just couldn't get it to work so I gave up.

So, I am just trying to get the simple example working. But to do so there are about 3 billion hurdles.

I am using visual studio 2010 though I have tried before with gcc which wasn't working either.

OK. The example provided with swig is as such:
/* File : example.c *//* A global variable */double Foo = 3.0;/* Compute the greatest common divisor of positive integers */int gcd(int x, int y) {  int g;  g = y;  while (x > 0) {    g = x;    x = y % x;    y = g;  }  return g;}

and
/* File : example.i */%module example%inline %{extern int    gcd(int x, int y);extern double Foo;%}


So it shouldn't be at all complicated?

I start off by running the interface file through swig:
swig.exe -python example.i
Which works to give me my example_wrap.c

It seems the next step is to compile the project with example.c and example_wrap.c in the form of a dll.

So I convert the visual studio 6 project that comes with the example to vs2010 format, everything seems fine, all the settings are kept.

I had to set it to release mode so it could find the right python library (C:\python25\libs\python25.lib). But when I compile, visual studio can't find "tcl.h". tcl.h is included within the swig generated example_wrap.c file.

tcl.h isn't on my machine and no tutorials/guides even remotely speak of it, so I shouldn't really expect it to be a problem and I can't find any known fix for the issue. I notice that there is a tcl directory in my python directory (c:\python25\tcl), but there is just no header anywhere on my machine.

A quick google for tcl.h found it, so I put it in visual studio's include directory and now I am getting syntax errors in example_wrap.c:

Quote:1>ClCompile:
1> example_wrap.c
1>example_wrap.c(780): error C2143: syntax error : missing ')' before '*'
1>example_wrap.c(780): error C2081: 'Tcl_Obj' : name in formal parameter list illegal
1>example_wrap.c(780): error C2143: syntax error : missing '{' before '*'
1>example_wrap.c(780): error C2059: syntax error : ')'
1>example_wrap.c(781): error C2054: expected '(' to follow 'obj'
1>example_wrap.c(833): error C2143: syntax error : missing ')' before '*'
1>example_wrap.c(833): error C2081: 'Tcl_Obj' : name in formal parameter list illegal
1>example_wrap.c(833): error C2143: syntax error : missing '{' before '*'
1>example_wrap.c(833): error C2059: syntax error : '['
1>example_wrap.c(833): error C2059: syntax error : ')'
1>example_wrap.c(834): error C2143: syntax error : missing ')' before '*'
1>example_wrap.c(834): error C2081: 'Tcl_Obj' : name in formal parameter list illegal
...
..
.


They go onwards for about 100 lines. All the errors seem to be somehthing to do with tcl, which I discover via wikipedia is "Tool Command Language" and seems like it would be a component part of what makes python and c extensions work together. But considering not a single tutorial or guide on making python c extensions even mentions tcl, I shouldn't be having a problem with it :S The whole point in using swig is so I can avoid any issues like this isn't it?

If I follow a tutorial or guide it just takes me as far as I have got except things miraculously work without throwing syntax errors in files which I shouldn't have to be worrying about.

I don't know what I can do next to get extensions to work. It shouldn't be even remotely complicated but everything I do just leads to a mess of problems.

Has anybody tackled this problem before?

I could probably do what swig does manually, I am only going to be playing around with a few basic functions so I probably don't need the added complexity. Or would I be wrong about that?
Advertisement
It sounds like SWIG is building a TCL wrapper instead of a Python wrapper. You certainly have no reason to care about TCL, and you shouldn't bother trying to get that to compile. The generated file should be looking for python.h, not tcl.h.

BTW, resist the urge to give up on SWIG and do the binding manually. Good bindings require far too much time and effort, and quick bindings are far too fragile.
Quote:Original post by Sneftel
It sounds like SWIG is building a TCL wrapper instead of a Python wrapper. You certainly have no reason to care about TCL, and you shouldn't bother trying to get that to compile. The generated file should be looking for python.h, not tcl.h.

BTW, resist the urge to give up on SWIG and do the binding manually. Good bindings require far too much time and effort, and quick bindings are far too fragile.


I will look into it deeper. Yes I was thinking that doing binding manually would be a huge workload if I made any significant system on it later.

Right, on to persist with getting swig working...

edit:
Ok I ran swig.exe again and this time I got it to make a python wrapper, that was silly of me in the first place.

But now I have some general issues in my use of vs2010. I can never get the hang of visual studio it has bloat everywhere that I'm not used to setting up.
It is giving me 2 warnings:
Quote:warning MSB8012: TargetPath(C:\Users\Administrator\Desktop\simple\.\example.pyd) does not match the Linker's OutputFile property value (C:\Users\Administrator\Desktop\simple\_example.pyd). This may cause your project to build incorrectly. To correct this, please make sure that $(OutDir), $(TargetName) and $(TargetExt) property values match the value specified in %(Link.OutputFile).
warning MSB8012: TargetName(example) does not match the Linker's OutputFile property value (_example). This may cause your project to build incorrectly. To correct this, please make sure that $(OutDir), $(TargetName) and $(TargetExt) property values match the value specified in %(Link.OutputFile).


And then a few lines down in the build log I have a load of errors relating to linking:
Quote:Link:
1> Creating library .\Release\_example.lib and object .\Release\_example.exp
1>example_wrap.obj : error LNK2019: unresolved external symbol __imp__PyString_AsString referenced in function _SWIG_Python_str_AsChar
1>example_wrap.obj : error LNK2019: unresolved external symbol __imp__PyString_FromString referenced in function _SWIG_Python_str_FromChar
1>example_wrap.obj : error LNK2001: unresolved external symbol __imp__PyExc_RuntimeError
1>example_wrap.obj : error LNK2001: unresolved external symbol __imp__PyExc_AttributeError
...
..
.


I am bad at dealing with these things. I think vs2010 is right in poiting out the different names but I can't quite find where in the project/source file properties I need to change/fix them.

I have changed the output directory from release to just ./ so that means everything is in the right dir but I am still lost.

Oh, I have opted to do it in 2 parts as you will have noticed, by running example.i through swig first rather than a custom build within vs.

Every time I muck this up I have to start fresh >_<

Think I need a sandwich then I can put my brain back to work...

[Edited by - Bozebo on July 9, 2010 9:53:11 PM]
I wouldn't suggest converting the project files forward from VC6. The IDE has changed a lot since then, and projects that do source code processing and other such exotic things are likely to act weird. Just create a project in visual studio, put the source code and the wrapper file in, add python.lib as a dependency (and make sure that your header and library paths are set correctly) and change the output file to end in .pyd instead of .dll (it's under the linker options if I recall correctly).
edit: Skip to the last part of this reply

Nothing I do seems to work in the slightest.

I carefully made a new project and did everything cleanly step by step.

VS is still warning me about possible name mismatches and there could be something to it, but there are so many files and folders it decides to make and work with that I just don't get what it's talking about.

Every time, I start again and do it cleanly from scratch incase I mucked something up, when I get to this point again I try a few different things then get scared of any broken changes I might have made and start fresh again.

I've been trying this for hours and I am now at the stage where no matter what I do I get:
Quote:ClCompile:
1> All outputs are up-to-date.
1>Link:
1> Creating library C:\Users\Administrator\Desktop\simple\_example.lib and object C:\Users\Administrator\Desktop\simple\_example.exp
1>example_wrap.obj : error LNK2001: unresolved external symbol __imp__PyString_FromString
...
..
.

I am aware that the final working extension will be called _example.pyd
I suppose that is a culmination of _example.lib and _example.exp, I am somewhat inexperienced in the whole compilation progress as it is anyway, so getting to grips with this is prooving difficult.

I have opened up the provided project side-by-side with my fresh vs2010 one and checked each property in the project and every file to be sure it is clean and correct.

I have tried putting example.i in as a custom build tool and setting the command line parameter so that it uses swig, which worked fine and got me to the same dead end.

I really can't think of what to try next.

All guides/tutorials do things nearly the same way and they all get past where I am without anything going wrong. I think I can get an older version of visual studio on the msdnaa but that means polluting my system even more with this visual studio bloat that, as of yet, simply complicates any prorgamming I have to do (cygwin is fine for most of my stuff).

I might go back to trying cygwin for this but I originally thought that was the cause of my issues last time I tried to make a python extension because many guides and tutorials complained about it being m$ only, though that seemed to be a perspective from some years back.

edit:
OK, still using vs2010. Here is what I have done (if I remember all the steps as I write this down):
# I made a new empty c dll project
# I set the project properties as such:
- General
-- Output Directory: $(SolutionDir)
-- Intermediate Directory: $(SolutionDir)
-- Target Name: _example
-- Target Extension: .pyd
-- Configuration Type: Dynamic Library (.dll)
- Linker
-- Output File: _example.pyd
-- Additional Dependancies: C:\Python25\libs\python25.lib
# I added example.c to the project as a source file
# I set example.c's properties as such:
- Additional Include Directories: C:\Python25\include
# I ran swig.exe -python C:\Users\Administrator\Desktop\simple\example.i
# I added the resulting example_wrap.c to the project as a source file
# I set example_wrap.c's additional include directories to C:\Python25\include
# I changed the project from debug to release (so it gets python25.lib instead of python25_d.lib which I don't have because I would have to custom compile it)
# I try to build the project and that is as far as I can get:
Quote:1>Link:
1> Creating library C:\Users\Administrator\Desktop\simple\_example.lib and object C:\Users\Administrator\Desktop\simple\_example.exp
1>example_wrap.obj : error LNK2001: unresolved external symbol __imp__PyString_FromString
...
..
.


Any ideas? Has something bugged out or am I doing things wrong? I fear that making a "new empty c dll project" in vs2010 does about 4000 things that I am not aware of.
I don't know what it is but I dislike IDEs and like to stick with my syntax highlighting notepads and my command line compilers. At least, when exploring my own understanding of c or web development etc.

[Edited by - Bozebo on July 10, 2010 2:12:24 AM]
It's a strange error, because python.lib should definitely have that function available. Is yours a clean install of Python? For that matter, is SWIG making a wrapper for Python 2.5, or a later version? (The last probably shouldn't matter, but who knows.)
Can you compile/link it by hand?
Quote:Original post by theOcelot
Can you compile/link it by hand?


I think that is a good next step. I'll look into it soon.

[Edited by - Bozebo on July 11, 2010 12:54:40 AM]

This topic is closed to new replies.

Advertisement