1

I started working on a language parser and came across with undefined reference while I was using std::map<std::string, std::string>

/usr/bin/ld: /home/leksoBor/Desktop/research/neuLogo/src/./rules/0000_program_base/Program.hh:106: undefined reference to `ProgramObj::envCnfgTable[abi:cxx11]'
    /usr/bin/ld: /home/leksoBor/Desktop/research/neuLogo/src/./rules/0000_program_base/Program.hh:107: undefined reference to `ProgramObj::envCnfgTable[abi:cxx11]'
    /usr/bin/ld: /home/leksoBor/Desktop/research/neuLogo/src/./rules/0000_program_base/Program.hh:108: undefined reference to `ProgramObj::envCnfgTable[abi:cxx11]'
    /usr/bin/ld: /home/leksoBor/Desktop/research/neuLogo/src/./rules/0000_program_base/Program.hh:109: undefined reference to `ProgramObj::envCnfgTable[abi:cxx11]'
    /usr/bin/ld: parser.tab.o:/home/leksoBor/Desktop/research/neuLogo/src/./rules/0000_program_base/Program.hh:113: more undefined references to `ProgramObj::envCnfgTable[abi:cxx11]' follow

This is the error message I am getting and the code is following:

static void resetEnvCnfgTable()
  {
    envCnfgTable.insert(std::pair<std::string,std::string>("version", "0.0") );
    envCnfgTable.insert(std::pair<std::string,std::string>("description", "no description"));
    envCnfgTable.insert(std::pair<std::string,std::string>("real-type", "64-bit"));
    envCnfgTable.insert(std::pair<std::string,std::string>("whole-type", "32-bit"));
    envCnfgTable.insert(std::pair<std::string,std::string>("random-type", "mersenne"));
  };
  static void printEnvCnfgTable()
  {
    for(std::map<std::string, std::string>::iterator it = envCnfgTable.begin(); it != envCnfgTable.end(); it ++)
    {
      std::cout << it -> first << "   " << it -> second << '\n';
    } 
  };
  static std::map<std::string, std::string> envCnfgTable; 

I use:

clang++ -g -c -Wall -std=c++11  -o ../../parts/grammar/Program.o Program.cc

Any suggestions? I saw some threads similiar but could not resolve the problem.

  • Is this the whole code? Is this inside a `class something {... }` or inside a namespace? Please post an [MCVE]. – KamilCuk Jun 21 '20 at 09:40
  • it's inside of a class. – lekso borashvili Jun 23 '20 at 06:28
  • Does this answer your question? [What is the correct way to initialize static data members in C++ (98, 11 and 14)](https://stackoverflow.com/questions/26648817/what-is-the-correct-way-to-initialize-static-data-members-in-c-98-11-and-14) – KamilCuk Jun 23 '20 at 06:50
  • Not really. I tried to find solution before making this thread and found out that there are a lot of problems associated with linking older and newer versions of gcc because of dual ABI. However, I was not able to fix this particular problem. To make sure I will try to test non-static version as well. – lekso borashvili Jun 23 '20 at 09:14
  • Add `static std::map classname::envCnfgTable;` member definition in a translation unit outside of class definition.. Please create an [MCVE]. `gcc because of dual ABI` How is your question related to gcc? How is you question related to "ABI" and what "dual ABI" are you referring to and how is it related to your question? `with linking older` In your question you are compiling only a single object file. Please create an [MCVE]. How do you invoke the linker? With what object files? What is "ordered" for the linker? – KamilCuk Jun 23 '20 at 09:18
  • I think you directed me to the solution. I tried non static version and it works. The reason I was thinking it was caused by linking stuff was cxx11 error that I was getting. I will try to transform it to static and make it work now thanks. – lekso borashvili Jun 23 '20 at 09:36
  • `static std::map classname::envCnfgTable` worked. Thanks – lekso borashvili Jun 23 '20 at 09:44

0 Answers0