本文へスキップ
製品デスクトップツールTASTECAD図面の確認TASTEFILESファイル管理TASTEFTPファイル転送TASTESSHリモートワークスペースTASTEZIP圧縮と展開開発ツールTASTECODERust 統合開発環境TASTESTUDIO統合開発・テスト環境TASTEETLデータ統合(ETL)サーバーTASTEREMOTEリモートデスクトップサーバーTASTEGEO空間情報サーバーTASTEMAILメールワークスペースはじめるダウンロードドキュメント購入すべての製品
マーケットプレイス
ニュース
技術ニュース今日の開発ニュース開発ドキュメントプログラミング言語RustTypeScriptJavaScriptPythonGoC#JavaCC++KotlinDartWebフロントエンド フレームワークNext.jsReactVue.js · NuxtSvelteKitAngularサーバー フレームワークSpring BootDjangoFastAPIASP.NET CoreNestJS · Expressアプリ・デスクトップ フレームワークTauriFlutterReact NativeElectronアルゴリズムアルゴリズムの基礎主要アルゴリズム発展アルゴリズム実務のアルゴリズム
コミュニティプロジェクト開発ログプロンプトAIツールの問題ショーケースコミュニティガイド
協業技術移転ウェブサイト構築デスクトップアプリ構築リリース・決済パイプライン販売パートナーお問い合わせ
サービスリモートデスクトップインストールパッケージのカスタマイズ
TASTEDEV
製品

デスクトップツール

TASTECAD複雑な図面を、軽やかにひらくTASTEFILES必要なファイルだけを、最速でTASTEFTPファイルが行き交う、いちばん端正な道TASTESSHサーバーとあなたを、より確かにつなぐTASTEZIPより速くまとめ、より安全にひらく

開発ツール

TASTECODERust アプリの編集・ビルド・デバッグ・テストまでひとつのウィンドウでTASTESTUDIO編集からリモートテスト、承認した AI 修正までTASTEETL値を見ながら、データを運ぶ

サーバー

TASTEREMOTE自社サーバーで運用するリモートデスクトップTASTEGEOGeoServer 互換の空間情報サーバーを Rust で作り直しますTASTEMAILメールの主導権を、もう一度自分の手に

はじめる

ダウンロード全製品の最新版ドキュメント各製品の使い方購入サブスクリプション・永久ライセンスすべての製品全製品を一覧で
すべての製品
マーケットプレイスニュース
技術

ニュース

今日の開発ニュース言語・フレームワーク・GitHub・AI の公式発表開発ドキュメント言語・フレームワーク・アルゴリズムのガイド

プログラミング言語

RustTypeScriptJavaScriptPythonGoC#JavaCC++KotlinDart

フレームワーク

Webフロントエンド フレームワークNext.jsReactVue.js · NuxtSvelteKitAngular
サーバー フレームワークSpring BootDjangoFastAPIASP.NET CoreNestJS · Express
アプリ・デスクトップ フレームワークTauriFlutterReact NativeElectron

アルゴリズム

アルゴリズムの基礎計算量解析 · ソート · 探索 · 基本データ構造 · 木構造とヒープ主要アルゴリズムグラフ探索 · 最短経路と最小全域木 · 動的計画法 · 貪欲法 · 分割統治法 · バックトラッキング発展アルゴリズム文字列アルゴリズム · 数学と整数論 · ビット演算 · セグメント木 · Union-Find実務のアルゴリズムコーディング面接対策 · LRUキャッシュ · レート制限 · コンシステントハッシュ法
開発ドキュメントをすべて見る
コミュニティ
プロジェクトビルダーと作品を探す開発ログ決定と試行錯誤プロンプト再利用できる指示AIツールの問題Claude・Codex・Gemini の問題と回避策ショーケース完成した成果コミュニティガイドオープンにつくる方法
協業
技術移転Rust・egui でネイティブアプリを作るウェブサイト構築10言語、アカウント、ドキュメントとダウンロードデスクトップアプリ構築Rust・egui、WebView なしリリース・決済パイプライン署名・MSI・deb・rpm・AppImage・ハッシュ販売パートナー紹介して決済額の30%お問い合わせ構築・提携・投資のご相談
サービス
リモートデスクトップブラウザから自分のパソコンへインストールパッケージのカスタマイズ必要な機能だけ、設定は事前に
購入
JA
한국어EnglishDeutschEspañolFrançaisItalianoPortuguês (Brasil)日本語简体中文繁體中文
ログイン
ログイン
TASTEDEV

小さなツールから、より良い開発へ。

support@tastedev.netTASTEDEVを支援
製品製品ラインナップ購入ドキュメントダウンロードマーケットプレイスサポート
コミュニティプロジェクト開発ログプロンプトAIツールの問題ショーケース
ポリシープライバシーポリシー利用規約サブスクリプション規約返金ポリシーコミュニティガイドライン
アカウントログイン新規登録
訪問者
今日
012345678901234567890123456789012345678901234567890123456789
昨日
012345678901234567890123456789012345678901234567890123456789
累計閲覧
0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789
商号
GXSOFT
代表者
CHOI YOUNG GU
事業者登録番号
853-44-00368
所在地
9F V24, 351 Gyeongin-ro, Michuhol-gu, Incheon, Republic of Korea
電話
+82-10-4404-1523
メール
whitecalvin@tastedev.net
個人情報保護責任者
CHOI YOUNG GU

© 2026 TASTEDEV製品情報は検証済みの開発ソースに基づいています。リリース・改善中

C++ ドキュメント
C++
Getting StartedSyntax BasicsTypes and DataModules and PackagesErrors and TestingKey Concepts
ディスカッション

C++ ガイド · 4/6

Modules and Packages

この章は現在、英語でのみ提供しています。

As a program grows you need to split it across files, keep names from colliding, and pull in third-party libraries. In C++ that job has traditionally been shared by three things: header and source files, namespaces, and the build system. This chapter covers that classic setup, then looks at CMake targets, package managers, and where C++20 modules fit in.

Headers and source files

The usual convention is to put declarations in a header (.h or .hpp) and definitions in a source file (.cpp). Other files #include the header to learn what exists; the linker later connects the calls to the actual code.

// math_utils.hpp
#pragma once

namespace mathx {
int gcd(int a, int b);
double mean(const int* values, int count);
}
// math_utils.cpp
#include "math_utils.hpp"

namespace mathx {
int gcd(int a, int b) {
    while (b != 0) {
        int t = a % b;
        a = b;
        b = t;
    }
    return a;
}

double mean(const int* values, int count) {
    if (count == 0) return 0.0;
    long long sum = 0;
    for (int i = 0; i < count; ++i) sum += values[i];
    return static_cast<double>(sum) / count;
}
}

#pragma once keeps a header from being processed twice in the same translation unit. It isn't part of the standard, but every mainstream compiler supports it; the portable alternative is a classic #ifndef/#define include guard.

Separate compilation

Each .cpp file is compiled on its own into an object file, and the linker stitches the object files together. Change one source file and only that file needs recompiling.

g++ -std=c++20 -c math_utils.cpp -o math_utils.o
g++ -std=c++20 -c main.cpp -o main.o
g++ main.o math_utils.o -o app

Two linker errors come up constantly. "Undefined reference" means something was declared and used but never defined in any object file you linked. "Multiple definition" usually means a function body lives in a header that several source files include. If you really want a function defined in a header, mark it inline, or make it a template or a member function defined inside the class body.

Namespaces

Namespaces group names under a common prefix so that libraries don't trip over each other. Everything in the standard library lives in std.

#include <iostream>
#include "math_utils.hpp"

namespace app::config {          // C++17 nested namespace syntax
constexpr int retries = 3;
}

int main() {
    std::cout << mathx::gcd(12, 18) << '\n';   // 6
    std::cout << app::config::retries << '\n';

    using mathx::gcd;                           // bring in just this name
    std::cout << gcd(8, 12) << '\n';
}

Never write using namespace std; in a header: it leaks every standard name into every file that includes yours. Inside a source file, importing a few specific names with in a narrow scope is the safer habit.

この章について質問がありますか?
前の章Types and Data次の章Errors and Testing
using

CMake targets

Modern CMake is organized around targets. Libraries and executables are targets, and the target_* commands attach include paths, compile options and dependencies to them.

cmake_minimum_required(VERSION 3.20)
project(calc LANGUAGES CXX)
set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

add_library(mathx src/math_utils.cpp)
target_include_directories(mathx PUBLIC include)

add_executable(calc src/main.cpp)
target_link_libraries(calc PRIVATE mathx)

PUBLIC means a setting also propagates to anything that links against the target; PRIVATE keeps it internal. Here calc automatically gets the include directory just by linking to mathx, with no extra configuration.

Package managers

C++ has no official package manager, but a few tools have become standard practice:

ToolNotes
vcpkgMaintained by Microsoft. A vcpkg.json manifest plus a CMake toolchain file
ConanPython-based. Dependencies and generators go in conanfile.txt or conanfile.py
CMake FetchContentNo extra tool: CMake downloads sources (for example from Git) and builds them with your project

In vcpkg's manifest mode you keep a vcpkg.json at the project root and point CMake at the vcpkg toolchain when configuring:

# vcpkg.json, for example: { "name": "calc", "dependencies": [ "fmt" ] }
cmake -S . -B build -DCMAKE_TOOLCHAIN_FILE=<path-to-vcpkg>/scripts/buildsystems/vcpkg.cmake
cmake --build build

Your CMakeLists.txt then uses the library with find_package(fmt CONFIG REQUIRED) and target_link_libraries(calc PRIVATE fmt::fmt). With Conan, you run conan install . --build=missing to fetch or build dependencies, then configure CMake with the toolchain file Conan generates.

C++20 modules

C++20 introduced modules to address the long-standing costs of textual #include: slow builds from reparsing the same headers, and macros leaking between files. A module exposes only what it explicitly exports.

// greet.cppm (module interface unit; some toolchains prefer .ixx)
module;                 // global module fragment: classic #includes go here
#include <string>

export module greet;

export std::string greet(const std::string& name) {
    return "Hello, " + name;
}

A consumer simply writes import greet;. C++23 goes further with import std;, which makes the whole standard library available in one line. That said, compiler and build system support for modules is still uneven, so check the documentation for your exact compiler and CMake version before adopting them in a real project. Most existing libraries are still distributed as headers.

Summary

  • Put declarations in headers and definitions in source files; compile sources separately and link them.
  • Use namespaces to avoid collisions, and keep using namespace out of headers.
  • Describe builds in CMake with add_library, add_executable and target_link_libraries.
  • Bring in third-party code with vcpkg, Conan or FetchContent.
  • C++20 modules are promising, but confirm your toolchain supports them first.

コメント 0件

人気のコメント順新しい順

ログイン · ログインするとコメントできます。