Home > Backend Development > Python Tutorial > Two ways for C++ to call dynamic libraries and Python to call C++ dynamic libraries

Two ways for C++ to call dynamic libraries and Python to call C++ dynamic libraries

WBOY
Release: 2023-04-08 12:45:01
forward
3551 people have browsed it

Create dynamic library

Dynamic library is a library file loaded when the program is running and does not occupy the size of the program itself.

Select dynamic library project:

Two ways for C++ to call dynamic libraries and Python to call C++ dynamic libraries

##Create new .h and .cpp files:

Two ways for C++ to call dynamic libraries and Python to call C++ dynamic libraries

# cat.h
#pragma once
extern "C" _declspec(dllexport) int sum(int a, int b);
Copy after login
# cat.cpp
#include "pch.h"
#include "cat.h"
extern "C" _declspec(dllexport) int sum(int a, int b) {
return a + b;
}
Copy after login

Select the Release version for dynamic library publishing. This example uses ×64 bit.

C Import dynamic library method one

Two ways for C++ to call dynamic libraries and Python to call C++ dynamic libraries

#Create an empty C project and copy the .lib and .dll files in the dynamic library project to the current project :

Two ways for C++ to call dynamic libraries and Python to call C++ dynamic libraries

In the C project, add the dynamic library header file. You don’t need to copy it to the current project, just add the existing item. It only needs to be logically introduced here, but When #include, use the path of the .h file. Both absolute and relative paths are acceptable.

Two ways for C++ to call dynamic libraries and Python to call C++ dynamic libraries

#include 
#include "../../CATDLL/CATDLL/cat.h"
using namespace std;
#pragma comment(lib, "CATDLL.lib")
int main() {
cout << sum(1, 2) << endl;
return 0;
}
Copy after login

    h Header file: Contains information such as data structures, classes, functions and other information declared and output in the dll.
  • lib library file: contains the name and location of the project exported by the DLL. In the application executable file that calls the dll, what is stored is not the function code being called, but the project to be called in the DLL. memory address.
  • dll dynamic library: contains the actual content. When publishing, only the .exe file and the .dll file are required, in the same directory.
Can also be configured in Project-Properties-Linker-Input-Additional Dependencies:

Two ways for C++ to call dynamic libraries and Python to call C++ dynamic libraries

Can be omitted: #pragma comment( lib, "CATDLL.lib")

C Method 2 of importing dynamic library

#include 
#include 
using namespace std;
typedef int (*PSUM)(int, int);
int main() {
HMODULE hMoudle = LoadLibrary(TEXT("CATDLL.dll"));
PSUM psum = (PSUM)GetProcAddress(hMoudle, "sum");
cout << psum(4, 5) << endl;
FreeLibrary(hMoudle);
return 0;
}
Copy after login

Python imports C dynamic library

Since C dll is 64-bit, Python must also use 64-bit Bit.

import os
from ctypes import *
os.chdir("D:Cat课件CAT_CODINGC++项目开发MFC进阶和动态库注入辅助PYTEST")
dll = cdll.LoadLibrary("CATDLL.dll")
ret = dll.sum(1, 2)
print(ret)
Copy after login

In this way, many commonly used functions can be made into dynamic libraries in C and can be called by other languages ​​such as C or Python.

The above is the detailed content of Two ways for C++ to call dynamic libraries and Python to call C++ dynamic libraries. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:51cto.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template