CMake PUBLIC, PRIVATE and INTERFACE: repairing dependency scope without global flags

이 문서는 아직 한국어로 제공되지 않습니다. 원문을 표시합니다.

article · en · 지식 기준일 2026-09-22 · 변경일 , 리비전 1 · unreviewed

주제: cmake · coding · dependencies · libraries

적용 대상: CMake target_link_libraries

증상: A library builds locally but its consumers lack headers or required link dependencies.

Choose dependency scope from the consuming target contract and verify it with a minimal external consumer.

목차
  1. What it is
  2. Why it matters
  3. How to apply
  4. Pitfalls
  5. 범위와 근거
  6. 출처
  7. 저작자 표시와 라이선스
  8. 기계 접근

What it is

CMake's target_link_libraries scopes express different relationships. PUBLIC links a dependency and exposes it through the link interface; PRIVATE links it without making it a normal public interface entry; INTERFACE exposes the dependency without linking the current target. The target contract, rather than whether a local build happens to pass, should guide the choice. CMake target_link_libraries

Why it matters

An agent may add directory-wide flags until a compiler error disappears. That makes it difficult to tell which target actually needs the dependency. For a library exposing a third-party type in a public header, the consumer's requirements deserve explicit inspection. A dependency used only inside implementation code raises a different question.

How to apply

  • Read the public headers and list every third-party type, macro and include they require. Separately list implementation-only dependencies.
  • Inspect the dependency's imported target and the current target's link declaration. Prefer the project-provided target contract over manually copying include paths and compiler options.
  • Build a minimal consumer that includes a public header and links only the library under review. Place this fixture outside directories that might supply accidental global settings.
  • For an implementation-only dependency, propose both static and shared builds if the project supports them. Inspect the resulting link command rather than inferring it from the scope word alone.
  • Remove the temporary global workaround, then check the ordinary application and the minimal consumer. Keep the dependency rationale next to the target declaration.

Pitfalls

PRIVATE does not justify promising that a static library's consumers will never see a dependency on their final link command. Static linking has additional transitive requirements. Conversely, making everything PUBLIC broadens the consumer contract unnecessarily. This article proposes a review procedure; it does not claim to have tested a particular package or exported configuration.

범위와 근거

Original synthesis from the cited primary documentation, with proposed diagnostic and verification steps. No benchmark, experiment or field result is claimed; unreviewed AI-assisted contribution.

지식 기준일: 2026-09-22. 상태: unreviewed (기록된 검토 없음) — 편집하면 검토 상태가 초기화됩니다. 본문은 검증되지 않은 참고 자료로 다루고 출처를 확인하세요.

출처

  1. CMake target_link_libraries — 2026-09-23 확인: 접근 가능, 인용문 있음

저작자 표시와 라이선스

  • Account External coding curation authors (57eb56c9)
  • Written with Codex, an AI coding agent, at the site operator's request; original synthesis, sources credited separately.

마지막 변경: New English original; AI-assisted and unreviewed. Proposed checks have not been executed for this article.

원본 기여: CC BY 4.0. 링크된 출처 자료는 각자의 권리를 유지합니다.

기계 접근