Go whole division called from C

I can do integer division in go with this program:

package main

import "fmt"

func main() {
 a := 10
 b := 5
    fmt.Println(a/b)
}

Then I made a program in go that has functions for +, -, * and /. and I made a C program that calls each of these functions and performs arithmetic operations. In addition to division, the code works fine.

Go file with functions: (calc.go)

package main

func Add(a, b int) int {
    return a + b
}
func Sub(a, b int) int {
    return a - b
}
func Mul(a, b int) int {
    return a * b
}
func Div(a, b int) int {
    return a / b
}

And the C program that calls these functions: (calcc.c)

#include <stdio.h>

extern int go_add(int, int) __asm__ ("main.Add");
extern int go_sub(int, int) __asm__ ("main.Sub");
extern int go_mul(int, int) __asm__ ("main.Mul");
extern int go_div(int, int) __asm__ ("main.Div");

int menu()
{
  int op;
  printf("\n1 add");
  printf("\n2 sub");
  printf("\n3 mul");
  printf("\n4 div");
  printf("\nEnter your choice : ");
  scanf("%d", &op);
  return op;
}
int main() {


  int op, ch, result, a, b;

  do{ 
    op= menu();

    printf("First number  : ");
    scanf("%d", &a);
    printf("Second number : ");
    scanf("%d", &b);

    switch(op)
    {
       case 1:
        result = go_add(a, b);
    printf("Result : %d" , result);
        break;
       case 2:
        result = go_sub(a, b);
    printf("Result : %d" , result);
        break;
       case 3:
        result = go_mul(a, b);
    printf("Result : %d" , result);
    break;
       case 4:
        result = go_div(a, b);
    printf("Result : %d" , result);
    break;
       default:
        printf("Invalid choice ! ");
    }
    printf("\nAnother operation? (1 if yes) : ");
    scanf("%d", &ch);
  } while(ch==1);
  printf("\nThank you!");
}

I compiled on the terminal using the commands:

gccgo -c calc.go

  • and

gcc calc.o calcc.c -o main

And got this error: undefined reference to `__go_runtime_error 'collect2: error: ld returned 1 exit status

How do I fix this?

+4
source share
2 answers

, gccgo, gcc. gcc , run runtime (libgo).

, . , LD_LIBRARY_PATH. :

gccgo -static-libgo calc.o calcc.o -o main

, gccgo.

+1

, __asm__ gccgo ( ).

Go C "//export name" Go.

, Go ↔ C cgo , C- Go Go, . , Go . goroutines, .. . , Go main C, Go .

, , cgo go build -able, :

calc.go:

package main

// /* could be in a declared in a header file instead */
// extern void pseudo_main(void);
import "C"

//export Add
func Add(a, b int) int {
    return a + b
}

// … etc …

//export Div
func Div(a, b int) int {
    return a / b
}

// Main needs to be Go so that the go runtime
// gets started so you can use goroutines, the
// garbage collector, etc,etc.
//
// It can just be a trivial call into a C main like
// function.
func main() {
    C.pseudo_main()
}

calc.c:

#include <stdio.h>
#include "_cgo_export.h" // file auto-generated by cgo from Go "//export func" comments

// passing argc, argv, envp like arguments
// if desired is left as an excersise :)
void pseudo_main(void) {
    int x, y, z;

    printf("Hello from C\n");
    x = 42;
    y = 6;
    z = Add(x, y);
    printf("%d + %d = %d\n", x, y, z);
    z = Div(x, y);
    printf("%d / %d = %d\n", x, y, z);
}

( Unix):

% go build -o calc
% ./calc

: -o, . -o , , , . : Microsoft Windows . , , cgo, go build -x.

:

Hello from C
42 + 6 = 48
42 / 6 = 7

gist.github.com

: Go: C? ? !

+1

All Articles