我得到了已经存在的 C 代码,它使用了 C99 风格的 VLA。就像这个:
int foo(int n, double l[n][n], double a[n][n]);
我想在我的 C++11 项目中包含 header 。由于 C++ 不允许此类构造,因此我使用 extern "C"
来包含这些头文件。
然而编译器根本不喜欢这样。
./header.h:23:42: error: use of parameter outside function body before ‘]’ token
void foo(int n, double l[n][n], double x[n], double b[n]);
^
./header.h:23:45: error: use of parameter outside function body before ‘]’ token
void foo(int n, double l[n][n], double x[n], double b[n]);
^
./header.h:23:46: error: expected ‘)’ before ‘,’ token
void foo(int n, double l[n][n], double x[n], double b[n]);
^
./header.h:23:48: error: expected unqualified-id before ‘double’
void foo(int n, double l[n][n], double x[n], double b[n]);
^~~~~~
我想我在某处读到 VLA 在 C11 中成为可选的。这是否意味着 gcc 完全摆脱了它?如果是这样,除了 extern "C"
之外我还能做什么?当然,我可以使用较旧的 C 标准编译源代码。但我必须以某种方式包含标题。有什么想法吗?
重写整个事情只是最后的手段。
最佳答案
I'd like to include the headers in my C++11 project. As C++ doesn't allow these kind of constructs I'm using extern "C" to include these header files. However the compiler doesn't like this at all.
确实,这是个问题,因为 C++11 没有 VLA。您可能需要考虑用 C 编写包装器,将其编译为 C 并在 C++ 中使用。
I think I read somewhere that VLAs became optional in C11. Does this mean that gcc got rid of it completely?
不,gcc 在将代码编译为 C11 时仍然支持 VLA。但是,这与您的问题无关,您的问题是关于 C++11 的,这是一种完全不同的语言,不支持 VLA。
单独使用what can I do other than extern "C"?
extern "C"
对您没有帮助,因为正如我所解释的,C++11 没有 VLA。考虑一下在 C 语言中,数组是一个连续 序列,这意味着它是所有一个分配 并且所有元素一个接一个地出现。因此,这样的二维数组被表示为一个由 n * n
元素组成的一维数组。您的包装器也使用 extern "C"
,应该将指向该展平数组第一个元素的 double *
转换为 double (*)[n]
指向该展平数组的前 n
个元素。
那是 C11 中的类型转换,不需要暴露给 C++,因为采用 double *
的函数,该函数被编译为 C11 代码(不 C++11) 并使用链接器链接到 C++11 项目。 C++11 不需要查看函数下的 C11 代码。
因此您需要重写 header (您可以将其作为 .hpp
文件独立于 .h
文件进行维护),幸好这不是完成重写。至少希望您已经了解到,C++ 并不是 C 的严格超集。
关于c++ - 在 C++11 环境中使用 VLA,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38085440/