Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. export CMake variable to C++
Forum Updated to NodeBB v4.3 + New Features

export CMake variable to C++

Scheduled Pinned Locked Moved Solved General and Desktop
4 Posts 2 Posters 1.3k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    DungeonLords
    wrote on 19 Apr 2024, 19:49 last edited by DungeonLords
    #1

    Inside my CMakeLists.txt I wrote

    project(CMake_variable_to_code LANGUAGES CXX C DESCRIPTION "export CMake variable to C++ code example" VERSION 1.0.0.0)
    

    How I can get CMake variables in C++ code? I want to get values: "CMake_variable_to_code", "export CMake variable to C++ code example", "1.0.0.0". And I also make suggestion to Qt to export variables from CMakeLists.txt

    1 Reply Last reply
    0
    • D Offline
      D Offline
      DungeonLords
      wrote on 25 Jul 2024, 12:10 last edited by
      #4

      I make code example for both ways. Here

      1 Reply Last reply
      0
      • D Offline
        D Offline
        DungeonLords
        wrote on 19 Apr 2024, 19:50 last edited by DungeonLords
        #2

        This is export CMake variable to C++ code example. I also use Qt but it is not a main point. In breaf

        1. Inside CMakeLists.txt any project has something like
        project(CMake_variable_to_code LANGUAGES CXX C DESCRIPTION "export CMake variable to C++ code example" VERSION 1.0.0.0)
        
        1. Somewhere inside CMakeLists.txt you should add
        add_compile_definitions(MYPROJECT_NAME=${PROJECT_NAME})
        add_compile_definitions(MYPROJECT_DESCRIPTION=${PROJECT_DESCRIPTION})
        add_compile_definitions(CMAKE_PROJECT_VERSION=${CMAKE_PROJECT_VERSION})
        
        1. Then in C++ code you should add
        #define Q(x) #x
        #define QUOTE(x) Q(x)
        
        1. Now you can use CMake's variables inside C++ code. For example
        QCoreApplication::setApplicationName(QUOTE(MYPROJECT_NAME));
        QCoreApplication::setApplicationVersion(QUOTE(CMAKE_PROJECT_VERSION));
        

        See also my feature request for Qt project.

        Compiling

        git clone https://github.com/AndreiCherniaev/CMake_variable_to_code.git && cd CMake_variable_to_code
        rm -rf build-host && mkdir build-host/
        cmake -S src/ -B build-host/
        cmake --build build-host/ --parallel
        

        Test

        build-host/CMake_variable_to_code --version
        CMake_variable_to_code 1.0.0.0
        
        build-host/CMake_variable_to_code --help
        Usage: build-host/CMake_variable_to_code [options]
        export CMake variable to C++ code example
        
        Options:
          -h, --help     Displays help on commandline options.
          --help-all     Displays help, including generic Qt options.
          -v, --version  Displays version information.
        

        See full-code repo.

        1 Reply Last reply
        0
        • D DungeonLords has marked this topic as solved on 19 Apr 2024, 19:50
        • S Offline
          S Offline
          SimonSchroeder
          wrote on 22 Apr 2024, 06:38 last edited by
          #3

          Here is a different approach from the official CMake tutorial: https://cmake.org/cmake/help/latest/guide/tutorial/A Basic Starting Point.html#exercise-3-adding-a-version-number-and-configured-header-file

          1 Reply Last reply
          0
          • D DungeonLords has marked this topic as solved on 10 May 2024, 07:40
          • D Offline
            D Offline
            DungeonLords
            wrote on 25 Jul 2024, 12:10 last edited by
            #4

            I make code example for both ways. Here

            1 Reply Last reply
            0
            • D DungeonLords has marked this topic as solved on 25 Jul 2024, 12:10

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved