Visual Studio 编译静态库过程
一、问题引入
在写程序过程中,一般都是直接写 xx.h 和 xx.c 文件。基本上依靠这两个文件就可以完成一个简单的小项目,但是一旦构建一个大项目就会产生很多文件。这时一部分基础文件是不需要修改的,应该将其编译为库文件(windows系统中静态库文件 xx.lib、linux系统中静态库文件 xx.o)
编译得到的静态库文件不像 xx.c 源文件可以进行修改,若想修改静态库文件必须重新修改其源文件并重新编译。
二、解决过程
💡 注意:编译静态库时,源文件是没有 main()函数的
file.h
//==============================================================================
//
// Title: file.h
// Purpose: A short description of the interface.
//
// Created on: 2023-03-08 at 10:17:29 by .
// Copyright: . All Rights Reserved.
//
//==============================================================================
#ifndef __file_H__
#define __file_H__
#ifdef __cplusplus
extern "C" {
#endif
//==============================================================================
// Include files
//==============================================================================
// Constants
//==============================================================================
// Types
//==============================================================================
// External variables
//==============================================================================
// Global functions
//int Declare_Your_Functions_Here (int x);
void SplitPath(const char* pathName, char *driveName, char *dirName, char *fileName);
int GetCurrentAllFile(const char *searchDir, char fileList[][300], int *numOfFile);
void GetCurrentAllDir(const char *searchDir, char dirList[][300], int *numOfDir);
#ifdef __cplusplus
}
#endif
#endif /* ndef __file_H__ */
file.c
//==============================================================================
//
// Title: file.c
// Purpose: A short description of the implementation.
//
// Created on: 2023-03-08 at 10:17:29 by .
// Copyright: . All Rights Reserved.
//
//==============================================================================
//==============================================================================
// Include files
//#include "file.h"
#include <stdio.h>
#include <stdlib.h>
#include <dirent.h>
//==============================================================================
// Constants
//==============================================================================
// Types
//==============================================================================
// Static global variables
//==============================================================================
// Static functions
//==============================================================================
// Global variables
//==============================================================================
// Global functions
/// HIFN What does your function do?
/// HIPAR x/What inputs does your function expect?
/// HIRET What does your function return?
//int Define_Your_Functions_Here (int x)
//{
// return x;
//}
void SplitPath(const char* pathName, char *driveName, char *dirName, char *fileName)
{
char c[4][300] = { 0 };
_splitpath(pathName, c[0], c[1], c[2], c[3]);
if (NULL != driveName)
{
sprintf(driveName, "%s", c[0]);
}
if (NULL != dirName)
{
sprintf(dirName, "%s", c[1]);
}
if (NULL != fileName)
{
sprintf(fileName, "%s%s", c[2], c[3]);
}
//printf("%s\n%s\n%s\n%s\n", c[0], c[1], c[2], c[3]);
}
void GetCurrentAllFile(const char *searchDir, char fileList[][300], int *numOfFile)
{
int i = 0;
DIR *d;
struct dirent *dir;
d = opendir(searchDir);
if (d)
{
while ((dir = readdir(d)) != NULL)
{
if (DT_REG == dir->d_type)
{
sprintf(fileList[i], "%s\\%s", searchDir, dir->d_name);
i++;
}
}
closedir(d);
}
*numOfFile = i;
}
void GetCurrentAllDir(const char *searchDir, char dirList[][300], int *numOfDir)
{
int i = 0;
DIR *d;
struct dirent *dir;
d = opendir(searchDir);
if (d)
{
while ((dir = readdir(d)) != NULL)
{
if (DT_DIR == dir->d_type)
{
//printf("%s ", dir->d_name);
sprintf(dirList[i], "%s\\%s", searchDir, dir->d_name);
i++;
}
}
closedir(d);
}
*numOfDir = i;
}
2-1 编译静态库文件
-
Visual Studio 2017 新建静态库项目


-
删除项目默认创建的
.c和.h文件,创建自己的file.c和file.h文件 -
取消项目的预编译头

若项目中文件没有问题,那么可以进行编译,项目路径下生成 Debug 文件夹

2-2 使用静态库文件
-
在其他项目移除
file.c文件
-
在其他项目中添加
FileStaticLib_Test.lib文件以及该文件所在目录

-
重新编译、运行项目。 运行结果和之前附加的
file.c文件一样的结果
三、反思总结
若参与大型项目,其中基础组件经过长时间的测试无bug 且 后续不在基础组件新增功能,这时可以考虑将基础组件的源文件编译成库文件。
一方面可以避免基础组件的源文件的误修改,另一方面可以屏蔽基础组件的实现细节,保证安全性。
四、参考引用
无

浙公网安备 33010602011771号