Which libraries should be linked when Python is embedded in c?

Source: Internet
Author: User

If you have any libraries to connect to when embedding Python in c, you can click our article to view it, I have some knowledge about the libraries that should be linked when I embed Python into c. The following is a detailed introduction of the article. I hope you will have some experience later or later.

 
 
  1. //------------------------------------------   
  2. //cppobj.h   
  3. //   
  4. #include <iostream>   
  5. using namespace std;   
  6. class cppobj {   
  7. private :   
  8. string s;   
  9. public :   
  10. cppobj(string a): s(a) {};   
  11. string show() {return s;}   
  12. };   
  13. //-----------------------------------------   
  14. //------------------------------------------   
  15. //cppobj.i   
  16. %module cppobj   
  17. %include "std_string.i"   
  18. %{   
  19. #include "cppobj.h"   
  20. %}   
  21. %include "cppobj.h"   
  22. //------------------------------------------   
  23. //------------------------------------------   
  24. // Makefile   
  25. _cppobj.so: cppobj_wrap.o   
  26. g++ -o $@ $< -shared -fpic -lpython24   
  27. cppobj_wrap.cxx: cppobj.i cppobj.h   
  28. swig -c++ -python cppobj.i   
  29. cppobj_wrap.o: cppobj.h cppobj_wrap.cxx   
  30. g++ -c cppobj_wrap.cxx -fpic -IC:/Python24/include   
  31. clean:   
  32. rm -rf cppobj_wrap.cxx _cppobj.so *.o cppobj.py cppobj.pyc   
  33. //------------------------------------------   

When we embed Python into c, we need to link the first part of the relevant code of the library.

 
 
  1. $ mv _cppobj.so _cppobj.pyd  

Then you can

 
 
  1. >>> from cppobj import cppobj   
  2. >>> c = cppobj('Hello')   
  3. >>> c.show()   
  4. 'Hello'  

I hope you can take notes from a long time ago. Top-up is an introduction to the Code related to the link library when you embed Python into c. I hope you will gain some benefits.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.