I did originally answer something different based on how I thought cgo works. But if CONSTANT2
is recognized by cgo, something different might be the reason. Could you please:
run the tool
nm
on the library file and tell whether the output containsCONSTANT2
or any other constants you refer to that were at the same time established through#define
. A library might declare a#define
constant at the same time as a global symbol to get around compatibility issues.provide a minimal working example of your problem if possible. This is, an example that can be compiled by someone who reads your post and exhibits your problem. Your question looks like it may miss some important parts to answer it. For instance, it would be nice to know the actual library you have problems with.
original answer
If you use #define
, you do not establish anything the compiler will actually see. A #define
is a preprocessing directive that is removed before parsing. To establish constants the compiler (and thus cgo) can see, actually declare them:
const char *CONSTANT1 = "";
const char *CONSTANT2 = "";
const char *CONSTANT3 = (char*)0;
const char *CONSTANT4 = (char*)0;
If you cannot touch the header, there is typically not much you can do; you basically have to duplicate all constants in the Go part of your code:
const (
CONSTANT1 = "",
CONSTANT2 = "",
CONSTANT3 = nil,
CONSTANT4 = nil,
)
You could try complex tricks like running cpp over your Go code, but this is likely to cause more trouble than it would solve problems.