简体   繁体   中英

connecting c/c++ and python

What I am trying to do is that I want to read a file using python, and then with the data in the file, create a variable in c/c++(I don't want to read var from the file :) ).

Is this possible? If this is possible, then how would you do it?

Thank you guys!

Maybe Boost.Python can help.

You could expose a C++ function to your Python script. Something like that:

void do_sth_with_processed_data(const std::string& data)
{
  // …
}

BOOST_PYTHON_MODULE(do_sth)
{
  def("do_sth_with_processed_data", do_sth_with_processed_data);
}

In your Python script you now could have:

import do_sth

// … 

do_sth_with_processed_data(my_processed_data) // this calls the c++ function

Yes. Open the first file in Python, process it and save the results to a second file.

Then open the second file in your C or C++ program and use the data.

Swig can generate a Python interface to C or C++ code for you automatically. Since it wraps constructors you could read the data in Python and then pass it (with a little care) to the constructor of a C++ class for example.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM