1

I'm running into a linker error when I try to compile my code.

When I compile using g++ *.cpp -std=c++11 -o run I get the following error:

main.cpp:(.text+0x355): undefined reference to `Actions(mBoard&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::set<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::less<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >, std::allocator<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > > >)'
collect2: error: ld returned 1 exit status

I've tried compiling and linking all the files separately to no avail. Its not a member function so it's not a label issue. I've also tried to use g++ -std=c++11 main.cpp mAI.cpp -o run to make sure it is in fact compiling and linking both files but no luck.

What's really making me crazy is that it's not complaining about the Translate function that's declared and defined in the same way as the Actions function.

Any help is greatly appreciated.

mAI.hpp

#ifndef MAI_HPP
#define MAI_HPP

#include <iostream>
#include <set>
#include <tuple>
#include "mBoard.hpp"
using namespace std;

void Actions(mBoard const &state, string const playerColor, set<string> moveList);
tuple<int, int> Translate(string index);

#endif

mAI.cpp

#include "mAI.hpp"

std::set<char> blackPieces = {'r', 'n', 'b', 'q', 'k', 'p'};
std::set<char> whitePieces = {'R', 'N', 'B', 'Q', 'K', 'P'};

void Actions(mBoard const &state, string const playerColor, set<string> moveList)
{
    //Do some stuff
}

tuple<int, int> Translate(string index)
{
    //Do different stuff
}

main

#include "mAI.hpp"
#include "mBoard.hpp"
#include <set>
#include <tuple>
#include <string>
#include <iostream>

int main()
{

  mBoard dasBoard;
  set<string> moveList;
  string color = "White";

  cout<<"TEST - Translate: f6 to file, rank: ";
  tuple<int, int> test;
  test = Translate("f6");
  cout << get<0>(test) << ", " << get<1>(test) << endl;

  Actions(dasBoard, color, moveList);
  return 0;
}
Oberon311
  • 39
  • 5
  • I put that in and get the same linker error... – Oberon311 Mar 04 '18 at 18:23
  • Done and done with no change. I can see main but out of curiosity why in mAI.cpp? With the include of the .hpp wouldn't it include there automatically? – Oberon311 Mar 04 '18 at 18:30
  • [mcve] needed. And incidentally see [Why is “using namespace std” considered bad practice?](https://stackoverflow.com/q/1452721/1362568) – Mike Kinghan Mar 04 '18 at 18:46

1 Answers1

2

That sounds like a very old gcc compiler. That was a problem during abi change in earlier versions of gcc and installed libraries on the system.

using -D_GLIBCXX_USE_CXX11_ABI=0 and recompile all maybe help.

See also this documentation: https://gcc.gnu.org/onlinedocs/libstdc%2B%2B/manual/using_dual_abi.html

BTW: I can compile and link your code with gcc 7.3.1 ( fedora ) without any error. Some warnings of unused vars after cut and paste your code and only add an empty aBoard class. So I believe there is nothing wrong with your code.

From the documentation:

If you get linker errors about undefined references to symbols that involve types in the std::__cxx11 namespace or the tag [abi:cxx11] then it probably indicates that you are trying to link together object files that were compiled with different values for the _GLIBCXX_USE_CXX11_ABI macro. This commonly happens when linking to a third-party library that was compiled with an older version of GCC. If the third-party library cannot be rebuilt with the new ABI then you will need to recompile your code with the old ABI.

Klaus
  • 24,205
  • 7
  • 58
  • 113