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

相关推荐
伞伞悦读3 小时前
【第38期】Python 模块与包详解:import、from、模块搜索路径、包结构和 __init__
开发语言·python
Smileyqp沛沛4 小时前
前端?C++ ?较大差异基础罗列
c++·基础·前端转c++
C语言小火车4 小时前
C/C++ 为什么需要编译器?
开发语言·c++
旖旎夜光4 小时前
力控面试题 01.01: 判定字符是否唯一(位运算) —— 题解
c++·学习·算法·leetcode·力控
吞下星星的少年·-·5 小时前
C++ 萌新语法入门篇
c++·算法比赛
霍霍的袁5 小时前
【C++】map 和 set 的使用 | 从用法到底层
开发语言·c++·学习·visual studio
another heaven5 小时前
【算法/C++ MD5算法能否逆解码?原理、C++实现与同类哈希算法对比】
c++·算法·哈希算法
程序猿编码5 小时前
告别改源码适配模型:纯 C++ 可配置 LLM 推理引擎,全格式全结构兼容
c++·大模型·llm·推理引擎
孙启超5 小时前
【AI开发之Rust】第 11 课:智能指针与内部可变性
开发语言·后端·rust
此生决int5 小时前
深入理解C++系列(20)——C++11(下)
开发语言·c++