CheckTypeSize: add a test for size of struct members

This commit is contained in:
Rolf Eike Beer 2012-08-13 19:27:44 +02:00
parent 48783b71fb
commit c6fed68ef8
4 changed files with 68 additions and 2 deletions

View File

@ -12,7 +12,13 @@ check_type_size(__int64 SIZEOF___INT64)
check_type_size(size_t SIZEOF_SIZE_T)
check_type_size(ssize_t SIZEOF_SSIZE_T)
set(CMAKE_REQUIRED_INCLUDES "${CMAKE_CURRENT_SOURCE_DIR}")
set(CMAKE_EXTRA_INCLUDE_FILES somestruct.h)
check_type_size("((struct somestruct*)0)->someint" SIZEOF_STRUCTMEMBER_INT)
check_type_size("((struct somestruct*)0)->someptr" SIZEOF_STRUCTMEMBER_PTR)
check_type_size("((struct somestruct*)0)->somechar" SIZEOF_STRUCTMEMBER_CHAR)
configure_file(config.h.in config.h)
include_directories(${CheckTypeSize_BINARY_DIR})
include_directories("${CheckTypeSize_BINARY_DIR}")
add_executable(CheckTypeSize CheckTypeSize.c)

View File

@ -1,4 +1,5 @@
#include "config.h"
#include "somestruct.h"
#ifdef HAVE_SYS_TYPES_H
# include <sys/types.h>
@ -29,6 +30,7 @@
int main()
{
int result = 0;
struct somestruct x;
/* void* */
#if !defined(HAVE_SIZEOF_DATA_PTR)
@ -118,5 +120,41 @@ int main()
NODEF(SIZEOF_SSIZE_T);
#endif
return result;
/* struct somestruct::someint */
#if defined(SIZEOF_STRUCTMEMBER_INT)
CHECK(x.someint, SIZEOF_STRUCTMEMBER_INT);
CHECK(x.someint, SIZEOF_INT);
# if !defined(HAVE_SIZEOF_STRUCTMEMBER_INT)
NODEF(HAVE_SIZEOF_STRUCTMEMBER_INT);
# endif
#elif defined(HAVE_SIZEOF_STRUCTMEMBER_INT)
NODEF(SIZEOF_STRUCTMEMBER_INT);
#endif
/* struct somestruct::someptr */
#if defined(SIZEOF_STRUCTMEMBER_PTR)
CHECK(x.someptr, SIZEOF_STRUCTMEMBER_PTR);
CHECK(x.someptr, SIZEOF_DATA_PTR);
# if !defined(HAVE_SIZEOF_STRUCTMEMBER_PTR)
NODEF(HAVE_SIZEOF_STRUCTMEMBER_PTR);
# endif
#elif defined(HAVE_SIZEOF_STRUCTMEMBER_PTR)
NODEF(SIZEOF_STRUCTMEMBER_PTR);
#endif
/* struct somestruct::someint */
#if defined(SIZEOF_STRUCTMEMBER_CHAR)
CHECK(x.somechar, SIZEOF_STRUCTMEMBER_CHAR);
CHECK(x.somechar, SIZEOF_CHAR);
# if !defined(HAVE_SIZEOF_STRUCTMEMBER_CHAR)
NODEF(HAVE_SIZEOF_STRUCTMEMBER_CHAR);
# endif
#elif defined(HAVE_SIZEOF_STRUCTMEMBER_CHAR)
NODEF(SIZEOF_STRUCTMEMBER_CHAR);
#endif
/* to avoid possible warnings about unused or write-only variable */
x.someint = result;
return x.someint;
}

View File

@ -37,3 +37,15 @@
/* ssize_t */
#cmakedefine HAVE_SIZEOF_SSIZE_T
@SIZEOF_SSIZE_T_CODE@
/* struct somestruct::someint */
#cmakedefine HAVE_SIZEOF_STRUCTMEMBER_INT
@SIZEOF_STRUCTMEMBER_INT_CODE@
/* struct somestruct::someptr */
#cmakedefine HAVE_SIZEOF_STRUCTMEMBER_PTR
@SIZEOF_STRUCTMEMBER_PTR_CODE@
/* struct somestruct::somechar */
#cmakedefine HAVE_SIZEOF_STRUCTMEMBER_CHAR
@SIZEOF_STRUCTMEMBER_CHAR_CODE@

View File

@ -0,0 +1,10 @@
#ifndef _CMAKE_SOMESTRUCT_H
#define _CMAKE_SOMESTRUCT_H
struct somestruct {
int someint;
void *someptr;
char somechar;
};
#endif