0

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
djthoms
  • 3,026
  • 2
  • 31
  • 56
  • 1
    Note that symbols starting with (or, indeed, containing) a double underscore are reserved for the implementation. That means you should never use them in your own code unless you are using someone else's definition. – Jonathan Leffler Apr 27 '15 at 04:43
  • 1
    Your "is this necessary" line is not necessary. The definition in the implementation should be hidden from the outside world — made static or placed in the anonymous namespace or … It should not be visible outside that file. – Jonathan Leffler Apr 27 '15 at 04:45
  • 1
    Does your linker line include both `main.o` and `LitMap.o`? Superficially, it probably doesn't. But it would improve your question no end if you did show the command being executed that fails. – Jonathan Leffler Apr 27 '15 at 04:47
  • @JonathanLeffler good to know! It's a compile error with I run my make file. Should I post that too or no? – djthoms Apr 27 '15 at 04:54
  • 1
    We probably don't need to see the makefile; we just need to see the command that is executed to link the program. If it doesn't list both `main.o` and `LitMap.o` as object files, then you've got a problem. The problem will require a fix to the makefile, so there's no harm in posting it if it is small (say 20 lines or less). But show the failing command line as reported by make (assuming you don't hide what is being executed with `@` markers). – Jonathan Leffler Apr 27 '15 at 04:57
  • @JonathanLeffler when I run `make` it gives me that error. – djthoms Apr 27 '15 at 05:07
  • Never mind... Figured it out – djthoms Apr 27 '15 at 05:08
  • 1
    Actually, it isn't `make` that gives the error; it is the C++ compiler that you are running from `make` that gives the error. The issue is: what is the command line that `make` is running when you get that error? – Jonathan Leffler Apr 27 '15 at 05:09
  • Last one: I appreciate the help. I neglected to add `LitTab.o` to my makefile and once I did that and removed the table dec. in `main.cpp` it compiled fine. I was being a n00b. I really appreciate your help though! – djthoms Apr 27 '15 at 05:10
  • Note that the conventional `make` macro for the C++ compiler is usually `CXX`, and the corresponding flags are normally in `CXXFLAGS`. You should not, usually, use `CC` and `CFLAGS` for C++ compilation. – Jonathan Leffler Apr 27 '15 at 05:10
  • @JonathanLeffler Not sure what you mean by that – djthoms Apr 27 '15 at 05:11

0 Answers0