Search code examples
gocgo

Unable to include required C header files for go program


I am trying to include a header file which exists in /usr/local/WordNet-3.0/include/ in my go program

using these flags

// #cgo CFLAGS: -I/usr/local/WordNet-3.0/include
// #cgo LDFLAGS: /usr/local/WordNet-3.0/lib/libWN.3.dylib

/*
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
 #include "wn.h"

static void printlicense() {
    printf("WordNet License %s\n\n%s", dblicense, license);
}
*/
import "C"
import "unsafe"
import (
        "os"
)

but when I run my program using go run, it gives me following error:

"fatal error: 'wn.h' file not found." I am on go 1.5.1.

Any help on what I am doing wrong would be appreciated.

EDIT : I have got this to work by copying the file over in my working directory, but I would still like to know, what I was doing wrong.


Solution

  • Did a quick test on my local : you need to remove the blank line between your cgo flags and your C code.

    Try this :

    // #cgo CFLAGS: -I/usr/local/WordNet-3.0/include
    // #cgo LDFLAGS: /usr/local/WordNet-3.0/lib/libWN.3.dylib
    /*
     #include <stdio.h>
     #include <stdlib.h>
     #include <string.h>
     #include "wn.h"
    
    static void printlicense() {
        printf("WordNet License %s\n\n%s", dblicense, license);
    }
    */
    import "C"
    import "unsafe"
    import (
            "os"
    )