简体   繁体   中英

Variable length arrays not possible w CLion and C99 Standard?

I´ve been happily coding with CLion to create a project for University whilst using C99 standard. As of today variable lengths for array declaration won`t work. Does anyone have any ideas why? Code:

int main() {
    // to allow debugging with CLION
    setbuf(stdout, 0);
    int number = 5;
    int myarray[number];
    return 0;
}

CMakeLists.txt

project(PG1 C)

set(CMAKE_C_STANDARD 99)

add_executable(PG1 main.c ...)

Error is:

C:\...\PG1\main.c(10): error C2057: Constant value required
C:\...\PG1\main.c(10): error C2466: Declaration of array with constant size 0 not possible
C:\...\PG1\main.c(10): error C2133: "myarray": unknown size
NMAKE : fatal error U1077: "C:\PROGRA~2\MICROS~2\2019\BUILDT~1\VC\Tools\MSVC\1427~1.291\bin\Hostx86\x86\cl.exe": Return-Code "0x2"
Stop.
NMAKE : fatal error U1077: ""C:\Program Files (x86)\Microsoft Visual Studio\2019\BuildTools\VC\Tools\MSVC\14.27.29110\bin\HostX86\x86\nmake.exe"": Return-Code "0x2"
Stop.
NMAKE : fatal error U1077: ""C:\Program Files (x86)\Microsoft Visual Studio\2019\BuildTools\VC\Tools\MSVC\14.27.29110\bin\HostX86\x86\nmake.exe"": Return-Code "0x2"
Stop.
NMAKE : fatal error U1077: ""C:\Program Files (x86)\Microsoft Visual Studio\2019\BuildTools\VC\Tools\MSVC\14.27.29110\bin\HostX86\x86\nmake.exe"": Return-Code "0x2"
Stop.

CLion is using MS Visual Studio 2019 as the underlying compiler. MSVC is not a fully compliant C compiler, and in particular it does not support variable length arrays.

You would have to use gcc or clang to get support for VLAs.

As of today variable lengths for array declaration won`t work. Does anyone have any ideas why?

OP's compiler is not C99 compliant (which first specified Variable-length_array - VLA support), not compliant with later versions that optionally support VLA, nor does the compiler have that enabled as an extension.

is there any way how i can verify the compiler version?

Code could test various macros to see if VLAs are supported by the version of the compiler.

#if defined __STDC__ && defined __STDC_VERSION__ && (__STDC_VERSION__ == 199901)
  #define VLA_SUPPORTED 1
#elif defined __STDC__ && defined __STDC_VERSION__  && (__STDC_VERSION__ >= 201112)  && \
    (__STDC_NO_VLA__ != 1)
  #define VLA_SUPPORTED 1
#else 
  #define VLA_SUPPORTED 0
#endif

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM