【发布时间】:2020-08-21 04:08:37
【问题描述】:
我有一个链接到共享库的程序。这个库包含一个 RandomFile.cxx 文件,它的数组定义如下:
static double randomArray[] = {0.1, 0.2, 0.3};
在 RandomFile.cxx 的头文件 RandomFile.hxx 中,没有任何关于 randomArray 的 extern、getter 或任何东西。
在我的程序中,我想以某种方式访问这个数组。
到目前为止我已经尝试过:
// sizeOfRandomArray was calculated by counting the elements.
int sizeOfRandomArray = 3;
// 1st attempt: does not compile because of undefined reference to the array
extern double randomArray[sizeOfRandomArray];
// 2nd attempt: does not compile because of undefined reference to the array
extern "C" double randomArray[sizeOfRandomArray];
// 3rd attempt: does not compile because of undefined reference to the array
extern "C++" double randomArray[sizeOfRandomArray];
// 4th attempt: compiles but i don't get the actual values
extern "C" {
double randomArray[sizeOfRandomArray];
}
// 5th attempt: compiles but i don't get the actual values
extern "C++" {
double randomArray[sizeOfRandomArray];
}
// 6th attempt: compiles and works but I overload my code with the whole RandomFile.cxx file.
#include "RandomFile.cxx"
我不能(不想)更改 RandomFile.cxx,因为它是名为 VTK 的大型库的一部分。
有什么方法可以做到,不包括 cxx 文件或在我的代码中复制数组?
提前致谢。
【问题讨论】:
-
在代码中包含 cxx 是个坏主意,不要这样做...
-
共享库必须有函数。他们有没有访问并返回这个数组?
-
有些人访问它(在 cxx 中)但没有人返回它。