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

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.

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

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.

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.

Hi Claus, GITVER is defined from CMakeLists.txt

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?

I know, your code is a miracle to me!

typeof(Sem)?
init of semPS?

IMHO that is no cmake problem!

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

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

Sadly I get the same fail with

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

?

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

if !defined(OTHER_BUILD)

because OTHER_BUILD is enabled.

doit!

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

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

Here is the solution

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}
)

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

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

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

Thank you Ben, I’ll take a look