[英]g++ fails with “undefined reference” errors to the standard C++ library
[英]Why g++ reported an error in C++ Standard Library in MacOS GDK?
我在构建代码时遇到了一个奇怪的错误:
在我向您展示错误是什么之前,我想向您展示代码在做什么。 这是我的小 cmd 项目的节点 class,每个节点都是一个文件/文件夹。 我写了一个 function 来显示节点的信息。
// Node.h
#include <string>
#include <memory>
using namespace std;
typedef bool NODE_TYPE;
#define FILE true
#define DIR false
class Node
{
private:
string _node_name;
NODE_TYPE _node_type;
string _create_date;
string _create_user;
weak_ptr<Node> _parent;
shared_ptr<Node> _sibling;
shared_ptr<Node> _child;
public:
/*Constructors, Destructors and other functions*/
const string toString();
};
// Node.cpp
#include "Node.h"
#include <string>
#include <sstream>
using namespace std;
const string Node::toString()
{
ostringstream buffer;
buffer << ... // the data member of Node
return buffer.str();
}
我在main.cpp
中写了一个测试,当我说g++ Node.cpp main.cpp -Wall -std=c++11
时,错误如下:
In file included from Node.cpp:6:
In file included from /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/c++/v1/sstream:184:
In file included from /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/c++/v1/istream:163:
In file included from /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/c++/v1/ostream:138:
In file included from /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/c++/v1/ios:214:
In file included from /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/c++/v1/__locale:40:
In file included from /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/xlocale.h:93:
/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/xlocale/_stdio.h:32:33: error: expected expression
int fprintf_l(FILE * __restrict, locale_t __restrict, const char * __restrict, ...)
像这样的错误还有21个,我猜SDK有问题,所以我重新安装了它,但仍然没有工作。 有人可以帮我吗?
我要感谢我的问题下方的所有评论,它们真的很有帮助。
在 C 标准库中, FILE
被定义为存储程序打开的文件信息的结构。 所以我自己的宏定义导致错误的宏扩展。
在这里,我想参考 Scott Meyers 的建议:对于简单的常量,更喜欢const
对象或enums
而#define
s 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.