Ticket #131 (closed defect: fixed)

Opened 10 years ago

Last modified 10 years ago

math_constants.h:37:1: warning: "M_PI" redefined

Reported by: baker Owned by: kevin
Priority: trivial Milestone:
Component: ALL modules Version:
Keywords: gcc, math.h, M_PI Cc:


gcc version 4.2.1 (Mac OS X 10.5.8) issues warnings like:

In file included from butterworth.c:46:
/Users/baker/Desktop/Software/Earthworm/earthworm-7.5svn/include/math_constants.h:37:1: warning: "M_PI" redefined
In file included from /usr/include/math.h:28,
                 from butterworth.c:44:
/usr/include/architecture/i386/math.h:533:1: warning: this is the location of the previous definition

This is because M_PI is already defined by math.h when it is redefined (to a different value) by include/math_constants.h.

To eliminate the warnings, conditionally define M_PI in include/math_constants.h:

/* Constants rounded for 21 decimals. */
#ifndef M_PI
#define M_PI        3.14159265358979323846

Additional information.

M_PI is not a part of the ISO C standard, but is defined by default by gcc in math.h. From the GNU C Library - Mathematics web page (http://ftp.gnu.org/old-gnu/Manuals/glibc-2.2.3/html_chapter/libc_19.html):

These constants come from the Unix98 standard and were also available in 4.4BSD; therefore they are only defined if
_BSD_SOURCE or _XOPEN_SOURCE=500, or a more general feature select macro, is defined. The default set of features
includes these constants. See section Feature Test Macros.

Microsoft Visual C does not define these constants by default, but will do so when _USE_MATH_DEFINES is defined. (See http://msdn.microsoft.com/en-us/library/4hwaceh6.aspx). To obtain the math.h definition of M_PI, one would have to edit every source file that include's math.h to define _USE_MATH_DEFINES before including math.h:

#define _USE_MATH_DEFINES // for C
#include <math.h>

or add -D_USE_MATH_DEFINES to the compiler command-line options. The latter is probably easiest, and can be selectively applied when compiling with Visual C.

Change History

comment:1 Changed 10 years ago by kevin

  • Owner changed from somebody to kevin
  • Status changed from new to assigned

comment:2 Changed 10 years ago by kevin

  • Status changed from assigned to closed
  • Resolution set to fixed
Note: See TracTickets for help on using tickets.