본문 바로가기
C, C++

CMake에서 외부 패키지 가져오기

by slowcloud_ 2026. 4. 5.

https://cmake.org/cmake/help/latest/module/FetchContent.html#id5

 

FetchContent — CMake 4.3.1 Documentation

Contents This module provides commands to populate content at configure time or as part of the calling script. Load this module in CMake with: Note The Using Dependencies Guide provides a high-level introduction to this general topic. It provides a broader

cmake.org

 

FetchContent를 통해, vcpkg나 conan, cpm 같은 별도의 패키지 관리자 없이 외부 패키지를 가져올 수 있다.

 

include(FetchContent)로 FetchContent를 사용할 것임을 알리고,

FetchContent_Declare로 패키지 선언, FetchContent_MakeAvailable로 패키지를 적용할 수 있다.

FetchContent_MakeAvailable은 한 번만 사용하고, 사용할 패키지를 모두 한 번에 써야 한다.

cmake_minimum_required(VERSION 4.0.0)

# clangd 의존성 해결
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)

project(CmakeTest)

# fetchcontent 가져오기
include(FetchContent)

# 정의하기
FetchContent_Declare(
	fmt
	GIT_REPOSITORY https://github.com/fmtlib/fmt.git
	GIT_TAG main
)

FetchContent_MakeAvailable(fmt)

add_executable(main main.cpp)
target_link_libraries(main fmt::fmt)

 

#include <vector>
#include "fmt/base.h"
#include "fmt/core.h"
#include "fmt/ranges.h"

int main() {
	std::vector<int> v{1,2,3};

	fmt::println("hello, world!");
	fmt::println("vector v : {}", v);

	return 0;
}

ninja로 빌드 후 실행한 모습.

외부 라이브러리를 정상적으로 사용하는 모습을 확인할 수 있다.

'C, C++' 카테고리의 다른 글

C/C++] gcc 최적화 옵션  (0) 2026.03.18
C/C++] always_inline  (0) 2026.03.06
C++] 템플릿 메타 프로그래밍  (0) 2026.03.06
C] spinlock  (0) 2026.03.05
C] pthread_mutex_t 활용하기  (0) 2026.03.04