Advertisement
AntonioVillanueva

go cgo librerias dinamicas en c

May 9th, 2023
1,003
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Go 1.06 KB | None | 0 0
  1. package main
  2.  
  3. /*
  4. // #cgo LDFLAGS: -L. -lmylib
  5. #include "mylib.h"
  6. */
  7. import "C"
  8. import "fmt"
  9.  
  10. func main() {
  11.     // Llamar a la función "my_function" con los parámetros necesarios
  12.     arg1 := C.int(10)
  13.     arg2 := C.int(11)
  14.     ret := C.my_function(arg1, arg2)
  15.  
  16.     fmt.Printf("Resultado: %d\n", int(ret))
  17.  
  18.     //Test 2 char * my_function2(char * arg1,char * arg2){
  19.     arg3 := C.CString("Hola")
  20.     arg4 := C.CString(" Tony")
  21.  
  22.     res := C.my_function2(arg3, arg4)
  23.  
  24.     fmt.Printf("Resultado: %s\n", C.GoString(res))
  25.  
  26. }
  27.  
  28. //mylib.h
  29. /*
  30. #ifndef MYLIB_H
  31. #define MYLIB_H
  32.  
  33. int my_function(int arg1, int arg2);
  34. char * my_function2(char * arg1,char * arg2);
  35.  
  36. #endif
  37. */
  38.  
  39. //mylib.c
  40. /*
  41. #include "mylib.h"
  42. #include <stdlib.h>
  43. #include <string.h>
  44. //compilar la libreria dinamica so asi  gcc -shared -o mylib.so mylib.c
  45.  
  46. int my_function(int arg1, int arg2){
  47.     return arg1+arg2;
  48. }
  49.  
  50. char * my_function2(char * arg1,char * arg2){
  51.     char* resultado = malloc(strlen(arg1) + strlen(arg2) + 1);
  52.     strcpy(resultado, arg1);
  53.     strcat(resultado, arg2);
  54.     return resultado;
  55. }
  56. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement