为什么我无法正确地从Golang中读取C常量?

huangapple go评论89阅读模式
英文:

Why can't I read a C constant from Golang properly?

问题

我正在使用go-hdf5将一个hdf5文件读入golang。我在Windows 7上使用最新版本的mingw和hdf5 1.8.14_x86。似乎尝试使用任何预定义类型都不起作用,让我们以T_NATIVE_UINT64为例进行讨论。我已经将问题简化为以下内容,基本上将go-hdf5排除在问题之外,并指出了一些非常基本的问题:

package main

/*
#cgo CFLAGS: -IC:/HDF_Group/HDF5/1.8.14_x86/include
#cgo LDFLAGS: -LC:/HDF_Group/HDF5/1.8.14_x86/bin -lhdf5 -lhdf5_hl
#include "hdf5.h"

#include <stdio.h>

void print_the_value2() { printf("the value of the constant is %d\n", H5T_NATIVE_UINT64); }
*/
import "C"

func main() {
    C.print_the_value2()
}

显然,你需要安装hdf5并将编译器指向头文件和动态链接库,然后运行go get,在我的电脑上执行这段代码会打印出以下内容:

the value of the constant is -1962924545

运行上述代码的不同变体,无论是如何读取常量,都会得到不同的H5T_NATIVE_UINT64值。然而,我非常确定这些都不是正确的值,实际上尝试使用返回的id类型也不起作用,这并不奇怪。

如果我编写并运行一个“真正的”C程序,我会得到不同的结果:

#include <stdio.h>
#include "hdf5.h"

hid_t _go_hdf5_H5T_NATIVE_UINT64() { return H5T_NATIVE_UINT64; }

int main()
{
    printf("the value of the constant is %d", _go_hdf5_H5T_NATIVE_UINT64());
}

使用以下命令进行编译:

gcc -IC:/HDF_Group/HDF5/1.8.14_x86/include -LC:/HDF_Group/HDF5/1.8.14_x86/bin -lhdf5 -lhdf5_hl -o stuff.exe stuff.c

然后运行得到:

the value of the constant is 50331683

这似乎是正确的值,因为我可以直接从我的go程序中使用它。显然,我希望能够使用这些常量。有任何想法为什么会发生这种情况吗?

根据下面的评论提供的额外信息:

我在hdf5头文件中查找了H5T_NATIVE_UINT64的定义,发现以下内容:

c:\HDF_Group\HDF5\1.8.14_x86\include>grep H5T_NATIVE_UINT64 *
H5Tpkg.h:H5_DLLVAR size_t H5T_NATIVE_UINT64_ALIGN_g; 
H5Tpublic.h:#define H5T_NATIVE_UINT64 (H5OPEN H5T_NATIVE_UINT64_g)
H5Tpublic.h:H5_DLLVAR hid_t H5T_NATIVE_UINT64_g;

整个头文件在这里:H5Tpublic.h

谢谢!

英文:

I am using go-hdf5 to read an hdf5 file into golang. I am on windows7 using a pretty recent copy of mingw and hdf5 1.8.14_x86 and it seems like trying to use any of the predefined types doesn't work, let's focus for example on T_NATIVE_UINT64. I have reduced the issue to the following, which basically leaves go-hdf5 out of the problem and points at something quite fundamental going wrong:

package main

/*
 #cgo CFLAGS: -IC:/HDF_Group/HDF5/1.8.14_x86/include
 #cgo LDFLAGS: -LC:/HDF_Group/HDF5/1.8.14_x86/bin -lhdf5 -lhdf5_hl
 #include &quot;hdf5.h&quot;

 #include &lt;stdio.h&gt;

 void print_the_value2() { printf(&quot;the value of the constant is %d\n&quot;, H5T_NATIVE_UINT64); }
*/
import &quot;C&quot;

func main() {
	C.print_the_value2()
}

You obviously need to have hdf5 and point the compiler at the headers/dlls and running go get, then executing prints this on my pc

the value of the constant is -1962924545

Running variations of the above, in how/where the constant is read, will give different answers for the value of H5T_NATIVE_UINT64. However I am pretty sure that is none are the right value and in fact trying to use a type with the id returned doesn't work, unsurprisingly.

If I write and run a "real" C program, I get different results

#include &lt;stdio.h&gt;
#include &quot;hdf5.h&quot;

hid_t _go_hdf5_H5T_NATIVE_UINT64() { return H5T_NATIVE_UINT64; }

int main()
{
	printf(&quot;the value of the constant is %d&quot;, _go_hdf5_H5T_NATIVE_UINT64());
}

Compiling using

C:\Temp&gt;gcc -IC:/HDF_Group/HDF5/1.8.14_x86/include -LC:/HDF_Group/HDF5/1.8.14_x86/bin -lhdf5 -lhdf5_hl -o stuff.exe stuff.c

and running gives me

the value of the constant is 50331683

And that appears to be the right value as I can use it directly from my go program. Obviously I want to be able to use the constants instead. Any idea why this could be happening?

Extra info following comments below:

I looked for the definition of H5T_NATIVE_UINT64 in the hdf5 headers and see the following

c:\HDF_Group\HDF5.8.14_x86\include&gt;grep H5T_NATIVE_UINT64 *
H5Tpkg.h:H5_DLLVAR size_t H5T_NATIVE_UINT64_ALIGN_g; 
H5Tpublic.h:#define H5T_NATIVE_UINT64 (H5OPEN H5T_NATIVE_UINT64_g)
H5Tpublic.h:H5_DLLVAR hid_t H5T_NATIVE_UINT64_g;

The whole header is here

http://www.hdfgroup.org/ftp/HDF5/prev-releases/hdf5-1.8.14/src/unpacked/src/H5Tpublic.h

Thanks!

答案1

得分: 0

H5T_NATIVE_UINT64 不是一个常量,而是一个 #define,最终会被解析为 (H5Open(), H5T_NATIVE_UINT64_g),而 cgo 无法理解这个定义。

可以通过在 gcc 的预处理器上打开调试输出来进行检查:

gcc -E -dM your_test_c_file.c | grep H5T_NATIVE_UINT64

结果为:

#define H5T_NATIVE_UINT64 (H5OPEN H5T_NATIVE_UINT64_g)

现在对 H5OPEN 进行相同的操作:

gcc -E -dM test_go.c | grep '#define H5OPEN'

结果为:

#define H5OPEN H5open(),

目前,cgo 可以理解简单的整数常量定义,比如 #define VALUE 1234,或者任何 gcc 预处理器可以转换为整数常量的内容。请参考 $GOROOT/src/cmd/cgo/gcc.go 中的 func (p *Package) guessKinds(f *File) 函数。

英文:

H5T_NATIVE_UINT64 is NOT a constant but a #define that ultimately evaluates to (H5Open(), H5T_NATIVE_UINT64_g), which cgo does not understand.

It's easy to check by turning on debug output on gcc's preprocessor:

gcc -E -dM your_test_c_file.c | grep H5T_NATIVE_UINT64

Result:

#define H5T_NATIVE_UINT64 (H5OPEN H5T_NATIVE_UINT64_g)

Now the same for H5OPEN:

gcc -E -dM test_go.c | grep &#39;#define H5OPEN&#39;

gives:

#define H5OPEN H5open(),

Right now, cgo does understand simple integer constant defines like #define VALUE 1234, or anything that the gcc preprocessor will turn into an integer constant. See the function func (p *Package) guessKinds(f *File) in $GOROOT/src/cmd/cgo/gcc.go.

huangapple
  • 本文由 发表于 2015年2月11日 05:50:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/28442524.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定