473,387 Members | 1,541 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Problem when import C model

Hi, all
I am learning how to import c code in python.
Here is my simple code foo.c:
=====================
#include <Python.h>
void bar()
{
printf("Hello! C wrap!");
}
static PyObject *foo_bar(PyObject *self, PyObject *args) {
/* Do something interesting here. */
bar();
Py_RETURN_NONE;
}
static PyMethodDef foo_methods[] = {
{ "bar", (PyCFunction)foo_bar, METH_NOARGS, NULL },
{ NULL, NULL, 0, NULL }
};
PyMODINIT_FUNC initfoo() {
Py_InitModule3("foo", foo_methods, "My first extension module.");
}
=====================
I use gcc to compile the foo.c:
gcc -shared -I/usr/local/python/2.4.2/include/python2.4 -fPIC foo.c -o
foo.so

Problem is:
I can import foo on linux 64-bit platform if I also run gcc on linux
64-bit platform.
But I can not import foo on linux 64-bit platform if I run gcc on linux
32-bit platform.
Here is the error messege:
=====================
traceback (most recent call last):
File "<stdin>", line 1, in ?
ImportError: ./foo.so: cannot open shared object file: No such file or
directory
=====================

I can not figure out what cause this problem. wrong gcc option? wrong
python code?

Jul 3 '06 #1
2 1469
this is more of a linux question than a python question, so you may get
better luck with asking there.

What you describe makes perfect sense to me. If python is a 64 bit
program, it can only link to 64 bit libraries. If you compiled your
library in 32-bit mode, then the library headers will indicate this,
and linux's library loading code will ignore it when loading libraries
for 64-bit programs.

I think there' might be a trick that lets you compile ELF files with
both 64-bit and 32-bit code, but actually doing so is outside of my
expertise.
ba*******@gmail.com wrote:
Hi, all
I am learning how to import c code in python.
Here is my simple code foo.c:
=====================
#include <Python.h>
void bar()
{
printf("Hello! C wrap!");
}
static PyObject *foo_bar(PyObject *self, PyObject *args) {
/* Do something interesting here. */
bar();
Py_RETURN_NONE;
}
static PyMethodDef foo_methods[] = {
{ "bar", (PyCFunction)foo_bar, METH_NOARGS, NULL },
{ NULL, NULL, 0, NULL }
};
PyMODINIT_FUNC initfoo() {
Py_InitModule3("foo", foo_methods, "My first extension module.");
}
=====================
I use gcc to compile the foo.c:
gcc -shared -I/usr/local/python/2.4.2/include/python2.4 -fPIC foo.c -o
foo.so

Problem is:
I can import foo on linux 64-bit platform if I also run gcc on linux
64-bit platform.
But I can not import foo on linux 64-bit platform if I run gcc on linux
32-bit platform.
Here is the error messege:
=====================
traceback (most recent call last):
File "<stdin>", line 1, in ?
ImportError: ./foo.so: cannot open shared object file: No such file or
directory
=====================

I can not figure out what cause this problem. wrong gcc option? wrong
python code?
Jul 3 '06 #2
cm************@yaho.com <co**********@gmail.comwrote:
What you describe makes perfect sense to me. If python is a 64 bit
program, it can only link to 64 bit libraries. If you compiled your
library in 32-bit mode, then the library headers will indicate this,
and linux's library loading code will ignore it when loading libraries
for 64-bit programs.
32 bit and 64 bit are completely different architectures - you can't
mix and match them in one executable.

The python solution is to use distutils and build a setup.py then you
can compile for each platform easily.
I think there' might be a trick that lets you compile ELF files
with both 64-bit and 32-bit code, but actually doing so is outside
of my expertise.
I don't think so.

You can choose which you compile with. On 32 bit you'll compile 32
bit by default. If you want 64 bit choose -m64, eg

echo <<'#END' >z.c
#include <stdio.h>
int main(void)
{
printf("Hello\n");
return 0;
}
#END
gcc -c -m32 -o z32.o z.c
gcc -c -m64 -o z64.o z.c
file z*.o

gives

z32.o: ELF 32-bit LSB relocatable, Intel 80386, version 1 (SYSV), not stripped
z64.o: ELF 64-bit LSB relocatable, AMD x86-64, version 1 (SYSV), not stripped

--
Nick Craig-Wood <ni**@craig-wood.com-- http://www.craig-wood.com/nick
Jul 3 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Andre Lerche | last post by:
Hi list, I am quite new to Python and try to learn Python with a small pygtk program. I am facing a problem which I am unable to solve for myself. I think I have read the documentation and some...
2
by: Samuele Giovanni Tonon | last post by:
hi, i'm trying to develop a trivial application which random copy files from a directory to another one. i made it using pygtk for the graphical interface, however i find some problem with...
5
by: Greg Vereschagin | last post by:
I'm trying to figure out a regular expression that will match the innermost tag and the contents in between. Specifically, the string that I am attempting to match looks as follows: ...
0
by: Manuzhai | last post by:
Hello there, I have this weird problem with a mod_python application. Recently I installed ElementTree and cElementTree through ez_setup.py, even though they were already installed normally...
1
by: NachosRancheros | last post by:
Ok so I just started to program with Python about a week ago and I am trying to make a program that will take the path of a file and a shortcut command and save it to a text file. Eventually I want...
3
by: shreya | last post by:
While generating client jar files from ant command using build.xml I am getting following error generate-client: Generating client jar for click2pstn.wsdl ... ...
11
by: AZRebelCowgirl73 | last post by:
here is the instructiions I am to use Ask the user to input the number of effective cars in the shop (this number should be between 1 and 100, inclusively). In a loop, controlled by the inputted...
6
by: AZRebelCowgirl73 | last post by:
Here is my problem: I have two java files: One named Car.java and the other named CarDealerApp.java: In the CarDealerApp program, I read in through user input the make, model, year and price of...
6
by: kang jia | last post by:
hi this is my database in models.py in the Django application, however, i don not know where is the error, i have tried many times, but always got error to set up database, sometimes they claim...
2
by: minuji | last post by:
hello everybody, To compile my program I write, javac -classpath "../../.." BeerSelect.java package com.example.web; import com.example.model.BeerExpert; import javax.servlet.*; import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.