I’m working on a multiple file project in C++ that gives me linker errors and I don’t understand why. Yes, I have been searching but others all seem to have troubles with third-party libraries and -l link flags, which is not the case here. A small excerpt that should include all relevant pieces of code:
main.cpp
#include "common.h"
//...
void sort_table(byte table[TABLE_LENGTH][2][CIPHER_SIZE], const unsigned int beg = 0, const unsigned int end = TABLE_LENGTH)
{
//..
}
void precompute_table(byte table[TABLE_LENGTH][2][CIPHER_SIZE])
{
//..
#ifdef METHOD_HELLMAN
precompute_hellman_table(table);
#endif
//..
}
int main()
{
//..
byte table [TABLE_LENGTH][2][CIPHER_SIZE];
precompute_table(table);
//..
}
common.h
#ifndef COMMON_H
#define COMMON_H
//..
typedef unsigned char byte;
//..
#define METHOD_HELLMAN
#define TABLE_LENGTH 40000
#define CIPHER_SIZE 2
//..
void sort_table(byte[TABLE_LENGTH][2][CIPHER_SIZE]);
//..
#endif
hellman.h
#ifndef HELLMAN_H
#define HELLMAN_H
#include "common.h"
extern void precompute_hellman_table(byte[TABLE_LENGTH][2][CIPHER_SIZE]);
#endif
hellman.cpp
#include "hellman.h"
//..
void precompute_hellman_table(byte table[TABLE_LENGTH][2][CIPHER_SIZE])
{
//..
sort_table(table);
}
So hellman.cpp uses a general function in main.cpp, which is forward declared in common.h. The following errors are produced when compiling/linking using MinGW:
File Message
obj\Release\hellman.o:hellman.cpp undefined reference to `sort_table(unsigned char (*) [2][2])'
Why is my code incorrect?
Here’s your declaration
Here’s your definition
See the difference?
The declaration and the definition should be the same, except the default values should be in the declaration only.
Like this
and this