Failed to start call C from go
I am trying to run a C call from go language code. Here is the program I'm running:
package main
// #include<proxy.h>
import "C"
import "fmt"
func main(){
fmt.Println(C.CMD_SET_ROUTE)
}
Here is the content of the proxy.h file:
#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 get when trying to run this 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 am on version 1.4.2. Can you please help me figure out what exactly is going on here? TIA.
+3
source to share
1 answer
Four things:
- You must use double quotes when including
proxy.h
as it is in the same directory as your file.go
. - There cannot be a blank line before the "C" and "C" comments.
- You are missing
#endif
at the endproxy.h
. - You need to define
CMD_DEFINE
before enablingproxy.h
. Otherwise, Go cannot access the static variable.
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
+6
source to share