Issue
I'm using cmake's configure_file()
function to set some version information in a C build. The generated output looks like this:
#pragma once
// Time
#define VERSION_HOUR 15
#define VERSION_MINUTE 53
#define VERSION_SECOND 56
I obtain these values via cmake's string(TIMESTAMP)
capabilities:
# Time
string(TIMESTAMP hour "%H")
string(TIMESTAMP min "%M")
string(TIMESTAMP sec "%S")
The problem I'm encountering is that the values returned by string(TIMESTAMP)
have leading zeros. For example, I can end up with something like this:
#pragma once
#define VERSION_HOUR 15
#define VERSION_MINUTE 03
#define VERSION_SECOND 06
In this case compilation of the code base will fail as there are parts in the code which basically look like this:
const build_version_t build_version = {
.hour = VERSION_HOUR,
.minute = VERSION_MINUTE,
.second = VERSION_SECOND
}
Where .hour
, .minute
and .second
are fields of type uint8_t
.
Literals with a leading zero are interpreted as octal.
What is the easiest/best way to strip the leading zeros from the corresponding cmake variable(s)?
Solution
There are tons of ways to handle this. One way is to evaluate it using math
. For example:
set(hour 09)
math(EXPR hour ${hour})
message(STATUS ">${hour}<")
Outputs:
-- >9<
(The angle brackets are only there to show that there are no other character in the final value of hour
.)
Answered By - Lindydancer