"Initializer element is not constant" error for no reason in Linux GCC, compiling C -


i take main.c file , compile gcc -std=c1x -c main.c in mac os x, , works fine no errors. exact same thing in linuxmint , on raspberry pi, , in both cases, gives me errors "initializer element not constant".

one example of problematic line relevant code:

//static global constants const unsigned long long latitude = (long) 3600000; const unsigned long long longitude = (long) 1810000; const unsigned long long max_coordinates_number = (latitude-1) + latitude*(longitude-1); //compiler error: initializer element not constant 

it's supposed let me arithmetic, right? replace actual numbers, , work, become messy. , works fine on mac anyway. there option in gcc have specify on linux (besides -std=c1x, don't need on mac)?

the c language requires initializer static object constant expression. (since initialization of static objects occurs before main begins, there's no place run-time evaluation happen.)

c's const keyword not mean "constant", though words related. constant expression 1 can be, , in cases must be, evaluated @ compile time. const means read-only. example, @ block scope (inside function definition), this:

const int r = rand(); 

is legal. initializer can't evaluated @ compile time; const merely means r may not modified after it's been initalized.

when write:

const unsigned long long latitude = (long) 3600000; 

a reference latitude not constant expression. compiler could evaluate such reference @ compile time, c standard doesn't require to. (the line between constant , non-constant expressions had drawn somewhere, , authors of language chose make distinction relatively simple, few special cases.)

now it's true c language could have been defined latitude constant expression. in c++, , i've argued c adopt similar rule. under current c rules, it's not, means can't use latitude in initializer static object.

this means clang (the compiler that, understand it, 1 invoked when type gcc under macos) non-conforming, because fails diagnose error. on own linux system, find that, when invoked -std=c11 -pedantic, gcc 4.7.2 correctly diagnoses error, clang 3.4 not.

except perhaps clause section 6.6 paragraph 10 of 2011 iso c standard (which exists in 1990 , 1999 standards):

an implementation may accept other forms of constant expressions.

it's conceivable clang accepts latitude constant expression because takes advantage of permission -- i'd still expect @ least warning clang -std=c11 -pedantic -wall -wextra, , there none.

update : when compile following:

#include <stdio.h>  const unsigned long long latitude = (long) 3600000;  int main(void) {     switch (0) {         case latitude:             puts("wrong");             break;         default:             puts("ok(?)");             break;     } } 

with clang 3.0 options -std=c99 -pedantic, get:

c.c:7:14: warning: expression not integer constant expression (but allowed extension) [-pedantic]         case latitude:              ^~~~~~~~ 1 warning generated. 

with clang 3.4, warning is:

c.c:7:14: warning: expression not integer constant expression; folding constant gnu extension [-wgnu-folding-constant]         case latitude:              ^~~~~~~~ 1 warning generated. 

so clang recognize it's not constant expression; bug doesn't warn declaration of max_coordinates_number.


Comments

Popular posts from this blog

python - Subclassed QStyledItemDelegate ignores Stylesheet -

java - HttpClient 3.1 Connection pooling vs HttpClient 4.3.2 -

SQL: Divide the sum of values in one table with the count of rows in another -