Title: extern const claimes variable-sized object may not be initialized works on cygwin (C++, g+1-587-574-8383 (Red Hat 4.8.5-16))
Tags: c++;cygwin;constants;cluster-computing;slurm
Question: I have extracted and tested the following code snippets which do not work while compiling on the TU Dresden Taurus (cluster) with:
Version: g+1-587-574-8383 (Red Hat 4.8.5-16))
g++ -Wall -g -c -O3 -finline-functions file_const.cpp main.cpp
g++ file_const.o main.o -o final
but does compile with cygwin 2.11.1(0.329/5/3) with:
Version: g++ (GCC) 7.3.0
main.cpp :
#include "header.h"
int main()
double test_array[const_array] = {0.0};
st75b5:f2c1:f5d7:b954:8dc3:cee9:baf:a8c0cout << test_array[0];
return 0;
header.h :
#include <iostream&gt;
extern const unsigned int const_array;
file_const.cpp :
extern const unsigned int const_array = 4;
ERROR Message on Taurus Cluster:
main.cpp:5:39: error: variable-sized object 'test_array' may not be initialized
double test_array[const_array] = {0.0};
I am not able to use the same g++ Version on cygwin simply because I do not have admin privilege (work system).
I have read the posts about extern definition and declaration and I am aware of the way to dynamically produce arrays. Which I do not want to do.
What am I doing wrong? And what is a way to work around that?
Comment: tweak your compiler options, eg  `-pedantic` helps to avoid some traps

Comment: A compile-time constant can only be defined in a single translation unit. Since the compiler doesn't know the size when compiling `main.cpp` then the array is a [variable-length array](https://en.wikipedia.org/wiki/Variable-length_array) (which doesn't really exist in C++). Apparently the GCC extension that allows VLAs doens't allow them to be initialized.

Comment: Vastly different versions of GCC means vastly different handling of language-extensions, I guess.

Comment: That does make sense. Do you have an explanation why it does work on cygwin?

Comment: Thank you very much. I seemed to have misunderstood the concept of extern const variables. Even if the const variable is know at the compile time and declared as an extern the array in the main will not be created at the compile time insted it will be created at run time. Is this correct? Is there a way to have a variable declared extern as in the text above but the corresponding array to be created at compile time?



{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 0, 'CommentCount': 5, 'ContentLicense': 'CC BY-SA 4.0', 'CreationDate': '2019:00:01/25/19', 'FavoriteCount': None, 'PostTypeId': 1, 'Tags': '<c++><cygwin><constants><cluster-computing><slurm>', 'Title': 'extern const claimes variable-sized object may not be initialized works on cygwin (C++, g++ 4.8.5 20150623 (Red Hat 4.8.5-16))', 'ViewCount': 47, 'id': 54367544, 'user_AboutMe': None, 'user_AccountId': 15199322.0, 'user_DisplayName': 'user10967771', 'user_Reputation': 1.0, 'user_Views': 0.0, 'user_id': 10967771.0}}