duancilan5124 2015-03-20 06:11
浏览 43
已采纳

从C调用Go中的整数除法

I am able to perform integer division in go by 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 program in C that calls each of these functions and performs arithmetic operations. Except division, the code works fine.

The go file with the functions is : (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 is : (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("
1 add");
  printf("
2 sub");
  printf("
3 mul");
  printf("
4 div");
  printf("
Enter 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("
Another operation? (1 if yes) : ");
    scanf("%d", &ch);
  } while(ch==1);
  printf("
Thank 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 should I fix this?

  • 写回答

2条回答 默认 最新

  • dqq46733 2015-03-20 08:34
    关注

    You need to link using gccgo and not with normal gcc. Normal gcc doesn't know that it ought to link against the go runtime (libgo).

    Depending on your configuration, you might also need to specify where the runtime library can be found. For example by embedding it statically or by making it available in the LD_LIBRARY_PATH environment variable. Example:

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

    For more information, check Setting up and using gccgo.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 用hfss做微带贴片阵列天线的时候分析设置有问题
  • ¥50 我撰写的python爬虫爬不了 要爬的网址有反爬机制
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥120 计算机网络的新校区组网设计
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 海浪数据 南海地区海况数据,波浪数据
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等