zoukankan      html  css  js  c++  java
  • cmake 静态调用 c++ dll 的类的一个例子(Clion IDE)[更新1:增加1.模版的应用,2.ma 的算法]

    CMakeLists.txt

    project(aaa)
    add_library(aaa SHARED aaa.cpp)
    add_executable(bbb bbb.cpp)
    target_link_libraries(bbb aaa)

    aaa.h

    #pragma once
    
    #ifndef AAA_AAA_H
    #define AAA_AAA_H
    #endif
    
    #ifdef BUILD_AAA_DLL
    #define IO_AAA_DLL __declspec(export)
    #else
    #define IO_AAA_DLL __declspec(import)
    #endif
    
    IO_AAA_DLL class father
    {
    private:
        const double PI = 3.14;
    public:
        void hello(void);
        /* 该函数用于介绍 dll 的接口
         *
         */
        double * ma(double *array, int arrayLen, int maLen);
        /* 该函数用于计算 ma 值
         * array    传入数组
         * arrayLen 数组长度
         * maLen    计算天数
         */
    };

    aaa.cpp

    #define BUILD_AAA_DLL
    
    #include "aaa.h"
    #include <iostream>
    
    using namespace std;
    
    IO_AAA_DLL void father::hello(void)
    {
        cout << "+----------------------------------+" << endl;
        cout << "|Hello from class.father.hello()   |" << endl;
        cout << "|             --Made by DengChaohai|" << endl;
        cout << "+----------------------------------+" << endl;
    
    }
    
    double * father::ma(double *array, int arrayLen, int maLen)
    {
        int n = maLen;
        // 保存计算天数
        double ma[arrayLen];
        // 用于保存 ma 值
        while(arrayLen >= maLen && maLen >0)
        // 传入数组长度要大于计算天数
        {
            double sum = 0;
            for(int i = maLen - n; i < maLen; i++)
            // 计算长度不变,但 ma 值计算要一步步移动,
            {
                sum = sum + array[i];
            }
            ma[maLen - 1] = sum / n;
            // 简单的平均值算法
            cout << "wma[" << maLen - 1 << "] = " << ma[maLen - 1] << endl;
            maLen++;
        }
        return ma;
        // 返回数组指针,是否调用再说
    }

    bbb.cpp

    #include "aaa.h"
    #pragma comment(a, "C:UsersPerelman.CLion2016.1systemcmakegeneratedaaa-4d5bae384d5bae38Debuglibaaa.a")
    
    #include <iostream>
    using namespace std;
    
    template <typename t> int getArrayLen(t &array)
    // 应用模版,动态定义数据类型
    {
        return sizeof(array) / sizeof(array[0]);
    }
    
    int main()
    {
        father child;
        child.hello();
        double open[] = {0.11, 0.12, 0.13, 0.14, 0.15, 0.16, 0.17, 0.18, 0.19, 0.20, 0.21, 0.22, 0.23};
        double *p = child.ma(open, getArrayLen(open), 1);
        cout << *(p + 4);
        return 0;
    }
    360截图20160613183325803

    360截图20160613184244117 

    bbb.py

    from ctypes import *
    h = windll.LoadLibrary('C:\Users\Perelman\.CLion2016.1\system\cmake\generated\aaa-4d5bae38\4d5bae38\Debug\libaaa.dll')
    h._ZN6father5helloEv()
    '''调用函数 hello,此函数名由 depends 工具获得'''
    PyList = [0.11, 0.12, 0.13, 0.14, 0.15, 0.16, 0.17, 0.18, 0.19, 0.20, 0.21, 0.22, 0.23]
    '''python 的 list 数据'''
    CArray = (c_double*len(PyList))(*PyList)
    '''转成 c 的 数组格式'''
    h._ZN6father2maEPdii(byref(CArray), 13, 3)
    '''调用函数 ma,参数 1 的指针用 byref 取'''

    1 

    2

  • 相关阅读:
    SQL练习题32:请你创建一个actor_name表,并且将actor表中的所有first_name以及last_name导入该表.
    SQL练习题31:对于表actor批量插入如下数据,如果数据已经存在,请忽略(不支持使用replace操作)
    SQL练习题30:对于表actor批量插入如下数据(不能有2条insert语句哦!)
    npm run dev 报错:missing script:dev
    [转]vue中“:”、“.”、“@”的意义
    Vue踩坑记录
    Vue指令:v-clock解决页面闪烁问题
    npm-安装模块时出现rollbackFailedOptional
    js中[]、{}、()的区别
    IDEA离线安装插件
  • 原文地址:https://www.cnblogs.com/blog-3123958139/p/5581617.html
Copyright © 2011-2022 走看看