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

相关推荐
程序猿小蒜1 小时前
基于springboot的共享汽车管理系统开发与设计
java·开发语言·spring boot·后端·spring·汽车
听风吟丶2 小时前
Java 8 Stream API 高级实战:从数据处理到性能优化的深度解析
开发语言·python
hygge9992 小时前
Spring Boot + MyBatis 整合与 MyBatis 原理全解析
java·开发语言·经验分享·spring boot·后端·mybatis
AA陈超3 小时前
ASC学习笔记0014:手动添加一个新的属性集
c++·笔记·学习·ue5
Run_Teenage3 小时前
C++:智能指针的使用及其原理
开发语言·c++·算法
码界奇点5 小时前
Java设计模式精讲从基础到实战的常见模式解析
java·开发语言·设计模式·java-ee·软件工程
四维碎片5 小时前
【Qt】配置安卓开发环境
android·开发语言·qt
西游音月5 小时前
(7)框架搭建:Qt实战项目之主窗体导航栏、状态栏
开发语言·qt
3***49965 小时前
Swift Experience
开发语言·ios·swift
iFlow_AI5 小时前
iFlow CLI Hooks 「从入门到实战」应用指南
开发语言·前端·javascript·人工智能·ai·iflow·iflow cli