라이브러리

프로그램을 개발하기 위해 사용되는 함수, 데이터, 자료형 등을 하나로 묶어 놓은 것

라이브러리 종류

a : 리눅스용, 정적 라이브러리

.so : 리눅스용, dll 같은 동적 라이브러리

.lib : 윈도용, 정적 라이브러리

.dll : 윈도용, 동적 라이브러리

정적 라이브러리 (Static Link Library)

image2025-2-20_14-44-51.png

프로그램 컴파일 시 링킹 단계에서 라이브러리가 제공하는 코드를 복사하여 실행파일에 넣는 방식의 라이브러리

정적 라이브러리 직접 구현 해보기 (계산기)

├── arith_func.hpp : 함수 선언이 들어 있는 헤더 파일

├── func_1.cpp : 더하기 빼기 소스 파일

├── func_2.cpp : 곱하기 나누기 소스 파일

├── main.cpp

└── Makefile : 컴파일 및 빌드 관리를 위한 파일

// main.cpp
#include <iostream>
#include "arith_func.hpp"
 
int main() {
    int a = 10, b = 5;
 
    std::cout << "Add: " << add(a, b) << std::endl;
    std::cout << "Sub: " << sub(a, b) << std::endl;
    std::cout << "Mul: " << mul(a, b) << std::endl;
    std::cout << "Div: " << div(a, b) << std::endl;
 
    return 0;
}
// func_1.cpp
#include "arith_func.hpp"
 
int add(int a, int b) {
    return a+b;
}
 
int sub(int a, int b) {
    return a-b;
}
 
// func_2.cpp
#include "arith_func.hpp"
 
int div(int a, int b) {
    return a/b;
}
 
int mul(int a, int b) {
    return a*b;
}
 
// arith_func.hpp
#ifndef ARITH_FUNC_HPP
#define ARITH_FUNC_HPP
 
int add(int a, int b);
int sub(int a, int b);
int div(int a, int b);
int mul(int a, int b);
 
#endif