db_tutorial icon indicating copy to clipboard operation
db_tutorial copied to clipboard

Fail to Compile Make (initializer element is not constant)

Open rjrockzz opened this issue 6 years ago • 7 comments

error: initializer element is not constant const uint32_t USERNAME_OFFSET = ID_OFFSET + ID_SIZE; ^~~~~~~~~ /cygdrive/c/Users/RJ/CLionProjects/DB Minor/rj/db.c:56:31: error: initializer element is not constant const uint32_t EMAIL_OFFSET = USERNAME_OFFSET + USERNAME_SIZE; ^~~~~~~~~~~~~~~ /cygdrive/c/Users/RJ/CLionProjects/DB Minor/rj/db.c:57:27: error: initializer element is not constant const uint32_t ROW_SIZE = ID_SIZE + USERNAME_SIZE + EMAIL_SIZE; ^~~~~~~ /cygdrive/c/Users/RJ/CLionProjects/DB Minor/rj/db.c:93:33: error: initializer element is not constant const uint32_t IS_ROOT_OFFSET = NODE_TYPE_SIZE; ^~~~~~~~~~~~~~ /cygdrive/c/Users/RJ/CLionProjects/DB Minor/rj/db.c:95:40: error: initializer element is not constant const uint32_t PARENT_POINTER_OFFSET = IS_ROOT_OFFSET + IS_ROOT_SIZE; ^~~~~~~~~~~~~~ /cygdrive/c/Users/RJ/CLionProjects/DB Minor/rj/db.c:97:5: error: initializer element is not constant NODE_TYPE_SIZE + IS_ROOT_SIZE + PARENT_POINTER_SIZE; ^~~~~~~~~~~~~~ /cygdrive/c/Users/RJ/CLionProjects/DB Minor/rj/db.c:103:48: error: initializer element is not constant const uint32_t INTERNAL_NODE_NUM_KEYS_OFFSET = COMMON_NODE_HEADER_SIZE; ^~~~~~~~~~~~~~~~~~~~~~~ /cygdrive/c/Users/RJ/CLionProjects/DB Minor/rj/db.c:106:5: error: initializer element is not constant INTERNAL_NODE_NUM_KEYS_OFFSET + INTERNAL_NODE_NUM_KEYS_SIZE; ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~ /cygdrive/c/Users/RJ/CLionProjects/DB Minor/rj/db.c:107:44: error: initializer element is not constant const uint32_t INTERNAL_NODE_HEADER_SIZE = COMMON_NODE_HEADER_SIZE + ^~~~~~~~~~~~~~~~~~~~~~~ /cygdrive/c/Users/RJ/CLionProjects/DB Minor/rj/db.c:117:5: error: initializer element is not constant INTERNAL_NODE_CHILD_SIZE + INTERNAL_NODE_KEY_SIZE; ^~~~~~~~~~~~~~~~~~~~~~~~ /cygdrive/c/Users/RJ/CLionProjects/DB Minor/rj/db.c:125:45: error: initializer element is not constant const uint32_t LEAF_NODE_NUM_CELLS_OFFSET = COMMON_NODE_HEADER_SIZE; ^~~~~~~~~~~~~~~~~~~~~~~ /cygdrive/c/Users/RJ/CLionProjects/DB Minor/rj/db.c:128:5: error: initializer element is not constant LEAF_NODE_NUM_CELLS_OFFSET + LEAF_NODE_NUM_CELLS_SIZE; ^~~~~~~~~~~~~~~~~~~~~~~~~~ /cygdrive/c/Users/RJ/CLionProjects/DB Minor/rj/db.c:129:40: error: initializer element is not constant const uint32_t LEAF_NODE_HEADER_SIZE = COMMON_NODE_HEADER_SIZE + ^~~~~~~~~~~~~~~~~~~~~~~ /cygdrive/c/Users/RJ/CLionProjects/DB Minor/rj/db.c:138:39: error: initializer element is not constant const uint32_t LEAF_NODE_VALUE_SIZE = ROW_SIZE; ^~~~~~~~ /cygdrive/c/Users/RJ/CLionProjects/DB Minor/rj/db.c:140:5: error: initializer element is not constant LEAF_NODE_KEY_OFFSET + LEAF_NODE_KEY_SIZE; ^~~~~~~~~~~~~~~~~~~~ /cygdrive/c/Users/RJ/CLionProjects/DB Minor/rj/db.c:141:38: error: initializer element is not constant const uint32_t LEAF_NODE_CELL_SIZE = LEAF_NODE_KEY_SIZE + LEAF_NODE_VALUE_SIZE; ^~~~~~~~~~~~~~~~~~ /cygdrive/c/Users/RJ/CLionProjects/DB Minor/rj/db.c:142:44: error: initializer element is not constant const uint32_t LEAF_NODE_SPACE_FOR_CELLS = PAGE_SIZE - LEAF_NODE_HEADER_SIZE; ^~~~~~~~~ /cygdrive/c/Users/RJ/CLionProjects/DB Minor/rj/db.c:144:5: error: initializer element is not constant LEAF_NODE_SPACE_FOR_CELLS / LEAF_NODE_CELL_SIZE; ^~~~~~~~~~~~~~~~~~~~~~~~~ /cygdrive/c/Users/RJ/CLionProjects/DB Minor/rj/db.c:145:46: error: initializer element is not constant const uint32_t LEAF_NODE_RIGHT_SPLIT_COUNT = (LEAF_NODE_MAX_CELLS + 1) / 2; ^ /cygdrive/c/Users/RJ/CLionProjects/DB Minor/rj/db.c:147:5: error: initializer element is not constant (LEAF_NODE_MAX_CELLS + 1) - LEAF_NODE_RIGHT_SPLIT_COUNT; ^ make[3]: *** [CMakeFiles/db.dir/build.make:63: CMakeFiles/db.dir/rj/db.c.o] Error 1 make[2]: *** [CMakeFiles/Makefile2:73: CMakeFiles/db.dir/all] Error 2 make[1]: *** [CMakeFiles/Makefile2:85: CMakeFiles/db.dir/rule] Error 2 make: *** [Makefile:118: db] Error 2

rjrockzz avatar Aug 13 '19 16:08 rjrockzz

Hi, looks like we are about on the same pace for this. Don't know if you have already found an answer but I just compiled the initial constants of part 8 and was struggling with the same problem. I found this https://stackoverflow.com/questions/2611063/static-define-and-const-in-c and changed the row constants from part 7 to #define ID_SIZE sizeof((Row*(0)->id) #define USERNAME_SIZE sizeof(((Row*)0)->username) #define EMAIL_SIZE sizeof(((Row*)0)->email) #define ID_OFFSET 0 #define USERNAME_OFFSET (ID_OFFSET + ID_SIZE) #define EMAIL_OFFSET (USERNAME_OFFSET + USERNAME_SIZE) #define ROW_SIZE (ID_SIZE + USERNAME_SIZE + EMAIL_SIZE)

for part 8 I did this : /*** COMMON NODE HEADER LAYOUT ***/ #define NODE_TYPE_SIZE sizeof(uint8_t) const uint32_t NODE_TYPE_OFFSET = 0; #define IS_ROOT_SIZE sizeof(uint8_t) #define IS_ROOT_OFFSSET NODE_TYPE_SIZE #define PARENT_POINTER_SIZE sizeof(uint32_t) const uint32_t PARENT_POINTER_OFFSET = IS_ROOT_OFFSET + IS_ROOT_SIZE; const uint32_t COMMON_NODE_HEADER_SIZE = NODE_TYPE_SIZE + IS_ROOT_SIZE + PARENT_POINTER_SIZE;

Notes: From what I gathered from the link is that if there is a literal EXPLICIT constant you can use the const … if it is IMPLICIT it can be a #define.

Hope this helps for the first part, I will be on the leaf_node section next

ghost avatar Aug 22 '19 19:08 ghost

  • typo #define IS_ROOT_OFFSET

ghost avatar Aug 22 '19 19:08 ghost

/*** LEAF_NODE_BODY_LAYOUT ***/ #define LEAF_NODE_KEY_SIZE sizeof(uint32_t) #define LEAF_NODE_KEY_OFFSET 0 #define LEAF_NODE_VALUE_SIZE ROW_SIZE #define LEAF_NODE_VALUE_OFFSET (LEAF_NODE_KEY_OFFSET + LEAF_NODE_KEY_SIZE) #define LEAF_NODE_CELL_SIZE (LEAF_NODE_KEY_SIZE + LEAF_NODE_VALUE_SIZE) #define LEAF_NODE_SPACE_FOR_CELLS (PAGE_SIZE - LEAF_NODE_HEADER_SIZE) #define LEAF_NODE_MAX_CELLS (LEAF_NODE_SPACE_FOR_CELLS / LEAF_NODE_CELL_SIZE)

for /*** LEAF NODE HEADER LAYOUT ***/ I changed all to #define

For your information, I am running this on my Raspberry Pi.

ghost avatar Aug 22 '19 21:08 ghost

Same question, and changed these to #define works well . Besides, I turn it into a cpp file, and the const variables become right.

Mrtj2016 avatar Sep 21 '19 03:09 Mrtj2016

Hi All,

I had the same issue and found that in C const need to be compile-time constant. My solution was to use a C++ compiler to compile the files.

Thanks

m7jay avatar Oct 22 '19 16:10 m7jay

I think the author of this project uses a mac. If I remember correctly gcc is an alias on Mac OS which resolve into clang. When you change gcc into clang you don't get this error.

sirh3e avatar Oct 28 '20 10:10 sirh3e

@ghost does your code working????/

StoneISLive avatar Aug 14 '23 17:08 StoneISLive