[英]Linker error: undefined reference to function from header file
我确实试图找到我的具体问题,但我发现最接近的是这个问题,问题出在全局变量上。
我需要制作一个词法分析器 header 文件及其cpp
文件以在main.cpp
中使用。 这是两个词法分析器文件:
// lexer.hpp
#ifndef LEXER_HPP
#define LEXER_HPP
// Some include files, not really relevant
namespace 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*/};
当我在main.cpp
中使用这些东西并编译时,我收到以下错误:
/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 status
显然,我的 function 出了点问题,但我终生无法弄清楚到底出了什么问题。
如果您需要,这里有一些其他信息:
lexer.cpp
变成了一个.o
文件。 不过,无论我使用.cpp
文件还是.o
文件都没有区别。main.cpp
: g++ -o main main.cpp lexer.o
lexer.hpp
、 lexer.cpp
、 lexer.o
和main.cpp
都在同一个目录中。做这个
TokenList interpreter::lex::tokenizeString(std::string tokStr)
{
/* Stuff*/
}
或这个
namespace interpreter { namespace lex {
TokenList tokenizeString(std::string tokStr)
{
/* Stuff*/
}
} }
虽然你说过using namespace interpreter::lex;
这并不意味着新定义的名称会被添加到interpreter::lex
命名空间中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.