{"id":25019707,"url":"https://github.com/djeada/cmake-examples","last_synced_at":"2025-03-30T10:11:52.456Z","repository":{"id":114371570,"uuid":"343936065","full_name":"djeada/CMake-Examples","owner":"djeada","description":"Examples of projects with CMake.","archived":false,"fork":false,"pushed_at":"2022-11-03T10:33:52.000Z","size":125,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":3,"default_branch":"main","last_synced_at":"2025-02-05T11:52:01.042Z","etag":null,"topics":["cmake","cpp"],"latest_commit_sha":null,"homepage":"","language":"C++","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/djeada.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE","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":"2021-03-02T22:57:31.000Z","updated_at":"2022-08-23T19:28:16.000Z","dependencies_parsed_at":null,"dependency_job_id":"fe09e025-b972-4e84-a64d-88f5c8919277","html_url":"https://github.com/djeada/CMake-Examples","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/djeada%2FCMake-Examples","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/djeada%2FCMake-Examples/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/djeada%2FCMake-Examples/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/djeada%2FCMake-Examples/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/djeada","download_url":"https://codeload.github.com/djeada/CMake-Examples/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":246301997,"owners_count":20755514,"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":["cmake","cpp"],"created_at":"2025-02-05T11:51:27.420Z","updated_at":"2025-03-30T10:11:52.449Z","avatar_url":"https://github.com/djeada.png","language":"C++","funding_links":[],"categories":[],"sub_categories":[],"readme":"# CMake\nExamples of projects with CMake.\n\n## Introduction\n\nCMake is:\n* cross-platform\n* free \n* open-source\n* compiler-independent\n\n### Compilation with gcc\n\nWhen your project contains just three files (main.c code.c code.h), you can easily compile it from the terminal using the following commands:\n\n```bash\ngcc -Wall -Wextra -Werror -std=c99 -pedantic -g -o exe code.c main.c\n```\n\nYou have now an executable that you can run, called \u003ci\u003eexe\u003c/i\u003e.\n\n### Using Makefiles for compilation\n\nWhen dealing with a large number of files, using command line commands for compilation becomes almost impossible.\nInstead, you can use configuration files to define which files to compile and with which flags.\n\nAn example of Makefile for our project with three files (main.c code.c code.h):\n\n```make\nCC=gcc\nCFLAGS=-I.\nDEPS = code.h\n\n%.o: %.c $(DEPS)\n\t$(CC) -c -o $@ $\u003c $(CFLAGS)\n\nproject: main.o code.o \n\t$(CC) -o exe main.o code.o \n```\n\nUse the following command to execute it:\n\n```bash\nmake\n```\n\n*  Make maintains a record of the modifications made to the source files.\nAs a result, everytime you execute it, it will only recompile the files that have changed. \n* Invented at Bell Labs in 1975. It is still one of the most extensively used build managers\n* It has it's own programming language.\n* Many other tools know how to work with it.\n\n### Using CMake for compilation\n\nBuild systems like \u003ci\u003eMake\u003c/i\u003e are usualy platform specific.\nYou can, however, use a platform-independent meta build system such as CMake.\n\nAn example of CMakeList.txt for our project with three files (main.c code.c code.h):\n\n```CMake\ncmake_minimum_required(VERSION 3.15.0)\n\nproject(ExampleProject VERSION 1.0.0)\n\nadd_library(code\ncode.c)\n\nadd_executable(executable\nmain.c)\n\ntarget_link_libraries(executable code)\n```\n\nYou can use the following commands to run it:\n\n```bash\nmkdir build\ncd build\ncmake..\ncmake --build .\n```\n\n### Make vs CMake\n\nMake is a build system. CMake is a build system generator. CMake can create Makefiles, but it also works for Ninja, KDEvelop, and Xcode projects, as well as Visual Studio solutions.\n\nMake will build your project, i.e. it will produce an executable (assuming no errors occur in the process). CMake, on the other hand, will produce the configuration files for Make or other tool.\n\n### CMake Workflow\n\n1. Edit files in the source tree.\n1. Run cmake to generate or configure native build system files.\n1. Open project files from the build tree and use the native build tools.\n\n## CMake language\n\n* All values are strings.\n* Commands are case insensitive.\n* Arguments passed to the commands are case sensitive and space separated.\n* Quoted argument is treated as one value.\n\n### Variables\n\n* Variable names are case sensitive.\n* You can use alpha-numerics and underscores.\n* You should always initialize a variable with a value.\n\n```CMake\nset(TEXT \"Hello World!\")\nset(NUMBER 10)\n\nmessage(\"Your text: ${TEXT} Your number: ${NUMBER}\")\n```\n\n### Lists\n\nIndexed like in every other language: 0...n-1\n\nNegative indexes are counted from the end.\n\n```CMake\nset(LIST xx yy zz)\nset(LIST2 1 2 3)\n\nmessage(\"First list: ${LIST} Second list: ${LIST2}\")\n\nlist(REMOVE_AT LIST 2)\nlist(REMOVE_ITEM LIST xx)\nlist(INSERT LIST 1 gg)\nlist(APPEND LIST dd)\nlist(REVERSE LIST)\nlist(REMOVE_DUPLICATES LIST)\nlist(SORT LIST)\n\nmessage(\"First list: ${LIST} Second list: ${LIST2}\")\n```\n\n### Strings\n\n```CMake\nset(TEXT \"Python boss.\")\n\nmessage(\"Text: ${TEXT}\")\n\nstring(APPEND TEXT \" and not cpp.\")\nstring(REPLACE \"Python\" \"C\" NEW_TEXT ${TEXT} )\nstring(TOLOWER ${TEXT} NEW_TEXT)\nstring(TOUPPER ${TEXT} NEW_TEXT)\nstring(COMPARE EQUAL ${NEW_TEXT} \"XXX\"  flag)\nstring(COMPARE GREATER ${NEW_TEXT} ${TEXT}  flag)\n\nmessage(\"Text: ${TEXT}\")\n```\n\n### Conditionals\n\n```CMake\nset(FLAG ON)\n\nif(${FLAG})\n\tmessage(\"You can see this message.\")\nelse()\n\tmessage(\"You will never see that message.\")\nendif()\n```\n\n### Loops\n\n```CMake\n foreach(LETTER a b c)\n \tmessage(${LETTER})\n```\n\n### Functions\n\n```CMake\nfunction(display NAME)\n\tmessage(\"Provided parameter: ${${NAME}}\")\n\n\tmessage(\"ARGC=\t${ARGC}\")\n\tmessage(\"ARGV=\t${ARGV}\")\n\tmessage(\"ARGN=\t${ARGN}\")\n\t\n\tif(DEFINED ARGV0)\n\t\tmessage(\"ARGV0:\t${ARGV0}\")\n\tendif()\n\t\n\tif(DEFINED ARGV1)\n\t\tmessage(\"ARGV1:\t${ARGV1}\")\n\tendif()\n\t\n\tif(DEFINED ARGV2)\n\t\tmessage(\"ARGV2:\t${ARGV2}\")\n\tendif()\nendfunction()\n```\n\n\n## Requirements\n \nTo run our examples, you will need the following:\n\n* CMake v3.15+\n* gcc\n* make\n\n## Table of contents\n\n  - [An executable and libraries](https://github.com/djeada/CMake/tree/main/src/ExecutableAndLibraries). A simple example with three .cpp files and two .h files, all in the same directory.\n  - [Subdirectories](https://github.com/djeada/CMake/tree/main/src/Subdirectories). The project is divided into subdirectories, each with its own CMakeList.txt file.\n  - [Exporting and using packages](https://github.com/djeada/CMake/tree/main/src/ExportingAndUsingPackages). Export the library as a package and include it in an entirely unrelated project.\n  - [OpenCV](https://github.com/djeada/CMake/tree/main/src/OpenCV). Create a program that reads an image, writes text to it, and saves it using the OpenCV library.\n  - [MPI](https://github.com/djeada/CMake/tree/main/src/MPI). Hello world with MPI.\n  - [ClangTidy](https://github.com/djeada/CMake/tree/main/src/ClangTidy). Example on how to integrate clang-tidy with CMake.\n  - [GTest](https://github.com/djeada/CMake/tree/main/src/GTest ). Run google unit tests (GTest) with CMake.\n  - Doxygen with CMake.\n\n## Contributing\nPull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.\n\nPlease make sure to update tests as appropriate.\n\n## License\n[MIT](https://choosealicense.com/licenses/mit/)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdjeada%2Fcmake-examples","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdjeada%2Fcmake-examples","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdjeada%2Fcmake-examples/lists"}