{"id":31847745,"url":"https://github.com/mizuhoaoki/cmake_tutorial","last_synced_at":"2025-10-12T09:54:29.195Z","repository":{"id":154615981,"uuid":"449420240","full_name":"MizuhoAOKI/cmake_tutorial","owner":"MizuhoAOKI","description":"Easy tutorial to understand how to use GCC, Make, and CMake properly.","archived":false,"fork":false,"pushed_at":"2024-04-16T16:00:00.000Z","size":76,"stargazers_count":4,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2024-04-16T20:41:33.054Z","etag":null,"topics":["build-tool","cmake","cpp","make","makefile","tutorial"],"latest_commit_sha":null,"homepage":"","language":"C++","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/MizuhoAOKI.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null}},"created_at":"2022-01-18T19:31:01.000Z","updated_at":"2024-04-16T20:18:11.000Z","dependencies_parsed_at":"2024-04-16T17:00:51.595Z","dependency_job_id":null,"html_url":"https://github.com/MizuhoAOKI/cmake_tutorial","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/MizuhoAOKI/cmake_tutorial","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/MizuhoAOKI%2Fcmake_tutorial","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/MizuhoAOKI%2Fcmake_tutorial/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/MizuhoAOKI%2Fcmake_tutorial/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/MizuhoAOKI%2Fcmake_tutorial/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/MizuhoAOKI","download_url":"https://codeload.github.com/MizuhoAOKI/cmake_tutorial/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/MizuhoAOKI%2Fcmake_tutorial/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":279010964,"owners_count":26084840,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-10-12T02:00:06.719Z","response_time":53,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["build-tool","cmake","cpp","make","makefile","tutorial"],"created_at":"2025-10-12T09:54:12.989Z","updated_at":"2025-10-12T09:54:29.190Z","avatar_url":"https://github.com/MizuhoAOKI.png","language":"C++","readme":"# Tutorial of GCC , Make and CMake.\nGCC, Make and CMake are famous and useful tools to build C/C++ programs.\n\nThis note helps you to understand functions and how to use them properly.\n\n## Sample program\n#### Task : Calculate PI in 3 different methods.\n1. Viete's method\n2. Ramanujan's method\n3. Gregory's method\n\n#### Output\n```\n\u003ecmake_tutorial/build$./calcpi \n[main.cpp] This program calculates PI in three different methods.\n[main.cpp] Number of iterations = 8\n\n[viete.cpp] Calculating PI with Viete's formula.\n[ramanujan.cpp] Calculating PI with Ramanujan's formula.\n[gregory.cpp] Calculating PI with Gregory's formula.\n\n[Results]\n  Viete's method     : PI = 3.141588 \n  Ramanujan's method : PI = 3.141593 \n  Gregory's method   : PI = 3.252366 \n```\n\n\n#### Directory tree\n- cmake_tutorial\n    - include \n        - factorial.hpp\n        - gregory.hpp\n        - ramanujan.hpp\n        - viete.hpp\n    - src\n        - main.cpp\n        - factorial.cpp\n        - gregory.cpp\n        - ramanujan.cpp\n        - viete.cpp\n                                      \nNow, we build the project in three different ways.\n1. gcc\n2. gcc + make\n3. gcc + make + cmake\n\n## Way 1. Build with gcc\n### What is [GCC](https://gcc.gnu.org/)?\nIt is a famous compiler of C, C++ and some other programming languages.\n\n### Environment setup\nInstall gcc.\n- Windows : \n    - `scoop install gcc`\n- Mac OS : \n    - `brew install gcc`\n- Ubuntu : \n    - `sudo apt install build-essential`\n\n### Procedures\n\n1. Move to the directory to output intermediate files and executables.\n    - `cd build`\n\n1. Build shared libraries\n    - `g++ -shared -o libV.so  -I../include -c ../src/viete.cpp`\n    - `g++ -shared -o libR.so  -I../include -c ../src/ramanujan.cpp`\n    - `g++ -shared -o libG.so  -I../include -c ../src/gregory.cpp`\n    - Use \".dll\" extention instead when you use windows.\n\n1. Build a static library.\n    - `g++ -static -o factorial.o -c -I../include ../src/factorial.cpp`\n    - `ar rcs libfactorial.a factorial.o`\n\n1. Generate executable.\n    - `g++ -o calcpi ../src/main.cpp -I../include -L. -lV -lR -lG -lfactorial`\n    - Note : -I option indicates the location of header files.\n    - Note : -L option indicates the location of shared libraries.\n\n1. Run `./calcpi`\n\n## Way 2. Build with gcc + make\n### What is [Make](https://www.gnu.org/software/make/)?\nIt is an useful tool to help compiling C/C+ programs.\n\n### Advantages of make\n- Fewer build commands. Just type \"make\".\n- Avoid unnecessary build processes =\u003e Faster builds!\n\n### Environment setup\nInstall gcc and make.\n- Windows : \n    - `scoop install gcc make`\n- Mac OS : \n    - `brew install gcc make`\n- Ubuntu : \n    - `sudo apt install build-essential`\n\n### Procedures\n1. `cd build` and locate Makefile\n    ```\n    # compiler\n    CC  = g++\n    # compile options\n    CFLAGS    = -Wall\n    # name of executable\n    TARGET  = calcpi\n    # target src code\n    SRCS    = ../src/main.cpp\n    # src directory\n    SRCDIR  = ../src\n    # include directory\n    INCDIR  = -I../include\n    # directory including libraries\n    LIBDIR  = -L.\n    # library files to link\n    LIBS    = -lV -lR -lG -lfactorial\n\n    # Generate an executable.\n    $(TARGET): $(SRCS) libV.so libR.so libG.so libfactorial.a\n        $(CC) $(CFLAGS) -o $@ $(SRCS) $(INCDIR) $(LIBDIR) $(LIBS)\n\n    # Build libraries\n    libV.so : \n        $(CC) -shared -o $@ $(INCDIR) -c $(SRCDIR)/viete.cpp\n\n    libR.so :\n        $(CC) -shared -o $@ $(INCDIR) -c $(SRCDIR)/ramanujan.cpp\n\n    libG.so :\n        $(CC) -shared -o $@ $(INCDIR) -c $(SRCDIR)/gregory.cpp\n\n    libfactorial.a :\n        $(CC) -static -o factorial.o $(INCDIR) -c $(SRCDIR)/factorial.cpp\n        ar rcs $@ factorial.o\n\n    # make all\n    all: clean $(TARGET)\n\n    # make clean\n    clean:\n        -rm -f $(TARGET) *.dll *.so *.o\n    ```\n1. `make`\n1. Run `./calcpi`\n\n## Way 3. Build with gcc + make + cmake\n### What is [CMake](https://cmake.org/)?\nIt is an excellent tool extending make to help building C/C++ projects.\n\n### Advantages of CMake\n- Easier to set up build environment for complexed projects of C/C++.\n    - CMake generates Makefile automatically.\n\nFor example, \n- Better support for complex directory structures.\n- Easy to specify dependent libraries.\n- Multiple executables can be generated at once.\n\n### Environment setup\nInstall gcc, make, and cmake.\n- Windows : \n    - `scoop install gcc make cmake`\n- Mac OS : \n    - `brew install gcc make cmake`\n- Ubuntu : \n    - `sudo apt install build-essential cmake`\n\n### Procedures\n1. `cd cmake_tutorial` and locate CMakeLists.txt\n    ```\n    # set required version\n    cmake_minimum_required(VERSION 3.1)\n    # set compiler\n    set(CMAKE_C_COMPILER gcc)\n    set(CMAKE_CXX_COMPILER g++)\n    # set project name\n    project( CALCPI CXX)\n\n    # set build options\n    # set(CMAKE_CXX_FLAGS \"-g\")# debug mode\n    set(CMAKE_CXX_FLAGS \"-O2 -march=native -std=c++11 -Wall\")# release mode\n\n    # set include directories\n    include_directories(${CMAKE_CURRENT_SOURCE_DIR}/include)\n    include_directories(${CMAKE_CURRENT_SOURCE_DIR}/src)\n\n    # generate shared libraries\n    add_library(viete     SHARED ${CMAKE_CURRENT_SOURCE_DIR}/src/viete.cpp    ) \n    add_library(ramanujan SHARED ${CMAKE_CURRENT_SOURCE_DIR}/src/ramanujan.cpp) \n    add_library(gregory   SHARED ${CMAKE_CURRENT_SOURCE_DIR}/src/gregory.cpp  )\n\n    # generate a static library\n    add_library(factorial STATIC ${CMAKE_CURRENT_SOURCE_DIR}/src/factorial.cpp)\n\n    # generate executables\n    add_executable(calcpi ${CMAKE_CURRENT_SOURCE_DIR}/src/main.cpp)\n\n    # link libraries\n    target_link_libraries(calcpi viete ramanujan gregory factorial)\n    ```\n1. `cd build`\n1. `cmake ..`\n1. `make`\n1. Run `./calcpi`\n\n\n\n\n\n\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmizuhoaoki%2Fcmake_tutorial","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmizuhoaoki%2Fcmake_tutorial","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmizuhoaoki%2Fcmake_tutorial/lists"}