I did try to find my specific issue, but the closest I found was this question where the issue was with global variables.I need make a lexer header file along with it's cpp file to use in main.cpp. here are the two lexer files:// lexer.hpp#ifndef LEXER_HPP#define LEXER_HPP// Some include files, not really relevantnamespace interpreter{ namespace lex { enum class TokType : char; typedef std::vector<std::pair<TokType, std::string>> TokenList; typedef std::pair<TokType, std::string> Token; TokenList tokenizeString(std::string tokStr); } // namespace lex} // namespace interpreter#endif/* --------------------------------------*/// lexer.cpp// Again, some not-really-necessary include files#include "lexer.hpp"using namespace interpreter::lex;enum class TokType : char {/* Stuff */};TokenList tokenizeString(std::string tokStr) {/* Stuff*/};When I use this stuff in main.cpp and compile, I get the following error:/usr/bin/ld: /tmp/cc3XtuwW.o: in function `main':main.cpp:(.text+0x7d): undefined reference to `interpreter::lex::tokenizeString(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >)'collect2: error: ld returned 1 exit statusClearly, something is wrong with my function, but I cannot for the life of me figure out what is actually wrong.Here's some other information if you need it:I turned my lexer.cpp into a .o file. Whether I use the .cpp file or the .o file does not make a difference, though.How I compiled main.cpp: g++ -o main main.cpp lexer.olexer.hpp, lexer.cpp, lexer.o and main.cpp are all in the same directory. 解决方案 Do thisTokenList interpreter::lex::tokenizeString(std::string tokStr){ /* Stuff*/}or thisnamespace interpreter { namespace lex { TokenList tokenizeString(std::string tokStr) { /* Stuff*/ }} }Although you've said using namespace interpreter::lex; that doesn't mean that newly defined names get added to the interpreter::lex namespace. 这篇关于链接器错误:头文件中对函数的未定义引用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 08-05 01:37