Linker Error: Undefined Reference to Function
Linker Error: Undefined Reference to Function
我在尝试编译代码时 运行 遇到链接器错误。
当我使用 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 函数相同。
非常感谢任何帮助。
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
}
主要
#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;
}
这听起来像是一个非常古老的 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 ) 编译 link 你的代码,没有任何错误。剪切和粘贴代码后未使用变量的一些警告,只添加一个空的 aBoard class。所以我相信你的代码没有问题。
来自文档:
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.
我在尝试编译代码时 运行 遇到链接器错误。
当我使用 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 函数相同。
非常感谢任何帮助。
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
}
主要
#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;
}
这听起来像是一个非常古老的 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 ) 编译 link 你的代码,没有任何错误。剪切和粘贴代码后未使用变量的一些警告,只添加一个空的 aBoard class。所以我相信你的代码没有问题。
来自文档:
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.