北城已荒凉 2018-10-29 02:03 采纳率: 0%
浏览 667

与 c 代码相比,为什么 c + + 代码不需要"# define POSIX c source 200809L"?

When I use "POSIX interval timer" or do signal handling, I have to insert

#define _POSIX_C_SOURCE 200809L

on line 1 inside any of my files. But I figured out that only C code needs it, but not in C++ code.

How does the g++ compiler work differently from the gcc compiler on this issue?


below is my system environment

 user@~ $ g++ --version
 g++ (Ubuntu 5.4.0-6ubuntu1~16.04.10) 5.4.0 20160609
 Copyright (C) 2015 Free Software Foundation, Inc.
 This is free software; see the source for copying conditions.
 There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

and I build the project adding this line in CMakeLists.txt

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -g -std=c++14 -Wall")

转载于:https://stackoverflow.com/questions/53037824/in-contrast-to-c-code-why-c-code-doesnt-need-define-posix-c-source-200809

  • 写回答

2条回答 默认 最新

  • larry*wei 2018-10-29 02:27
    关注

    Let's do a simple experiment. Try to compile this short source file:

    auto f()
    {
        return _POSIX_C_SOURCE;
    }
    

    Nope. It doesn't compile since _POSIX_C_SOURCE is not defined.

    But what if we include a C++ header?

    #include <iostream>
    
    auto f()
    {
        return _POSIX_C_SOURCE;
    }
    

    It compiles well.

    (Try it live: https://godbolt.org/z/C6_a6P)

    That means it has nothing to do with g++. Somewhere in some of the C++ standard library header files, this macro is defined.

    评论

报告相同问题?

悬赏问题

  • ¥30 win from 窗口最大最小化,控件放大缩小,闪烁问题
  • ¥20 易康econgnition精度验证
  • ¥15 msix packaging tool打包问题
  • ¥28 微信小程序开发页面布局没问题,真机调试的时候页面布局就乱了
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致