简体   繁体   中英

calling int main() from python

I have a working python wrapper for C++ code (as suggested here Calling C/C++ from python? ) using ctypes. But the problem is with main function of the code. When I do something like

         extern "C" {
             void call_main(){ main();}
         }

in my c++ code and then call this function via python wrapper

            ...
            lib = cdll.lib('./mylib.so')
            def run():
                lib.call_main()

-> I get "segmentation fault".

The funny part is that when i copy paste my main method code into function called eg test (so it is int test() {....#pasted code...} in c++ code), extern it and then call lib.test()

=> And eveything works fine... So it must be a problem with the main function being called main or something

In C++ calling main() recursively is not allowed ( see 3.6.1, basic.start.main, paragraph 3). Also, you need a C++ aware entry point when you want to call C++ functionality. You can sometimes get away with calling C++ functionality without this but what is going to work and what is not isn't entirely straight forward. The obvious problem is with global objects needing initialization.

Just put the code you want to call into a different function and call this.

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