链接器错误 - typedef 结构
Linker Error - typedef struct
我有一个compiling/linking错误如下:
Undefined symbols for architecture x86_64:
"_memory_table", referenced from:
_initialize in main.o
linker command failed with exit code 1
问题可能出在下面的.h 文件中。我的 typedef 结构做错了吗? main.c 只是调用 initialize()。 initialize() 函数如下。为简洁起见,我删除了所有#define 宏。 Main 包含在代码的更下方。
谢谢。
//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++;
}
...
[已解决].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;
mem_table_t memory_table;
extern mem_table_t memory_table;
您缺少 memory_table
的定义。 extern
行是声明,不是定义。
将此行添加到您的 C 文件以修复链接问题:
mem_table_t memory_table;
我有一个compiling/linking错误如下:
Undefined symbols for architecture x86_64:
"_memory_table", referenced from:
_initialize in main.o
linker command failed with exit code 1
问题可能出在下面的.h 文件中。我的 typedef 结构做错了吗? main.c 只是调用 initialize()。 initialize() 函数如下。为简洁起见,我删除了所有#define 宏。 Main 包含在代码的更下方。
谢谢。
//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++;
}
...
[已解决].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;
mem_table_t memory_table;
extern mem_table_t memory_table;
您缺少 memory_table
的定义。 extern
行是声明,不是定义。
将此行添加到您的 C 文件以修复链接问题:
mem_table_t memory_table;