How to determine the 64-bit signed integer type and printf specifier with CMake?

einpoklum

In my project's CMakeLists.txt I have:

include(CheckIncludeFiles)
configure_file(config.h.in config.h @ONLY)

and I want to check for my platform's 64-bit integer type and printf conversion specifier for it, for use in C via config.h. How do I do that?

Note: I know that on most systems we can just use stdint.h's int64_t type, but suppose I can't assume stdint.h is available.

Mizux

I would use:

include(CheckIncludeFile)
check_include_file(stdint.h HAVE_STDINT_H)

or

include(CheckIncludeFileCXX)
check_include_file_cxx(cstdint HAVE_CSTDINT)

For int64_t I would do:

include(CheckTypeSize)
check_type_size("int64_t" SIZEOF_INT64_T)
check_type_size("long long" SIZEOF_LONG_LONG)
check_type_size("long" SIZEOF_LONG)

if(SIZEOF_INT64_T EQUAL "8")
  set(INT64_T "int64_t")
elseif(SIZEOF_LONG EQUAL "8")
  set(INT64_T "long")
elseif(SIZEOF_LONG_LONG EQUAL "8")
  set(INT64_T "long long")
else()
  message(FATAL_ERROR "Can't find suitable int64_t")
endif()
message(STATUS "Found int64_t: ${INT64_T}")

and then use a compile_definitions() or configure_file() to propagate it in your source code...

Also, you can try to look for the symbols otherwise:

set(TEST_INCLUDES "")
if(HAVE_STDINT_H)
    list(APPEND TEST_INCLUDES "stdint.h")
endif()
include(CheckCXXSymbolExists)
check_cxx_symbol_exists(int64_t "${TEST_INCLUDES}" HAVE_INT64_T)

src: https://github.com/Mizux/Cbc/blob/master/cmake/CheckEnv.cmake

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to printf a 64-bit integer as hex?

Portable printf format specifier for 64 bit hexadecimal?

How to put 32-bit signed integer into higher 32 bits of 64-bit unsigned integer?

Using printf width specifier with fixed width integer type variable

How to add 2^63 to a signed 64-bit integer and cast it to a unsigned 64-bit integer without using 128-bit integer in the middle

Converting binary representation to signed 64 bit integer in Python

Decode a byte array to a signed integer up to 64 bit

Why the literal "-9223372036854775808" (min 64 bit signed integer) is not accepted by kotlin?

Print a signed 64-bit integer in hexadecimal and octal

bit shifting signed integer

Convert 32-bit signed integer to 64-bit integer while preserving the exact bits

How to multiply double (floating point) by an integer type (32-bit, 64-bit, 128-bit, etc.) manually

How do I convert a signed 8-bit byte to a signed 16-bit integer in assembly?

Convert two 32 bit integers to one signed 64 bit integer string

How to detect if 64 bit MSVC with cmake?

CMake: How to prevent 64 bit build?

How to test the most significant bit of signed or unsigned integer?

How to find the most significant bit of a signed integer in C

How to use "zd" specifier with `printf()`?

How to create a 64 bit Unique Integer in Java

How big can a 64 bit unsigned integer be?

How to determine whether a given Linux is 32 bit or 64 bit?

How to convert signed 16 bit integer to unsigned 16 bit integer in Java?

Rust: How to cast from a signed integer type to a larger signed integer type *without* sign extension

Delphi Warning W1073 Combining signed type and unsigned 64-bit type - treated as an unsigned type

is an argument of type long OK for %x printf specifier?

printf data type specifier complex question

How to convert integer to unsigned 64-bit integer

How to determine if a number is any type of int (core or numpy, signed or not)?