Compiling modules in OSX, eg PyUSB?

Philip Semanchuk philip at semanchuk.com
Fri Mar 20 13:23:36 EDT 2009


On Mar 20, 2009, at 12:36 PM, Dr Mephesto wrote:

> windows? well, I thought that maybe the location of the "usb.h" thing
> was relevant, and I didnt see it mentioned on the linux instructions.

Oh, OK. Windows is a pretty different animal from Unix/Linux so it's  
not likely to be of much help here.

>
>
> find /usr -name usb.h -ls   gives me:
> 3545683       24 -rw-r--r--    1 root     wheel        8360 Mar 20
> 16:37 /usr/include/usb.h
> 3538549       24 -rw-rw-r--    1 root     wheel        8360 Feb 22
> 11:28 /usr/local/include/usb.h


Looks good.


> sudo python setup.py install         this gives me:
> pcfr147:pyusb-0.4.1 david$ sudo python setup.py install
> Password:
> running install
> running build
> running build_ext
> building 'usb' extension
> gcc -arch ppc -arch i386 -isysroot /Developer/SDKs/MacOSX10.4u.sdk -
> fno-strict-aliasing -Wno-long-double -no-cpp-precomp -mno-fused-madd -
> fno-common -dynamic -DNDEBUG -g -O3 -I/Library/Frameworks/
> Python.framework/Versions/4.1.30101/include/python2.5 -c pyusb.c -o
> build/temp.macosx-10.3-i386-2.5/pyusb.o
> In file included from pyusb.c:11:
> pyusb.h:6:17: error: usb.h: No such file or directory

OK, I think I see what's going wrong, but I don't understand it. I  
think I was wrong; setup.py doesn't automatically add /usr/include  
and /usr/local/include to the include path when compiling. I'm basing  
this on the documentation here, which states that these paths have to  
be added explicitly:
http://docs.python.org/distutils/setupscript.html#preprocessor-options


However, the two C extensions I distribute compile just fine without  
that, and they certainly rely on /usr/include and /usr/local/include.  
Strange...


But nevermind that. Looking in the setup.py for PyUSB, I see this  
special code added for OS X (a.k.a. Darwin):

elif -1 != platform.find("darwin"):
     extra_link_args = ['-framework',
                        'CoreFoundation',
                        '-framework',
                        'IOKit',
                        '-L/sw/lib']
     extra_compile_args = ['-I/sw/include']


The ['-I/sw/include'] tells the compiler what directories to search  
for include files. The /sw/ tree is specific to Fink, so if you'd used  
Fink to install libusb then the PyUSB setup would have found it. But  
you didn't use Fink (nothing wrong with that; I don't either) and so  
your usb.h landed in /usr/local/include. What you need to do, then, is  
add that directory to the list.

So change line 32 in the PyUSB setup.py from this:
     extra_compile_args = ['-I/sw/include']
to this:
     extra_compile_args = ['-I/sw/include', '-I/usr/local/include']


The same assumption is made about the linker path. Note the '-L/sw/ 
lib'. You'll need to track down your copy of libusb (it's probably in / 
usr/local/lib) and add that to the extra_link_args like so:

     extra_link_args = ['-framework',
                        'CoreFoundation',
                        '-framework',
                        'IOKit',
                        '-L/sw/lib',
                        '-L/usr/local/lib']



Run setup again and I bet you'll be off to the races.


You should certainly report this to the package maintainer, and you  
might also want to point out that the if block starting on line 17 of  
setup.py and the if block starting on line 26 are both for OS X, but  
they lead to different results!


Let us know how it works out,
Philip




More information about the Python-list mailing list