gocgogccgo

Not able to run C call from go


I am trying to run a C call from go language code. Here is the program I am running:

package main

// #include<proxy.h>

import "C"
import "fmt"

func main(){
    fmt.Println(C.CMD_SET_ROUTE)
}

Here is the content of the file proxy.h:

#ifndef PROXY_H
#define PROXY_H

#include <netinet/in.h>

#ifdef CMD_DEFINE
#   define cmdexport
#else
#   define cmdexport static
#endif

cmdexport const int CMD_SET_ROUTE = 1;
cmdexport const int CMD_DEL_ROUTE = 2;
cmdexport const int CMD_STOP      = 3;

Now, here is the error I am getting when trying to run that program:

pensu@ubuntu:~$ go run test.go 
# command-line-arguments
could not determine kind of name for C.CMD_SET_ROUTE

I am using gccgo-5 and go version 1.4.2. Could you please help me figure out what exactly is the issue here? TIA.


Solution

  • Four things:

    Below is the corrected code:

    package main
    
    // #define CMD_DEFINE
    // #include "proxy.h"
    import "C"
    import "fmt"
    
    func main(){
        fmt.Println(C.CMD_SET_ROUTE)
    }
    
    #ifndef PROXY_H
    #define PROXY_H
    
    #include <netinet/in.h>
    
    #ifdef CMD_DEFINE
    #   define cmdexport
    #else
    #   define cmdexport static
    #endif
    
    cmdexport const int CMD_SET_ROUTE = 1;
    cmdexport const int CMD_DEL_ROUTE = 2;
    cmdexport const int CMD_STOP      = 3;
    
    #endif