简体   繁体   English

可变长度 arrays 不可能使用 CLion 和 C99 标准?

[英]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.在使用 C99 标准的同时,我一直很高兴地使用 CLion 编写代码来为大学创建一个项目。 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 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. CLion 使用 MS Visual Studio 2019 作为底层编译器。 MSVC is not a fully compliant C compiler, and in particular it does not support variable length arrays. MSVC 不是完全兼容 C 的编译器,特别是它不支持可变长度 arrays。

You would have to use gcc or clang to get support for VLAs.您必须使用 gcc 或 clang 才能获得对 VLA 的支持。

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. OP 的编译器不符合 C99(它首先指定了 Variable-length_array - VLA支持),不符合可选支持 VLA 的更高版本,编译器也没有将其作为扩展启用。

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.代码可以测试各种宏以查看编译器版本是否支持 VLA。

#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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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