C++编写静态库

1、新建项目创建静态库staticLib1.

demoStaticLib.h

复制代码
#pragma once
class ArrayTool
{
public:
	int Max(const int* lpHead, const int nLength);
	int Sum(const int* lpHead, const int nLength);
};

demoStaticLib.cpp

复制代码
#include "pch.h"
#include "demoStaticLib.h"

int ArrayTool::Max(const int* lpHead, const int nLength)
{
    int nMaxVal = lpHead[0];
    for (int i = 0; i < nLength; i++) {
        if (nMaxVal < lpHead[i])
            nMaxVal = lpHead[i];
    }

    return nMaxVal;
}

int ArrayTool::Sum(const int* lpHead, const int nLength)
{
    int nTotal = 0;
    for (int i = 0; i < nLength; i++ ) {
        nTotal += lpHead[i];
    }
    return nTotal;
}

右键点击 、生成

2、创建引用其的项目useStaticApp

2、1添加引用 。将生成lib文件应用

2、2 项目属性->配置属性->C++->附件包含目录 引入上一个项目的目录

修改文件 useStaticApp.cpp

复制代码
#include <iostream>
#include "demoStaticLib.h"
int main()
{
    int nArr[] = { 1,34,6,7,8,35,67 };
    ArrayTool at;
    int nLen = sizeof(nArr) / sizeof(int);
    std::cout << "数组最大值 " << at.Max(nArr, nLen) << std::endl;
    std::cout << "数组元素之和 " << at.Sum(nArr, nLen) << std::endl;
    return 0;
}

2、邮件 点击 、生成

useStaticApp设为项目,然后运行调试

可参考 【C++】04 静态库_多文件静态库demo-CSDN博客

Visual Studio 2019-编写C++动态链接库_哔哩哔哩_bilibili

相关推荐
HaiXCoder2 分钟前
C++ 表达式怎么读:从一行代码到类型、值类别和生命周期
c++
七夜zippoe15 分钟前
Nexent 知识库:调优靠 Chunk 与重排
开发语言·python·ai智能体·nexent·知识库与rag
kyle~1 小时前
C# --- 反射
开发语言·c#
soso19682 小时前
低功耗设计与BMS开发指南
c语言·开发语言·汇编
Risehuxyc3 小时前
来自PHP项目devnexen的回信
开发语言·php
FRing033 小时前
vue2干货总结10(json-server、vant-ui、postcss插件、axios二次封装、图形验证码、toast轻提示)
开发语言·前端·vue.js·ui·json·vue2·postcss
kyle~3 小时前
因子图优化(Factor Graph Optimization)
c++·算法·机器人·传感器
我是大猴子4 小时前
分析sss
开发语言
geats人山人海4 小时前
现代c++4.1
java·开发语言·c++
布莱克6054 小时前
Swift 字符串详解:从基础用法到性能与最佳实践
服务器·开发语言·ssh·字符串·swift