{"id":22370043,"url":"https://github.com/andreicherniaev/cmake_variable_to_code","last_synced_at":"2025-03-26T16:24:45.837Z","repository":{"id":234560288,"uuid":"789146049","full_name":"AndreiCherniaev/CMake_variable_to_code","owner":"AndreiCherniaev","description":"export CMake variable to C++ code example","archived":false,"fork":false,"pushed_at":"2024-07-25T06:23:18.000Z","size":8,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-01-31T19:42:43.688Z","etag":null,"topics":["cmake","example","qt"],"latest_commit_sha":null,"homepage":"https://forum.qt.io/post/797532","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/AndreiCherniaev.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":"2024-04-19T19:44:08.000Z","updated_at":"2024-07-25T06:23:20.000Z","dependencies_parsed_at":null,"dependency_job_id":"a1a5e402-5108-470f-87ab-74e1493164dd","html_url":"https://github.com/AndreiCherniaev/CMake_variable_to_code","commit_stats":null,"previous_names":["andreicherniaev/cmake_variable_to_code"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/AndreiCherniaev%2FCMake_variable_to_code","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/AndreiCherniaev%2FCMake_variable_to_code/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/AndreiCherniaev%2FCMake_variable_to_code/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/AndreiCherniaev%2FCMake_variable_to_code/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/AndreiCherniaev","download_url":"https://codeload.github.com/AndreiCherniaev/CMake_variable_to_code/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245690527,"owners_count":20656601,"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","example","qt"],"created_at":"2024-12-04T19:31:59.201Z","updated_at":"2025-03-26T16:24:45.816Z","avatar_url":"https://github.com/AndreiCherniaev.png","language":"C++","readme":"This is export CMake variable to C++ code example. I also use Qt but it is not a main point. There are two ways: use [add_compile_definitions](https://cmake.org/cmake/help/latest/command/add_compile_definitions.html) or use [configure_file](https://cmake.org/cmake/help/latest/guide/tutorial/A%20Basic%20Starting%20Point.html#exercise-3-adding-a-version-number-and-configured-header-file) (better).\n# add_compile_definitions way\n1. Inside CMakeLists.txt any project has something like\n```\nproject(CMake_variable_to_code LANGUAGES CXX C DESCRIPTION \"export CMake variable to C++ code example\" VERSION 1.0.0.0)\n```\n2. Somewhere inside CMakeLists.txt you should add\n```\nadd_compile_definitions(MYPROJECT_NAME=${PROJECT_NAME})\nadd_compile_definitions(MYPROJECT_DESCRIPTION=${PROJECT_DESCRIPTION})\nadd_compile_definitions(CMAKE_PROJECT_VERSION=${CMAKE_PROJECT_VERSION})\n```\n\n3. Then in C++ code you should add\n```\n#define Q(x) #x\n#define QUOTE(x) Q(x)\n```\n\n4. Now you can use CMake's variables inside C++ code. For example\n```\nQCoreApplication::setApplicationName(QUOTE(MYPROJECT_NAME));\nQCoreApplication::setApplicationVersion(QUOTE(CMAKE_PROJECT_VERSION));\n```\n\nSee also my feature [request](https://bugreports.qt.io/browse/QTBUG-123649) for Qt project.\n\n## Compiling\n```\ngit clone https://github.com/AndreiCherniaev/CMake_variable_to_code.git \u0026\u0026 cd \"CMake_variable_to_code/add_compile_definitions_way\"\nrm -rf build-host \u0026\u0026 mkdir build-host/\ncmake -S src/ -B build-host/\ncmake --build build-host/ --parallel\n```\n\n## Test\n```\nbuild-host/CMake_variable_to_code --version\nCMake_variable_to_code 1.0.0.0\n```\n```\nbuild-host/CMake_variable_to_code --help\nUsage: build-host/CMake_variable_to_code [options]\nexport CMake variable to C++ code example\n\nOptions:\n  -h, --help     Displays help on commandline options.\n  --help-all     Displays help, including generic Qt options.\n  -v, --version  Displays version information.\n```\n\n# configure_file way\n## Compiling\n```\ncd \"CMake_variable_to_code/configure_file_way\"\nrm -rf build-host \u0026\u0026 mkdir build-host/\ncmake -S src/ -B build-host/\ncmake --build build-host/ --parallel\n```\n\n## Test\n```\nbuild-host/CMake_variable_to_code --version\nCMake_variable_to_code 1.0.0.0\n```\n```\nbuild-host/CMake_variable_to_code --help\nUsage: build-host/CMake_variable_to_code [options]\nexport CMake variable to C++ code example\n\nOptions:\n  -h, --help     Displays help on commandline options.\n  --help-all     Displays help, including generic Qt options.\n  -v, --version  Displays version information.\n```","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fandreicherniaev%2Fcmake_variable_to_code","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fandreicherniaev%2Fcmake_variable_to_code","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fandreicherniaev%2Fcmake_variable_to_code/lists"}