【问题标题】:-std=c++ 98 and OS X 10.10-std=c++ 98 和 OS X 10.10
【发布时间】:2015-01-19 23:13:53
【问题描述】:

我目前正在尝试在 OS X 10.10 上使用 -std=c++98 标志编译我的程序:

clang++ -std=c++98 -pedantic -W -Wall -Werror *.cpp

g++ -std=c++98 -pedantic -W -Wall -Werror *.cpp

奇怪的是,当我使用 OS 10.10 编译时,没有显示错误,而使用 GNU/Linux 发行版则显示了一些错误。

使用 GNU/Linux 发行版时,我有一些错误,因为我使用 s.open(file); 而不是 s.open(file.c_str());,但在 OS X 上没有错误,即使使用 s.open(file);

也许这个错误和每个操作系统的文件系统之间只有一个联系?

【问题讨论】:

  • “一些”错误是什么意思?可以举个例子吗?

标签: c++ macos g++ osx-yosemite clang++


【解决方案1】:

你是对的。这段代码不应该在 C++98 下编译:

#include <fstream>
#include <string>

int main() {
    std::string file("/tmp/foo.txt");
    std::ifstream s;
    s.open(file);
}

从技术上讲,这是 libc++ 中的一个错误,但我怀疑他们是否会想要修复它。

如果你愿意,你可以用 libstdc++ 作为标准库编译,它没有实现这个功能(至少苹果分发的版本没有)。

[10:13am][wlynch@watermelon /tmp] clang++ -std=c++98 -stdlib=libstdc++ foo.cc
foo.cc:7:12: error: no viable conversion from 'std::string' (aka 'basic_string<char>') to 'const char *'
    s.open(file);
           ^~~~
/usr/include/c++/4.2.1/fstream:518:24: note: passing argument to parameter '__s' here
      open(const char* __s, ios_base::openmode __mode = ios_base::in)
                       ^
1 error generated.

【讨论】:

  • 完美。所以我只需要添加-stdlib=libstdc++ 标志就可以产生与GNU/Linux 相同的错误。
  • 请注意,如果您尝试链接 Apple 提供的库,则在您的程序中使用 libstdc++ 可能会出现问题。见:stackoverflow.com/questions/19634803/…
  • “这在技术上是 libc++ 的一个 bug” 这不是 libc++ 的一个 bug,只是 libc++ 没有实现 c++98。
【解决方案2】:

问题在于,在 OS X 上,您仍在使用标准库的 C++11 实现,其中包括诸如接受 std::stringsostream::open 方法等 API 更改。

使用-std=c++98 更改 C++ 标准不会影响正在使用的标准库,并且 libc++ 不实现 C++98(例如,在 C++98 模式下没有#ifdef 来删除那些open(std::string) API ) 而我认为 libstdc++ 在 C++98 模式下构建时确实隐藏了非 C++98 API。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-03-16
    • 1970-01-01
    • 2015-03-22
    • 2015-11-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多