# CMake, setting a macro from a console program \[SOLVED\]

**URL:** <https://discourse.cmake.org/t/cmake-setting-a-macro-from-a-console-program-solved/2869>\
**Category:** Code\
**Created:** [March 2, 2021, 2:00pm UTC](https://discourse.cmake.org/t/cmake-setting-a-macro-from-a-console-program-solved/2869 "2021-03-02T14:00:10Z")\
**Posts on this page:** 16\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jos](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/j/2bfe46/32.png) [@Jos](https://discourse.cmake.org/u/Jos)\
**Post date:** [March 2, 2021, 2:00pm UTC](https://discourse.cmake.org/t/cmake-setting-a-macro-from-a-console-program-solved/2869/1 "2021-03-02T14:00:11Z")

</div>

Hi, I’m Making the portablity of a code to CMake. The code needs that CMake defines Macro with last short head from git.

If I define the value maually,

```
target_compile_definitions(LibDEFINES PRIVATE
	GITVER=3686e892
)

```

There is no problem, the code compiles, only with a warning

```
317:13: warning: floating constant exceeds range of ‘double’ [-Woverflow]
             .arg(GITVER)
         ^

```

The problem comes when I try to extract automatic the macro value from the git command

```
execute_process (
    COMMAND bash -c "git rev-parse --short HEAD"
    OUTPUT_VARIABLE GIT_VER
)

message(WARNING "MiMENSAJE\t" ${GIT_VER}) //Prints to console the right value 3686e892

target_compile_definitions(LibDEFINES PRIVATE
	GITVER=${GIT_VER}
)

```

but, I guess, the variable is not well defined, because, the compile process, stops with a strange error and yet compiles fine when is manual defined

```
A_Typedef.h:552:5: error: ‘Sem’ does not name a type
     Sem* semPS;

```

My first assumption is that GIT\_VER is declared like string, but I have no idea, perhaps I don’t use the right procedure to Set the macro.

How should I correctly define the macro from git command?

Thanks in advance.

---

<div class="post-metadata">

**Author:** ![ClausKlein](https://discourse.cmake.org/user_avatar/discourse.cmake.org/clausklein/32/352_2.png) [@ClausKlein](https://discourse.cmake.org/u/ClausKlein)\
**Post date:** [March 2, 2021, 2:09pm UTC](https://discourse.cmake.org/t/cmake-setting-a-macro-from-a-console-program-solved/2869/2 "2021-03-02T14:09:36Z")

</div>

> [@Jos](#):
>
> ```auto
> A_Typedef.h:552:5: error: ‘Sem’ does not name a type
> Sem* semPS;
> 
> ```

can you show your header file or at least the related lines?

---

<div class="post-metadata">

**Author:** ![Jos](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/j/2bfe46/32.png) [@Jos](https://discourse.cmake.org/u/Jos)\
**Post date:** [March 2, 2021, 2:23pm UTC](https://discourse.cmake.org/t/cmake-setting-a-macro-from-a-console-program-solved/2869/3 "2021-03-02T14:23:09Z")

</div>

Hi Claus, thank you

```
if !defined(OTHER_BUILD)
typedef struct CFItem
{
  struct CFItem* nextPS; // This member MUST always be the first one!
  char const* thrdPB;
  union
  {
    TCB* tcbPS;
    Sem* semPS; //<---------------------------- Here!
  } syncU;
  ulong idL;
  ubyte codeB;
  ubyte flagB;
} CFItem;

```

But I assume, the fail is not there, because when I define manually: GITVER=3686e892 A\_Typedef.h don’t fails and compile right.

---

<div class="post-metadata">

**Author:** ![ClausKlein](https://discourse.cmake.org/user_avatar/discourse.cmake.org/clausklein/32/352_2.png) [@ClausKlein](https://discourse.cmake.org/u/ClausKlein)\
**Post date:** [March 2, 2021, 2:35pm UTC](https://discourse.cmake.org/t/cmake-setting-a-macro-from-a-console-program-solved/2869/5 "2021-03-02T14:35:06Z")

</div>

I can’t see `GITVER` in you code?

try to build with `cmake -G Ninja ...` and use `ninja -nv` or have a look in the `build.ninja` file  
than you will see the compiler args.

---

<div class="post-metadata">

**Author:** ![Jos](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/j/2bfe46/32.png) [@Jos](https://discourse.cmake.org/u/Jos)\
**Post date:** [March 2, 2021, 2:37pm UTC](https://discourse.cmake.org/t/cmake-setting-a-macro-from-a-console-program-solved/2869/6 "2021-03-02T14:37:09Z")

</div>

Hi Claus, GITVER is defined from CMakeLists.txt

```auto
target_compile_definitions(LibDEFINES PRIVATE
	GITVER=${GIT_VER}
)

```

f I define manually GITVER=0x3686e892, the warning is out, and compiles perfect, so the question is, how can i turn the console string output into Hexadecimal?

---

<div class="post-metadata">

**Author:** ![ClausKlein](https://discourse.cmake.org/user_avatar/discourse.cmake.org/clausklein/32/352_2.png) [@ClausKlein](https://discourse.cmake.org/u/ClausKlein)\
**Post date:** [March 2, 2021, 2:42pm UTC](https://discourse.cmake.org/t/cmake-setting-a-macro-from-a-console-program-solved/2869/7 "2021-03-02T14:42:48Z")

</div>

I know, your code is a miracle to me!

typeof(Sem)?  
init of semPS?

IMHO that is no cmake problem!

```cmake
target_compile_definitions(LibDEFINES PRIVATE
	GITVER=0x${GIT_VER}
)

```

---

<div class="post-metadata">

**Author:** ![Jos](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/j/2bfe46/32.png) [@Jos](https://discourse.cmake.org/u/Jos)\
**Post date:** [March 2, 2021, 2:50pm UTC](https://discourse.cmake.org/t/cmake-setting-a-macro-from-a-console-program-solved/2869/8 "2021-03-02T14:50:56Z")

</div>

Thank you,  
good, I’m making a port of a QMake working code ☹

Sadly I get the same fail with

```auto
target_compile_definitions(LibDEFINES PRIVATE
	GITVER=0x${GIT_VER}
)

```

---

<div class="post-metadata">

**Author:** ![ClausKlein](https://discourse.cmake.org/user_avatar/discourse.cmake.org/clausklein/32/352_2.png) [@ClausKlein](https://discourse.cmake.org/u/ClausKlein)\
**Post date:** [March 2, 2021, 3:01pm UTC](https://discourse.cmake.org/t/cmake-setting-a-macro-from-a-console-program-solved/2869/9 "2021-03-02T15:01:12Z")

</div>

> [@ClausKlein](#):
>
> I know, your code is a miracle to me!
> 
> typeof(Sem)?  
> init of semPS?

?

---

<div class="post-metadata">

**Author:** ![Jos](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/j/2bfe46/32.png) [@Jos](https://discourse.cmake.org/u/Jos)\
**Post date:** [March 2, 2021, 3:13pm UTC](https://discourse.cmake.org/t/cmake-setting-a-macro-from-a-console-program-solved/2869/10 "2021-03-02T15:13:16Z")

</div>

After in code search… The type of sem is not defined!!! (!!!) (is not my code, I’m only the new kid in the Firma) but the structure should be not defined

```auto
if !defined(OTHER_BUILD)

```

because OTHER\_BUILD is enabled.

---

<div class="post-metadata">

**Author:** ![ClausKlein](https://discourse.cmake.org/user_avatar/discourse.cmake.org/clausklein/32/352_2.png) [@ClausKlein](https://discourse.cmake.org/u/ClausKlein)\
**Post date:** [March 2, 2021, 3:47pm UTC](https://discourse.cmake.org/t/cmake-setting-a-macro-from-a-console-program-solved/2869/11 "2021-03-02T15:47:56Z")

</div>

> [@ClausKlein](#):
>
> try to build with `cmake -G Ninja ...` and use `ninja -nv LibDEFINES` or have a look in the `build.ninja` file  
> than you will see the compiler args.

doit!

---

<div class="post-metadata">

**Author:** ![ClausKlein](https://discourse.cmake.org/user_avatar/discourse.cmake.org/clausklein/32/352_2.png) [@ClausKlein](https://discourse.cmake.org/u/ClausKlein)\
**Post date:** [March 2, 2021, 3:53pm UTC](https://discourse.cmake.org/t/cmake-setting-a-macro-from-a-console-program-solved/2869/12 "2021-03-02T15:53:01Z")

</div>

> [@Jos](#):
>
> `GITVER`

how do you think comes this define in you code?  
do not work with code you do not understand!

---

<div class="post-metadata">

**Author:** ![Jos](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/j/2bfe46/32.png) [@Jos](https://discourse.cmake.org/u/Jos)\
**Post date:** [March 2, 2021, 4:02pm UTC](https://discourse.cmake.org/t/cmake-setting-a-macro-from-a-console-program-solved/2869/13 "2021-03-02T16:02:25Z")

</div>

The problem is that  
execute\_process (  
COMMAND git rev-parse --short HEAD  
OUTPUT\_VARIABLE GIT\_VER  
)  
Introduces an extra “\n” at the end of the GIT\_VER string, now should I only supress the “\n” from the GIT\_VER string

---

<div class="post-metadata">

**Author:** ![Jos](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/j/2bfe46/32.png) [@Jos](https://discourse.cmake.org/u/Jos)\
**Post date:** [March 2, 2021, 4:26pm UTC](https://discourse.cmake.org/t/cmake-setting-a-macro-from-a-console-program-solved/2869/14 "2021-03-02T16:26:01Z")

</div>

Here is the solution

```auto
execute_process (
    COMMAND git rev-parse --short HEAD
    OUTPUT_VARIABLE GIT_VER
)
string(STRIP ${GIT_VER} GIT_VER) #to remove the "\n" at the end

target_compile_definitions(LibDEFINES PRIVATE
    GITVER=0x${GIT_VER}
)

```

---

<div class="post-metadata">

**Author:** ![Jos](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/j/2bfe46/32.png) [@Jos](https://discourse.cmake.org/u/Jos)\
**Post date:** [March 2, 2021, 4:36pm UTC](https://discourse.cmake.org/t/cmake-setting-a-macro-from-a-console-program-solved/2869/15 "2021-03-02T16:36:53Z")

</div>

Wenn die Quelle 1GB ist, kannst du kaum verstehen, aber nur Teile.

Viele Grüße und vielen Dank. Es gab gute Absicht

---

<div class="post-metadata">

**Author:** ![ben.boeckel](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/b/ea5d25/32.png) [@ben.boeckel](https://discourse.cmake.org/u/ben.boeckel)\
**Post date:** [March 2, 2021, 4:48pm UTC](https://discourse.cmake.org/t/cmake-setting-a-macro-from-a-console-program-solved/2869/16 "2021-03-02T16:48:17Z")

</div>

Better is to use the `OUTPUT_STRIP_TRAILING_WHITESPACE` argument to `execute_process` rather than manually stripping it.

---

<div class="post-metadata">

**Author:** ![Jos](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/j/2bfe46/32.png) [@Jos](https://discourse.cmake.org/u/Jos)\
**Post date:** [March 2, 2021, 4:49pm UTC](https://discourse.cmake.org/t/cmake-setting-a-macro-from-a-console-program-solved/2869/17 "2021-03-02T16:49:53Z")

</div>

Thank you Ben, I’ll take a look
