Search code examples
cunresolved-externaljump-table

Jump/Branch Table in C Programming


Kindly Help Me With The following piece of code

//jmp_common.h

typedef void (*jmp_Handler_t)(void);

#define JMP_CMD_HANDLER(com)    extern void Jmp_Handler_##com(void)

#define JMP_DEF_COM(com) extern void Jmp_Handler_##com(void);

#include "jmp_cmd_list.h"

#undef JMP_DEF_COM

typedef struct JmpStruct
{
    char * name;
    jmp_Handler_t handler;
}JmpStruct_t;

/*********************************/
/*********************************/

//jmp_cmd_list.h

JMP_DEF_COM(HELLO)
JMP_DEF_COM(WORLD)

/*********************************/
/*********************************/

//jmp_cmd_handlers.c

#include "jmp_common.h"

JMP_CMD_HANDLER(HELLO)
{
    int a = 100;
}

JMP_CMD_HANDLER(WORLD)
{
    int a = 100;
}

/*********************************/
/*********************************/

//main.c
#include "jmp_common.h"

#define JMP_DEF_COM(com) { #com , Jmp_Handler_##com },

const JmpStruct_t JumpTable[/*JMP_CMD_MAX*/2] = {
#include "jmp_cmd_list.h"
};

int main(void)
{
    JumpTable[0].handler();
    return 0;
}

The Problem Is it that the code fails to compile with a link error unresolved symbol void __cdecl Jmp_Handler_HELLO(void) However when the First Line of code in the main function is removed the code compiles succesfully. Kindly Help


Solution

  • Found the problem. Every file was a c file (.c extension) except main file it was c++ file (.cc extension). after changing the extension of main file it compiled smoothly. However I think that it should have compiled before, may be anyone can answer why it was not compiling.

    thankyou @chris and @colonel for your replies.