Home > Article > Backend Development > Two ways for C++ to call dynamic libraries and Python to call C++ dynamic libraries
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:
##Create new .h and .cpp files:# cat.h #pragma once extern "C" _declspec(dllexport) int sum(int a, int b);
# cat.cpp #include "pch.h" #include "cat.h" extern "C" _declspec(dllexport) int sum(int a, int b) { return a + b; }Select the Release version for dynamic library publishing. This example uses ×64 bit. C Import dynamic library method one #Create an empty C project and copy the .lib and .dll files in the dynamic library project to the current project : 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.
#include #include "../../CATDLL/CATDLL/cat.h" using namespace std; #pragma comment(lib, "CATDLL.lib") int main() { cout << sum(1, 2) << endl; return 0; }
#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; }
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)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!