Getting an undefined symbol error when compiling a C++ project but the issue occurs when calling a static method defined in a LitTab class. Here's the code below:
#ifndef __dasm__LitTab__
#define __dasm__LitTab__
#include <map>
class LitTab {
private:
static std::map<int, int> table;
public:
static void add(int address, int value);
static int get(int address);
static bool contains(int address);
};
#endif
and the implementation:
#include "LitTab.hpp"
std::map<int, int> LitTab::table;
void LitTab::add(int address, int value) {
table.insert(std::pair<int, int>(address, value));
}
bool LitTab::contains(int address) {
return LitTab::table.find(address) != LitTab::table.end();
}
int LitTab::get(int address) {
if(LitTab::contains(address)) return LitTab::table[address];
return NULL;
}
and here's the calling main function:
#include "LitTab.hpp"
using namespace std;
map<int, int> LitTab::table; // is this necessary?
int main(int argc, const char * argv[]) {
LitTab::add(1, 1); // fails here
return 0;
}
Here's the error message:
c++ -c -o regex.o regex.cpp
g++ -g -Wall -O0 -o dasm main.o optab.o LitTab.o File.o RecordTokenizer.o InstructionTokenizer.o regex.o
duplicate symbol __ZN6LitTab5tableE in:
main.o
LitTab.o
ld: 1 duplicate symbol for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make: *** [dasm] Error 1
Honestly, I've spent the last hour and a half search through forums, SO, and other websites and I'm tried of looking and I dunno what else to do. I need help.
Edit: Makefile addition
CC=g++
CFLAGS=-g -Wall -O0
all: dasm
dasm: main.o optab.o LitTab.o File.o RecordTokenizer.o InstructionTokenizer.o regex.o
$(CC) $(CFLAGS) -o $@ $^
clean:
rm *.o dasm