I'm using cgo to wrap a C library and have run into a strange set of linker errors. I've boiled the problem down to the following:
A file header.h contains
#ifndef HEADER_H
#define HEADER_H
#define CONSTANT1 ("")
#define CONSTANT2 ""
#define CONSTANT3 ((char*)0)
#define CONSTANT4 (char*)0
#endif /* HEADER_H */
And test.go contains
package main
/*
#include "header.h"
*/
import "C"
func main() {
_ = C.CONSTANT1
_ = C.CONSTANT2
_ = C.CONSTANT3
_ = C.CONSTANT4
}
Upon running go run test.go
I get the following error:
# command-line-arguments
... _cgo_main.o:(.data.rel+0x0): undefined reference to `CONSTANT4'
... _cgo_main.o:(.data.rel+0x8): undefined reference to `CONSTANT3'
... _cgo_main.o:(.data.rel+0x10): undefined reference to `CONSTANT1'
collect2: ld returned 1 exit status
I have two questions about this:
- Why does the linker have anything to do with pre-defined constants?
- Why do
CONSTANT1
,CONSTANT3
,CONSTANT4
show up as undefined, but notCONSTANT2
?
Thanks in advance.
*Edit: Constants defined as other values (e.g. ints) work fine.
*Edit2: Using go version go1.1.2 linux/amd64
*Edit3: A complete example of failure:
I'm working with the C OpenLDAP library and would like to use the LDAP_SASL_SIMPLE
constant. It's defined in ldap.h
as
#define LDAP_SASL_SIMPLE ((char*)0)
#define LDAP_SASL_NULL ("")
The LDAP_SASL_NULL
constant gives the same error.
A minimal demonstrative go program:
package main
/*
#cgo LDFLAGS: -lldap
#include <ldap.h>
*/
import "C"
func main() {
_ = C.LDAP_SASL_SIMPLE
}