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

相关推荐
学逆向的10 分钟前
汇编——数据存储模式
开发语言·汇编·网络安全
geovindu20 分钟前
CSharp: Prototype Pattern
开发语言·后端·设计模式·.net·原型模式·创建型模式
天天进步20151 小时前
Python全栈项目--校园食堂点餐与推荐系统
开发语言·python
aaPIXa6221 小时前
C++模板元编程:编译期计算Fibonacci数列
java·开发语言·c++
eybk1 小时前
写一个可以编制pdf文件的python程序
开发语言·python·pdf
Augustzero2 小时前
`co_await` 按下暂停键之后:从零看懂 C++20 协程
c++·后端
cui_ruicheng2 小时前
Python从入门到实战(八):封装、多态与抽象类
开发语言·python
apihz2 小时前
台风实时与历史详情查询免费 API 接口完整教程
android·开发语言·tcp/ip·dubbo·台风·天气预报
知无不研2 小时前
c语言和c++中的静态关键字
开发语言·c++·静态关键字
bksczm2 小时前
Linux之信号量(POSIX标准)
java·开发语言