Darrell Darrell - 2 months ago 18
C Question

Linker Error - typedef struct

I have a compiling/linking error as follows:

Undefined symbols for architecture x86_64:
"_memory_table", referenced from:
_initialize in main.o

linker command failed with exit code 1


The problem may be in the .h file below. Have I done anything wrong with my typedef structs? The main.c simply calls initialize(). The initialize() fuction is below. For brevity, I have taken out all of the #define macros. Main is included further down the code.

Thanks.

//globals.h
#ifndef memory_manager_globals_h
#define memory_manager_globals_h

/******** Memory Table Entry Data Structure**********/
typedef struct
{
uint32_t block_address;
uint32_t next_free_block;

}mem_table_entry_t;

/******** Memory Table Data Structure**********/
typedef struct
{
mem_table_entry_t two_kib[8];
mem_table_entry_t one_kib[16];
mem_table_entry_t half_kib[32];
mem_table_entry_t quarter_kib[64];
mem_table_entry_t eighth_kib[128];

}mem_table_t;

extern mem_table_t memory_table;

#endif


...

//MAIN.C
#include <stdio.h>
#include "globals.h"


void initialize(void)
{

int block_count = 0; //
uint32_t dynamic_address = HEAP_START;

while(block_count <= INITIALIZE_BLOCK_COUNT)
{
if((block_count >= TWO_KIB_LO) && (block_count < TWO_KIB_HI))
{
memory_table.two_kib[block_count].block_address = HEAP_START;
dynamic_address = dynamic_address + 0x800;

memory_table.two_kib[block_count].next_free_block = dynamic_address;
block_count++;
}
...


[SOLVED] The correct format of the .h file should be as follows:

/******** Memory Table Entry Data Structure**********/
typedef struct
{
uint32_t block_address;
uint32_t next_free_block;

}mem_table_entry_t;


/******** Memory Table Data Structure**********/
typedef struct
{
mem_table_entry_t two_kib[8];
mem_table_entry_t one_kib[16];
mem_table_entry_t half_kib[32];
mem_table_entry_t quarter_kib[64];
mem_table_entry_t eighth_kib[128];

}mem_table_t;

mem_table_t memory_table;

extern mem_table_t memory_table;

Answer

You are missing a definition of memory_table. The extern line is a declaration, not a definition.

Add this line to your C file to fix the linking problem:

mem_table_t memory_table;