Mixed programming of c/c++ and python 3

Python calls c/c++ functions

We have already used python to call a c function to output "Hello, world" in the Linux environment. Now we will add mutual parameter-passing functions.

#include <stdio.h>

void sayhi()
{
    
    
        printf("Hello, world.\n");
}

int plusplus(int a, int b) {
    
    
        printf("%d + %d ", a, b);
        return a + b;
}

Then we can directly pass the parameters

>>> from ctypes import *
>>> lib = cdll.LoadLibrary("./libcfun.so")
>>> a = lib.plusplus(1, 3)
1 + 3 >>> a
4
>>>

Because there is not much difference between the functions of C++ and those of C, but if compiling the C library requires extern C compilation, simply calling the functions is in C style.
Next we will call the c/c++ executable program

#include <stdio.h>

int main() {
    
    

        int i;
        printf("请输入:");
        scanf("%d", &i);
        printf("%d + 1 = %d\n", i, i+1);
        return 0;
}

The above is a simple executable program with simple input and output. Then our call in python is just like calling a shell command.

>>> import os
>>> os.system('./test')
请输入:3
3 + 1 = 4
0
>>>

However, the extra 0 seems to be due to the return 0 in the C program. Because I modify different return values, its output will change accordingly. The same steps also have the same output in the windows environment.
Insert: My windows uses the mingw compiler, so there are many pitfalls in generating dynamic libraries. For specific generation, you can use the project generation of vs (what others said).
Here is the previous failure to call the dynamic library in the windows environment. Update, use vs2017 to generate a dynamic library.
The operation is as follows:
Insert image description here
click OK and then click Finish to go to this interface and check "DLL" and "Empty Project"
Insert image description here
and then add the source file.

#include <iostream>

extern "C" __declspec(dllexport) void sayhi() {
    
    
	std::cout << "Hello, world." << std::endl;
}

Because what I edit and generate is a c++ function, I need to add extern "C" __declspec(dllexport) to the function header to indicate that this is generating a c function library. In addition, the corresponding project also needs to check the 64-bit project
Insert image description here
and then we will generate our dynamic Just use the library. Note that it is generated, not compiled.
Insert image description here
Then open it in the project's generated file directory and import the dynamic library. Now many right-click menus can add an "open cmd here" command option, which is very convenient. Everyone You can check how to do it.
Insert image description here
I directly tested it on the interactive programming interface of python. The results are as follows. It is indeed possible.

>>> from ctypes import *
>>> lib = cdll.LoadLibrary("./pcall.dll")
>>> lib.sayhi()
Hello, world.
1404507280
>>>

Guess you like

Origin blog.csdn.net/weixin_44948269/article/details/121673016