Go cgo ldap_init could not determine kind of name for C.ldap_init -


i trying learn , understand cgo. writing program in go connect ad , parse output. tested code in c, working fine expected. relevant part in c is

char *ldap_host = "x.x.x.x"; int   ldap_port     = 389; ldap = ldap_init(ldap_host, ldap_port))  

now trying same working in go as

//#cgo cflags: -lldap  //#include <ldap.h> import "c" func main() {     var hostname *string     hostname = &os.args[1]     ldap_port := 389     ldap := c.ldap_init(*hostname, ldap_port) } 

but getting following error

could not determine kind of name c.ldap_init 

what doing wrong here?

i start getting basics of go , cgo working before adding individual peculiarities of library ldap. starting points, official cgo doc page.

starting top:

you don't need cflags here, need ldflags linker, , liblber run.

#cgo ldflags: -lldap -llber 

you can't pass pointer go string c *char, different types. strings in go aren't addressable @ all, compile error if build process got far. use c.cstring, , c.free if need create c strings. need include stdlib.h c.free.

    url := c.cstring(os.args[1])     defer c.free(unsafe.pointer(url)) 

go's int size varies architecture, , not c's int type. ldap_port in example converted default type of int, have needed c.int.

finally, original error in question has nothing go or cgo. ldap_init function deprecated, , not exist without setting ldap_deprecated. should use ldap_initialize function. here minimal example compiles:

package main  import (     "log"     "unsafe" )  /* #include <stdlib.h> #include <ldap.h>  #cgo ldflags: -lldap -llber */ import "c"  func main() {     url := c.cstring("ldap://127.0.0.1:389/")     defer c.free(unsafe.pointer(url))      var ldap *c.ldap      rv := c.ldap_initialize(&ldap, url)     if rv != 0 {         log.fatalf("ldap_initialize() error %d", rv)     }     log.println("initialized") } 

Comments

Popular posts from this blog

javascript - jQuery: Add class depending on URL in the best way -

caching - How to check if a url path exists in the service worker cache -

Redirect to a HTTPS version using .htaccess -