{"id":15866341,"url":"https://github.com/squidmin/cmake-labs","last_synced_at":"2025-04-01T21:19:59.672Z","repository":{"id":149238834,"uuid":"609397972","full_name":"squidmin/cmake-labs","owner":"squidmin","description":"CMake learning labs / reference","archived":false,"fork":false,"pushed_at":"2023-06-11T18:13:11.000Z","size":8040,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-02-07T13:53:59.223Z","etag":null,"topics":["c","cmake","cpp","cxx","learning"],"latest_commit_sha":null,"homepage":"","language":"CMake","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/squidmin.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,"publiccode":null,"codemeta":null}},"created_at":"2023-03-04T03:48:16.000Z","updated_at":"2023-05-14T23:57:29.000Z","dependencies_parsed_at":null,"dependency_job_id":"2a9a6cd2-318a-4c50-b189-db758412b707","html_url":"https://github.com/squidmin/cmake-labs","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/squidmin%2Fcmake-labs","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/squidmin%2Fcmake-labs/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/squidmin%2Fcmake-labs/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/squidmin%2Fcmake-labs/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/squidmin","download_url":"https://codeload.github.com/squidmin/cmake-labs/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":246709907,"owners_count":20821298,"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","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":["c","cmake","cpp","cxx","learning"],"created_at":"2024-10-05T23:20:25.503Z","updated_at":"2025-04-01T21:19:59.666Z","avatar_url":"https://github.com/squidmin.png","language":"CMake","funding_links":[],"categories":[],"sub_categories":[],"readme":"# cmake-labs\n\nCMake learning labs.\n\n\n## Software installation\n\n\u003cdetails\u003e\n\u003csummary\u003eCompiler setup\u003c/summary\u003e\n\n- XCode: \u003chttps://www.ics.uci.edu/~pattis/common/handouts/macclion/clang.html\u003e\n- Check versions:\n\n  ```shell\n  lldb --version\n  clang --version\n  clang++ --version\n  ```\n\n\u003c/details\u003e\n\n\n\u003cdetails\u003e\n\u003csummary\u003eBrew\u003c/summary\u003e\n\n```shell\nbrew install git\nbrew install make\nbrew install cmake\nbrew install doxygen\nbrew install lcov\nbrew install gcovr\nbrew install ccache\n```\n\n\u003c/details\u003e\n\n\n\u003cdetails\u003e\n\u003csummary\u003eAdd VSCode extensions\u003c/summary\u003e\n\n- C/C++ Extension Pack (franneck94)\n- C/C++ Config (franneck94)\n\n**If you want to use your own set of VSCode extensions without installing the above extensions**:\n\nCreate a portable version of VSCode: \u003chttps://code.visualstudio.com/docs/editor/portable\u003e\n\nThis will create a separate version of VSCode that is fully contained to one folder. This portable version can run an instance side-by-side with the version that is installed on the user's system with no crossover.\nAll extensions installed on this portable version will be confined to the `/data` folder (created when setting up a portable version) within the portable version.\n\n\u003c/details\u003e\n\n\n\u003cdetails\u003e\n\u003csummary\u003eGenerate C++ config files\u003c/summary\u003e\n\n**View -\u003e Command Palette... -\u003e Generate C++ Config Files**\n\nThis will create a `.vscode` directory in the project containing the C++ config files.\n\n\u003c/details\u003e\n\n\n---\n\n\n## CMake Tutorial\n\n\n### Generating a project\n\n\u003cdetails\u003e\n\u003csummary\u003eGeneral format\u003c/summary\u003e\n\n```bash\ncmake [\u003coptions\u003e] -S \u003cpath-to-source\u003e -B \u003cpath-to-build\u003e\n```\n\nAssuming that a `CMakeLists.txt` is in the root directory, you can generate a project like the following:\n\n```bash\nmkdir build\ncd build\n```\n\n#### Option 1\n\n```bash\ncmake -S .. -B .\n```\n\n#### Option 2\n\n```bash\ncmake ..\n```\n\nAssuming that you have already built the CMake project, you can update the generated project:\n\n```bash\ncd build\ncmake .\n```\n\n\u003c/details\u003e\n\n\n\u003cdetails\u003e\n\u003csummary\u003eGenerator for GCC and Clang\u003c/summary\u003e\n\n```bash\ncd build\n```\n\n#### Option 1\n\n```bash\ncmake -S .. -B . -G \"Unix Makefiles\"\n```\n\n#### Option 2\n\n```bash\ncmake .. -G \"Unix Makefiles\"\n```\n\n\u003c/details\u003e\n\n\n\u003cdetails\u003e\n\u003csummary\u003eGenerator for MSVC\u003c/summary\u003e\n\n```bash\ncd build\n```\n\n#### Option 1\n\n```bash\ncmake -S .. -B . -G \"Visual Studio 16 2019\"\n```\n\n#### Option 2\n\n```bash\ncmake .. -G \"Visual Studio 16 2019\"\n```\n\n\u003c/details\u003e\n\n\n\u003cdetails\u003e\n\u003csummary\u003eSpecify the Build Type\u003c/summary\u003e\n\nPer default, the standard type is in most cases the debug type.\n\nIf you want to generate the project in `Release` mode, set the `CMAKE_BUILD_TYPE`:\n\n```bash\ncd build\ncmake -DCMAKE_BUILD_TYPE=Release ..\n```\n\n\u003c/details\u003e\n\n\n\u003cdetails\u003e\n\u003csummary\u003ePassing options\u003c/summary\u003e\n\nIf you have set some options in the `CMakeLists.txt`, you can pass values in the command line:\n\n```bash\ncd build\ncmake -DMY_OPTION=[ON|OFF] .. \n```\n\n\u003c/details\u003e\n\n\n\u003cdetails\u003e\n\u003csummary\u003eSpecify the Build Target (Option 1)\u003c/summary\u003e\n\nThe standard build command would build all created targets within the `CMakeLists.txt` file.\nIf you want to build a specific target, you can do so:\n\n```bash\ncd build\ncmake --build . --target ExternalLibraries_Executable\n```\n\nThe target `ExternalLibraries_Executable` is just an example of a possible target name.\n\n\u003e **Note**: All dependent targets will be built beforehand.\n\n\u003c/details\u003e\n\n\n\u003cdetails\u003e\n\u003csummary\u003eSpecify the Build Target (Option 2)\u003c/summary\u003e\n\nBesides setting the target within the `cmake --build` command, you could also run the `Makefile` generated from the previous step.\n\nIf you want to build the `ExternalLibraries_Executable`, you could do the following.\n\n```bash\ncd build\nmake ExternalLibraries_Executable\n```\n\n\u003e **Note**: When building a target, all dependencies of the target are also built.\n\n\u003c/details\u003e\n\n\n\u003cdetails\u003e\n\u003csummary\u003eRun the Executable\u003c/summary\u003e\n\nAfter generating the project and building a specific target, you might want to run the executable.\n\nBy default, the executable is stored in `build/5_ExternalLibraries/app/ExternalLibraries_Executable`, assuming that\n- you are building the project `5_ExternalLibraries`;\n- the main file of the executable is in the `/app` directory.\n\n```bash\ncd build\n./bin/ExternalLibraries_Executable\n```\n\n\u003c/details\u003e\n\n\n---\n\n\n## Different Linking Types\n\n\u003cdetails\u003e\n\u003csummary\u003eSyntax\u003c/summary\u003e\n\n```cmake\nadd_library(A ...)\nadd_library(B ...)\nadd_library(C ...)\n```\n\n\u003c/details\u003e\n\n\n### `PUBLIC`\n\n\u003cdetails\u003e\n\u003csummary\u003eExpand\u003c/summary\u003e\n\n```cmake\ntarget_link_libraries(A PUBLIC B)\ntarget_link_libraries(C PUBLIC A)\n```\n\n\u003e When `A` links in `B` as `PUBLIC`, it says that `A` uses `B` in its implementation, and `B` is also used in `A`'s public API. Hence, `C` can use `B`, since `B` is part of the public API of `A`.\n\n\u003c/details\u003e\n\n\n### `PRIVATE`\n\n\u003cdetails\u003e\n\u003csummary\u003eExpand\u003c/summary\u003e\n\n```cmake\ntarget_link_libraries(A PRIVATE B)\ntarget_link_libraries(C PRIVATE A)\n```\n\nWhen `A` links in `B` as `PRIVATE`, it is saying that `A` uses `B` in its\nimplementation, but `B` is not used in any part of `A`'s public API. Any code\nthat makes calls into `A` would not need to refer directly to anything from\n`B`.\n\n\u003c/details\u003e\n\n\n### `INTERFACE`\n\n\u003cdetails\u003e\n\u003csummary\u003eExpand\u003c/summary\u003e\n\n```cmake\nadd_library(D INTERFACE)\ntarget_include_directories(D INTERFACE {CMAKE_CURRENT_SOURCE_DIR}/include)\n```\n\nIn general, used for header-only libraries.\n\n\u003c/details\u003e\n\n\n---\n\n\n## Different Library Types\n\n\u003cdetails\u003e\n\u003csummary\u003eLibrary\u003c/summary\u003e\n\nA binary file that contains information about code.\nA library cannot be executed on its own. An application utilizes a library.\n\n\u003c/details\u003e\n\n\n\u003cdetails\u003e\n\u003csummary\u003eShared\u003c/summary\u003e\n\n- Linux: *.so\n- MacOS: *.dylib\n- Windows: *.dll\n\nShared libraries reduce the amount of code that is duplicated in each program that makes use of the library, keeping the binaries small.\nShared libraries will however have a small additional cost for the execution.\nIn general, the shared library is in the same directory as the executable.\n\n\u003c/details\u003e\n\n\n\u003cdetails\u003e\n\u003csummary\u003eStatic\u003c/summary\u003e\n\n- Linux/MacOS: *.a\n- Windows: *.lib\n\nStatic libraries increase the overall size of the binary, but it means that you don't need to carry along a copy of the library that is being used.\nAs the code is connected at compile time there are not any additional run-time loading costs.\n\n\u003c/details\u003e\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsquidmin%2Fcmake-labs","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsquidmin%2Fcmake-labs","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsquidmin%2Fcmake-labs/lists"}