Last active
October 26, 2024 04:18
-
-
Save tejainece/8b243b56f9f1dadfc501 to your computer and use it in GitHub Desktop.
Examples of calling C code from Golang
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
//#include<stdio.h> | |
//void inC() { | |
// printf("I am in C code now!\n"); | |
//} | |
import "C" | |
import "fmt" | |
func main() { | |
fmt.Println("I am in Go code now!") | |
C.inC() | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include<stdio.h> | |
void inCFile() { | |
printf("I am in C code in a .c file now!\n"); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
/* | |
extern void inCFile(); | |
*/ | |
import "C" | |
import "fmt" | |
func main() { | |
fmt.Println("I am in Go code now!") | |
C.inCFile() | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include<stdio.h> | |
#include "_cgo_export.h" | |
void inCFile() { | |
printf("I am in C code in a .c file now!\n"); | |
callFromC(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
/* | |
#include<stdio.h> | |
extern void inCFile(); | |
*/ | |
import "C" | |
import "fmt" | |
func main() { | |
fmt.Println("I am in Go code now!") | |
C.inCFile() | |
} | |
//export callFromC | |
func callFromC() { | |
fmt.Println("I am in Go code but I was called from C!") | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include<stdio.h> | |
#include "_cgo_export.h" | |
char * inCFile(char *str) { | |
char *ret = "C String"; | |
printf("Received string from Go: %s\n", str); | |
return ret; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
/* | |
#include<stdio.h> | |
#include <stdlib.h> | |
extern char * inCFile(char *str); | |
*/ | |
import "C" | |
import ( | |
"fmt" | |
"unsafe" | |
) | |
func main() { | |
cstr := C.CString("Go string!") | |
defer C.free(unsafe.Pointer(cstr)) | |
cString := C.inCFile(cstr) | |
gostr := C.GoString(cString) | |
fmt.Println("Received string from C: " + gostr) | |
} |
Yep, helped me, too.
Neat cheatsheet for rapid start
Thanks!
Do you have one for c++ ?
thanks this is very helpful
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Nicely done, thanks for posting this.