Help! Can't get cgo to work

1,638 views
Skip to first unread message

john...@gmail.com

unread,
Jul 19, 2013, 4:37:27 PM7/19/13
to golan...@googlegroups.com
I'm trying to build an interface for the Swiss Ephemeris package. So far, I've got the original compiled to both a .a and a .so, and I've moved the two header files I need into the same directory as the .go file. I've also stuck the .a (but not the .so) into pkg/darwin_amd64.

Here's the source:

// swephint is the interface to the Swiss Ephemeris: see the Swiss Ephemeris documentation
// for copyright information.

package swephint

/*

#cgo LDFLAGS: -llibswe

#import "swephexp.h"

*/
import "C"
import "unsafe"

func SetEphPath(path string) {
    cpath := C.CString(path)
    defer C.free(unsafe.Pointer(cpath))
        C.swe_set_ephe_path(cpath)
}

This is an almost direct copy from the cgo article.

What's happening is:

jhrothjr:src johnroth$ go install swephint
# swephint
ld: library not found for -llibswe
collect2: ld returned 1 exit status

I've already tried using -L for the directory where the .a and .so were originally compiled to. It also doesn't seem to make any difference whether I say -llibswe or -llibswe.a

As you might guess, I'm a relative novice in unix-style development tools.

Any ideas? Thanks in advance

Thanks.

Raffaele Sena

unread,
Jul 19, 2013, 6:45:04 PM7/19/13
to golan...@googlegroups.com
I would try to run cgo directly with some debug flags enabled to see what commands is running:

$ go tool cgo
usage: cgo -- [compiler options] file.go ...
  -cdefs=false: for bootstrap: write C definitions for C file to standard output
  -debug-define=false: print relevant #defines
  -debug-gcc=false: print gcc invocations
  -dynimport="": if non-empty, print dynamic import data for that file
  -dynlinker=false: record dynamic linker information in dynimport mode
  -dynout="": write -dynobj output to this file
  -gccgo=false: generate files for use with gccgo
  -gccgopkgpath="": -fgo-pkgpath option used with gccgo
  -gccgoprefix="": -fgo-prefix option used with gccgo
  -godefs=false: for bootstrap: write Go definitions for C file to standard output
  -import_runtime_cgo=true: import runtime/cgo in generated code
  -import_syscall=true: import syscall in generated code
  -objdir="": object directory

john...@gmail.com

unread,
Jul 19, 2013, 9:16:29 PM7/19/13
to golan...@googlegroups.com

Thanks for the suggestion. After a lot of spelunking in the docs, I finally got it to work. The problem was the #cgo declaration. It should have been:

#cgo LDFLAGS: -L(path to library) -lswe

It totally did not occur to me that ld would prefix what I wrote with "lib". That's the kind of "help" that definitely evil.

Thanks again. 

__kaveh__

unread,
Nov 15, 2014, 9:27:22 PM11/15/14
to golan...@googlegroups.com
I can not get it to work (Windows 7 x64, MinGW x86, Go x86; LiteIDE); I get this error:

.\swejpl.c:78:19: error: conflicting types for 'off_t'
   typedef __int64 off_t;
                   ^
In file included from c:\mingw\include\io.h:20:0,
                 from .\sweodef.h:86,
                 from .\swephexp.h:83,
                 from .\swejpl.c:71:
c:\mingw\include\sys\types.h:55:16: note: previous declaration of 'off_t' was here
 typedef _off_t off_t;
                ^


(as I understand I do not need to call `go tool cgo...` explicitly; it would be called during `go build/go install` based on header comments; right?)

Lars Seipel

unread,
Nov 15, 2014, 10:10:55 PM11/15/14
to __kaveh__, golan...@googlegroups.com
On Sat, Nov 15, 2014 at 06:27:22PM -0800, __kaveh__ wrote:
> I can not get it to work (Windows 7 x64, MinGW x86, Go x86; LiteIDE); I get
> this error:
>
> .\swejpl.c:78:19: error: conflicting types for 'off_t'
> typedef __int64 off_t;

This doesn't look like a cgo issue but more like an issue with your C
code. It's correct that you don't need to call go tool cgo manually. Go
install will handle it. You need to give it valid C code, though.

Don't bother with cgo until you can compile your C code using the
C compiler.

As the compiler says, your code defines off_t multiple times using
conflicting types. One of those comes from a standard header
(sys/types.h) shipped with MinGW. Remove the typedef from your code or
make the definitions agree (is there something like _FILE_OFFSET_BITS=64
on windows?).

Is this swejpl.c file the one found here:
http://www.astro.com/ftp/swisseph/src/swejpl.c

It has the incriminated text on line 78, shoved behind an "#if MSDOS".
Find out if it belongs there and what is the appropriate thing to do for
your system.

john...@gmail.com

unread,
Nov 16, 2014, 9:07:17 PM11/16/14
to golan...@googlegroups.com


On Saturday, November 15, 2014 7:27:22 PM UTC-7, __kaveh__ wrote:
I can not get it to work (Windows 7 x64, MinGW x86, Go x86; LiteIDE); I get this error:

.\swejpl.c:78:19: error: conflicting types for 'off_t'
   typedef __int64 off_t;
                   ^
In file included from c:\mingw\include\io.h:20:0,
                 from .\sweodef.h:86,
                 from .\swephexp.h:83,
                 from .\swejpl.c:71:
c:\mingw\include\sys\types.h:55:16: note: previous declaration of 'off_t' was here
 typedef _off_t off_t;
                ^


(as I understand I do not need to call `go tool cgo...` explicitly; it would be called during `go build/go install` based on header comments; right?)

The problem appears to be in sweodef.h, where it's attempting to do autodetection of the compiler and system. It doesn't seem to understand mingw, which I think is required to use cgo on Windows. (At least I think that's the case, there's a lot about cgo that's still baffling me.)

If that's the issue, we need to take it back to the swephem mailing list.

HTH

 

__kaveh__

unread,
Nov 17, 2014, 2:08:38 AM11/17/14
to golan...@googlegroups.com
Thanks John; as Lars put it correctly it's a problem with gcc not cgo. But since I am no C expert; I wished that this error could be something that happens with cgo - which apparently is not the case. I am trying to get gcc thing done first.

hhz...@gmail.com

unread,
Nov 18, 2016, 8:21:04 AM11/18/16
to golang-nuts
I ran into the same problem.  in one of my go file, I have:

//#cgo CFLAGS: -I/usr/include/libnl3
/*
#include <netlink/netlink.h>



However, when I try to build, I got the following error: ( I have libnl-3-dev, libnl-genl-3-dev installed and the netlink/netlink.h file is at /usr/include/libnl3/ directory. netlink.h file does include some other header files like
 #include <sys/somefile.h>
#include <Linux/some.h>
#include <netlink/other.h>    --> The same directory as "netlink/netlink.h" file


+ go install -gcflags '' -ldflags ' -X k8s.io/kubernetes/pkg/version.buildDate=2016-11-18T06:14:04Z -X k8s.io/kubernetes/pkg/version.gitCommit=a26530bfce8126450e4ad0faab222046ce0953f3 -X k8s.io/kubernetes/pkg/version.gitTreeState=dirty -X k8s.io/kubernetes/pkg/version.gitVersion=v1.1.0-alpha.1.16011+a26530bfce8126-dirty -X k8s.io/kubernetes/pkg/version.gitMajor=1 -X k8s.io/kubernetes/pkg/version.gitMinor=1+' k8s.io/kubernetes/cmd/kubelet k8s.io/kubernetes/cmd/kubemark k8s.io/kubernetes/cmd/hyperkube k8s.io/kubernetes/cmd/gendocs k8s.io/kubernetes/cmd/genkubedocs k8s.io/kubernetes/cmd/genman k8s.io/kubernetes/cmd/genyaml k8s.io/kubernetes/cmd/mungedocs k8s.io/kubernetes/cmd/genswaggertypedocs k8s.io/kubernetes/cmd/linkcheck k8s.io/kubernetes/examples/k8petstore/web-server/src k8s.io/kubernetes/federation/cmd/genfeddocs k8s.io/kubernetes/vendor/github.com/onsi/ginkgo/ginkgo
# k8s.io/kubernetes/vendor/github.com/google/seesaw/netlink
vendor/github.com/google/seesaw/netlink/cfuncs.go:19:29: fatal error: netlink/netlink.h: No such file or directory
 #include <netlink/netlink.h>
                             ^
compilation terminated.
Reply all
Reply to author
Forward
0 new messages