I did尝试找到我的具体问题,但我发现的最接近的是这个问题 https://stackoverflow.com/questions/43143833/linker-error-when-including-header-in-files-other-than-main-cpp问题出在全局变量上。
我需要制作一个词法分析器头文件及其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
显然,我的功能出了问题,但我一生都无法弄清楚what实际上是错误的。
如果您需要的话,这里还有一些其他信息:
- 我转动了我的
lexer.cpp
into a .o
文件。无论我使用.cpp
文件或.o
不过,文件没有什么区别。
- 我是如何编译的
main.cpp
: g++ -o main main.cpp lexer.o
-
lexer.hpp
, lexer.cpp
, lexer.o
and main.cpp
都在同一个目录中。
Do this
TokenList interpreter::lex::tokenizeString(std::string tokStr)
{
/* Stuff*/
}
or this
namespace interpreter { namespace lex {
TokenList tokenizeString(std::string tokStr)
{
/* Stuff*/
}
} }
虽然你说过using namespace interpreter::lex;
这并不意味着新定义的名称会添加到interpreter::lex
命名空间。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)