cmake 编译 c++ dll 的一个例子(更新2:增加 python 调用方法)

CMakeLists.txt

project(xxx)
add_library(xxx SHARED xxx.cpp)
add_executable(yyy yyy.cpp)
target_link_libraries(yyy xxx)

xxx.h

#ifndef XXX_XXX_H
#define XXX_XXX_H
#endif

#pragma once
#ifdef BUILD_XXX_DLL
#define IO_XXX_DLL __declspec(export)
#else
#define IO_XXX_DLL __declspec(import)
#endif

extern "C"
{
IO_XXX_DLL void hello(void);
IO_XXX_DLL double sum(double a, double b);
}

xxx.cpp

#define BUILD_XXX_DLL

#include "xxx.h"
#include <iostream>

using namespace std;

IO_XXX_DLL void hello(void)
{
    cout<<"Hello from dll!\n"<<endl;
}

IO_XXX_DLL double sum(double a, double b)
{
    cout<<"The sum of "<<a<<" and "<<b<<" is: "<<a + b<<endl;
    return a + b;
}

yyy.cpp

#include <windows.h>
#include <iostream>

using namespace std;

int main()
{
    HINSTANCE h = LoadLibrary("C:\\Users\\Perelman\\.CLion2016.1\\system\\cmake\\generated\\xxx-4d5c076f\\4d5c076f\\Debug\\libxxx.dll");
    typedef void (*p0)(void);
    p0 hello = (p0)GetProcAddress(h, "hello");
    cout<<"Run hello:\n"<<endl;
    hello();
    typedef double (*p1)(double, double);
    p1 sum = (p1)GetProcAddress(h, "sum");
    cout<<"Run sum:\n"<<endl;
    sum(10.0, 12.0);
    FreeLibrary(h);
    return 0;
}

1

yyy.py

import ctypes
# 注意一,注意 64bit 32bit 的问题,如果出现 193 错误:%1 不是有效的Win32应用程序,则见编译器由 64bit 更换为 32bit 即可
# 注意二,由于 dll 里是 cdecl 接口(如 extern “C” __declspec(dllimport)),因此 python 里用 ctypes.CDLL 接口
h = ctypes.CDLL('C:\\Users\\Perelman\\.CLion2016.1\\system\\cmake\\generated\\xxx-4d5c076f\\4d5c076f\\Debug\\libxxx.dll')
h.hello()
# 注意三,python 里的数据要经过 ctypes 处理下
# 指明参数格式
h.sum.argtypes = (ctypes.c_double, ctypes.c_double)
# 指明返回格式
h.sum.restype = ctypes.c_double
print(h.sum(10.1, 3))

3

3

转载于:https://www.cnblogs.com/blog-3123958139/p/5575360.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在 C++ 项目中使用 HslCommunication 库,您需要遵循以下步骤: 1. 下载 HslCommunication 库的源代码,并将其添加到您的 C++ 项目中。您可以将其作为子目录添加到您的项目中,或者将其作为外部依赖项添加到您的项目中。 2. 在您的 CMakeLists.txt 中添加以下内容,以编译 HslCommunication 库: ```cmake # 添加 HslCommunication 库的子目录 add_subdirectory(HslCommunication) # 根据需要设置编译选项 set(CMAKE_CXX_STANDARD 11) # 添加您的项目源文件 add_executable(MyExecutable main.cpp) # 链接 HslCommunication 库 target_link_libraries(MyExecutable HslCommunication) ``` 这将编译 HslCommunication 库,并将其链接到您的可执行文件中。 3. 在您的 C++ 代码中,您可以使用 HslCommunication 库中的类和函数。例如,以下是一个示例代码,使用 ModbusTcpClient 类从 ModbusTCP 设备中读取数据: ```cpp #include <iostream> #include "HslCommunication.h" using namespace std; using namespace HslCommunication; int main() { // 创建 ModbusTcpClient 对象 ModbusTcpClient^ client = gcnew ModbusTcpClient("192.168.1.100", 502); // 连接到设备 OperateResult connectResult = client->ConnectServer(); if (!connectResult.IsSuccess) { cout << "连接到设备失败:" << connectResult.Message << endl; return 1; } // 读取设备数据 OperateResult<byte[]> readResult = client->Read(1, 0, 10); if (!readResult.IsSuccess) { cout << "读取数据失败:" << readResult.Message << endl; return 1; } // 输出读取的数据 for (int i = 0; i < readResult.Content->Length; i++) { cout << "Data[" << i << "]: " << (int)readResult.Content[i] << endl; } // 断开连接 client->ConnectClose(); return 0; } ``` 这将创建一个 ModbusTcpClient 对象,并使用它从 ModbusTCP 设备中读取数据。 希望这可以帮助您开始在 C++ 中使用 HslCommunication 库。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值