当我尝试编译代码时,我遇到了链接器错误。
当我使用g++ *.cpp -std=c++11 -o run
进行编译时,出现以下错误:
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
我已经尝试分别编译和链接所有文件无济于事。它不是会员功能,因此它不是标签问题。我还尝试使用g++ -std=c++11 main.cpp mAI.cpp -o run
来确保它实际上正在编译和链接这两个文件,但没有运气。
让我疯狂的是,它并没有抱怨Translate
函数以与Actions函数相同的方式声明和定义。
非常感谢任何帮助。
#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
#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
}
#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;
}
答案 0 :(得分:2)
这听起来像一个非常古老的gcc编译器。在早期版本的gcc和系统上安装的库中进行abi更改时,这是一个问题。
使用-D_GLIBCXX_USE_CXX11_ABI=0
并重新编译所有可能有帮助。
另见本文档: https://gcc.gnu.org/onlinedocs/libstdc%2B%2B/manual/using_dual_abi.html
顺便说一句: 我可以使用gcc 7.3.1(fedora)编译和链接你的代码,没有任何错误。在剪切和粘贴代码之后,对未使用的变量进行了一些警告,并且只添加了一个空的aBoard类。所以我相信你的代码没有任何问题。
来自文档:
如果您收到有关涉及std :: __ cxx11命名空间中的类型或标签[abi:cxx11]的符号的未定义引用的链接器错误,那么它可能表示您正在尝试将使用不同值编译的目标文件链接在一起对于_GLIBCXX_USE_CXX11_ABI宏。当链接到使用旧版GCC编译的第三方库时,通常会发生这种情况。如果无法使用新的ABI重建第三方库,则需要使用旧的ABI重新编译代码。