473,408 Members | 2,734 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,408 software developers and data experts.

Calling C++ function from python script

The module which i am creating is like

Part A:
1. It does some processing by using python code.
2. The result of this python code execution is written to a text file.
[This part is already compelete]]

Part B:
1. I read a text file which is outputted by above python script in a
C++ program
2. and again output of this c++ code is text file
[This part is as well complete with graph data structure construction
part involved, which i feel can be done in better way in C++ than in
python]

Now i want to integrate this flow.

The communication between part A and part B is by call to a function
present in C++
How should i do that?
Kindly suggest some ways.

Regards
Pankaj

Jan 28 '06 #1
7 12975

Pankaj wrote:
The module which i am creating is like

Part A:
1. It does some processing by using python code.
2. The result of this python code execution is written to a text file.
[This part is already compelete]]

Part B:
1. I read a text file which is outputted by above python script in a
C++ program
2. and again output of this c++ code is text file
[This part is as well complete with graph data structure construction
part involved, which i feel can be done in better way in C++ than in
python]

Now i want to integrate this flow.

The communication between part A and part B is by call to a function
present in C++
How should i do that?
Kindly suggest some ways.

Regards
Pankaj


If the bulk of your code is in Python, write an extension module in
C++. You can use Swig or Boost here.
If the bulk of your code is in C++, embed Python.
The general rule is "extend, not embed"

Python documentation
http://docs.python.org/ext/ext.html
Unless you know C/C++ well, these are not easy topics.

Boost
http://www.boost.org/libs/python/doc...tml/index.html
Swig, SIP are other good alternatives.
All the above are relatively easy.

If your C++ can be simplified to C, Pyrex is remarkably easy to write
extensions in. In this case you will import/include a C header
containing your function and write a Pyrex wrapper around it rather
than directly using Pyrex as a language.

If you are on MS Windows, you can always write a COM server in either
language (COM in C++ is not easy unless if you are using something like
C++ Builder).

Or you can simply use popen from Python or use pipes or just read
stdout from either side.

As you can see, there are a number of options.

Jan 28 '06 #2
In my case, i want to use python script as parent script and call C++
function from within the python script.

If this is the case, then what am i doing?
1. extending
2. embedding.

?
Ravi Teja wrote:
Pankaj wrote:
The module which i am creating is like

Part A:
1. It does some processing by using python code.
2. The result of this python code execution is written to a text file.
[This part is already compelete]]

Part B:
1. I read a text file which is outputted by above python script in a
C++ program
2. and again output of this c++ code is text file
[This part is as well complete with graph data structure construction
part involved, which i feel can be done in better way in C++ than in
python]

Now i want to integrate this flow.

The communication between part A and part B is by call to a function
present in C++
How should i do that?
Kindly suggest some ways.

Regards
Pankaj


If the bulk of your code is in Python, write an extension module in
C++. You can use Swig or Boost here.
If the bulk of your code is in C++, embed Python.
The general rule is "extend, not embed"

Python documentation
http://docs.python.org/ext/ext.html
Unless you know C/C++ well, these are not easy topics.

Boost
http://www.boost.org/libs/python/doc...tml/index.html
Swig, SIP are other good alternatives.
All the above are relatively easy.

If your C++ can be simplified to C, Pyrex is remarkably easy to write
extensions in. In this case you will import/include a C header
containing your function and write a Pyrex wrapper around it rather
than directly using Pyrex as a language.

If you are on MS Windows, you can always write a COM server in either
language (COM in C++ is not easy unless if you are using something like
C++ Builder).

Or you can simply use popen from Python or use pipes or just read
stdout from either side.

As you can see, there are a number of options.


Jan 28 '06 #3
Extending

Jan 28 '06 #4
The examples given are too complicated.

So, if it can be explained using my sample example. Would be thankful
for that.

/***** 1.c File ******/
func( char a[10] )
{
int i;
for( i =0; i < 10; i++)
printf("\n array element is: %c", a[i]);
}

/***** 1.py File ******/

f = open( "x.txt", "r")
while 1:
l = f.readline( )

=>> Here i want to call func() as
func( l)

Jan 28 '06 #5
Right. They are complicated(Assuming that you are reading Python API
docs), especially if you have not done a substantial amount of C. You
spent 8 minutes trying to understand them. What did you expect? People
spend days sorting out the API issues.

If you are in a hurry, Pyrex is easiest approch to write Python
extensions. Try that instead.
http://www.cosc.canterbury.ac.nz/~greg/python/Pyrex/

Another approach... create a dll and call it from ctypes. Nothing to
learn (assuming you understand C calling conventions)

Jan 29 '06 #6
See, i tell u, nothing is difficult in this world.

I achieved the thing which i was asking for.

Thanks for the advice.

I used this paper:
http://www.swig.org/tutorial.html

for creating python modules from C code.

Which is what i needed. In order to interface both things, we need
convert atleast one thing to other so i choosed to convert C code to
python module.

The steps which i followed were:
These steps were to create a python module from C code. So a mandatory
condition for .c file is : it should not have main function, and any
variable in any function called from main function should be declared
global.

1. Creating wrapper from .i :
swig -python TestCase.i (where TestCase.i is interface file containing
declarations of functions and variables)
2. Creating .o's :
gcc -c TestCase.c TestCase_wrap.c (TestCase_wrap.c is file genereted
by swig and is a wrapper for creating a python module)
3. Shared Library:
ld -shared TestCase.o TestCase_wrap.o TestCase.so

Module was inserted as:
1. export LD_LIBRARY_PATH=$PWD:$LD_LIBRARY_PATH
2. python Tc.py
( In this python file i did: "import TestCase" and then used it as
"TestCase.main_module()"

**********************************

So, we don't need to write any code to do this.

Jan 29 '06 #7
One small thing was incorrect there.

gcc -c TestCase.c TestCase_wrap.c -I /usr/include/python2.2/

This include path is required for Python.h file

Jan 29 '06 #8

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

Similar topics

18
by: bart_nessux | last post by:
I need a script to call several functions at the same time. How does one call more than one function simultaneously?
11
by: Kamilche | last post by:
What a debug nightmare! I just spent HOURS running my script through the debugger, sprinkling in log statements, and the like, tracking down my problem. I called a function without the ending...
1
by: delrocco | last post by:
Hello, I really appreciate anyone who has time to read this and help, Thanks up front. I'm very new to python, having picked it up for the first time a week ago, but I feel I'm very close to...
3
by: David Bear | last post by:
I have a hash function written by another organization that I need to use. It is implemented in perl. I've been attempting to decode what they are doing in their hash function and it is taking way...
5
by: Shuaib | last post by:
Hi! I have a python script which returns an Integer value. How do I call this script from a C programe, and use the result returned? Thanks for your time.
1
by: joeedh | last post by:
Hi I'm getting extremely odd behavior. First of all, why isn't PyEval_EvalCode documented anywhere? Anyway, I'm working on blender's python integration (it embeds python, as opposed to python...
3
by: Anthony Smith | last post by:
In my php page I am calling a Python cgi. The problem is that originally the Python script was being called directly and it could access the environment variables that were being set. Now since the...
4
by: Quill_Patricia | last post by:
I have a Python script which is used to load data into a database. Up to now this script has been run by customers from the Windows command prompt using "python edg_loader.pyc". Any error messages...
9
by: Catherine Moroney | last post by:
I have one script (Match1) that calls a Fortran executable as a sub-process, and I want to write another script (Match4) that spawns off several instances of Match1 in parallel and then waits...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.