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

相关推荐
2601_962885727 小时前
如何用 Python 自动识别股票的支撑位与压力位?
开发语言·python
爱吃奥利奥_wen7 小时前
面向对象三板斧:封装、继承、多态,到底在“装“什么、“承“什么、“变“什么?
开发语言·经验分享·c#
All for pursuit.7 小时前
【位运算-1】338.比特位计数
数据结构·c++·算法·leetcode
我可能是个假开发7 小时前
FTP Unicode 文件名导致 `550` 的问题分析与解决方案
java·开发语言·spring
小师兄吃牛肉8 小时前
C语言中的nullptr有必要吗
c语言·c++·算法
WL学习笔记8 小时前
日期类(C++)
开发语言·c++·日期类实现
谢亮_vipxieliang9 小时前
Go map与结构体的正确使用
开发语言·golang·哈希算法
H.莓飛9 小时前
【C++】命名空间、缺省参数、函数重载与引用
linux·开发语言·c++·visual studio
xxwl5859 小时前
数据结构知识点和代码实现总结(C语言实现)
c语言·开发语言·数据结构
汉克老师10 小时前
GESP2026年9月认证C++八级( 第一部分选择题(1~7题)精讲
c++·gesp·小学生·学c++编程