zoukankan      html  css  js  c++  java
  • VS2019静态/动态库制作 20199321

    动态库

    • 新建动态库项目
    //pch.h
    #ifndef PCH_H
    #define PCH_H 
    #include "framework.h" 
    #endif //PCH_H
    
    #ifdef IMPORT_DLL
    #else
    #define IMPORT_DLL extern "C" _declspec(dllimport)
    #endif 
    
    IMPORT_DLL int add(int a, int b);
    IMPORT_DLL int minus(int a, int b);
    IMPORT_DLL int multiply(int a, int b);
    IMPORT_DLL double divide(int a, int b);
    
    
    // dllmain.cpp : 定义 DLL 应用程序的入口点。
    #include "pch.h"
    int add(int a, int b) 
    { return a + b; } 
    
    int minus(int a, int b) 
    { return a - b; } 
    
    int multiply(int a, int b)
    { return a * b; } 
    
    double divide(int a, int b) 
    { double m = (double)a / b; return m; }
    
    • 另新建win32控制台项目
    // test20199321.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
    
    #include <iostream>
    #include<windows.h>
    
    int main()
    {
     HINSTANCE hDllInst; 
     hDllInst = LoadLibrary(L"20199321.dll"); //调用DLL 
     typedef int(*PLUSFUNC)(int a, int b); //后边为参数,前面为返回值 
     PLUSFUNC plus_str = (PLUSFUNC)GetProcAddress(hDllInst, "add"); //GetProcAddress为获取该函数的地址 
     std::cout << plus_str(1,2);
    
    }
    

    静态库

    • 新建静态库项目
    //pch.h
    #ifndef __PCH__
    
    #define __PCH__
    
    extern int add(int a, int b);//extern关键字说明这是一个外部函数,这个函数不由自己实现,而是外部的库实现的,以便链接器进行链接
    extern int minus(int a, int b);
    extern int multiply(int a, int b);
    extern double divide (int a, int b);
    #endif
    
    // 20199321lib.cpp : 定义静态库的函数。
    //
    
    #include "pch.h"
    #include "framework.h"
    
    int add(int a, int b) 
    { return a + b; } 
    
    int minus(int a, int b) 
    { return a - b; } 
    
    int multiply(int a, int b) 
    { return a * b; } 
    
    double divide(int a, int b) 
    { double m = (double)a / b; return m; }
    
    • 另新建win32控制台项目
    #include<iostream>
    #include"pch.h"
    #pragma comment (lib,"20199321lib.lib")
    using namespace std;int main()
    { int  a=3,b=18; int c; 
    c=add(a,b); cout << c << endl;  return 0;}
    

  • 相关阅读:
    OK335xS-Android mkmmc-android-ubifs.sh hacking
    OK335xS-Android pack-ubi-256M.sh hacking
    OK335xS Ubuntu 12.04.1 版本 Android 开发环境搭建
    Qt Quick Hello World hacking
    Qt QML referenceexamples attached Demo hacking
    QT 5.4.1 for Android Ubuntu QtWebView Demo
    I.MX6 working note for high efficiency
    QT 5.4.1 for Android Windows环境搭建
    mkbootimg hacking
    Generate And Play A Tone In Android hacking
  • 原文地址:https://www.cnblogs.com/20199321zjy/p/11875701.html
Copyright © 2011-2022 走看看