问题描述
包主
我可以在这个程序中执行整数除法导入fmt
func main(){
a:= 10
b:= 5
fmt.Println(a / b)
}
然后我制作了一个程序,它具有+, - ,*和/的功能。
,我用C语言编写了一个程序,调用这些函数并执行算术运算。
除了部门,代码工作正常。
具有函数的go文件是:(calc.go)
package main
$ b func Add(a,b int)int {
return a + b
}
func Sub(a,b int)int {
返回a - b
}
func Mul(a,b int)int {
返回a * b
}
func Div(a,b int )int {
return a / b
}
而C程序调用这些函数是:(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(\\\
输入您的选择:);
scanf(%d,& 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(结果:%d,结果);
休息;
案例2:
result = go_sub(a,b);
printf(结果:%d,结果);
休息;
案例3:
result = go_mul(a,b);
printf(结果:%d,结果);
休息;
案例4:
result = go_div(a,b);
printf(结果:%d,结果);
休息;
默认值:
printf(Invalid choice!);
}
printf(\ n另一个操作?(如果是):);
scanf(%d,& ch);
} while(ch == 1);
printf(\\\
谢谢!);
}
我使用以下命令在终端上编译:
- 和
得到这个错误:
未定义引用`__go_runtime_error'
collect2:错误:ld返回1退出状态
我该如何解决这个问题? gccgo ,而不是正常的 gcc
。正常的gcc并不知道它应该与运行时环境(libgo)链接。
根据您的配置,您可能还需要指定运行时库的位置被发现。例如,通过静态嵌入或通过使其在 LD_LIBRARY_PATH
环境变量中可用。
示例:
gccgo -static-libgo calc.o calcc.o -o main
code>
有关更多信息,请查看。
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("\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 :
- and
And got this error : undefined reference to `__go_runtime_error'collect2: error: ld returned 1 exit status
How should I fix this?
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.
这篇关于Go中的整数除法从C中调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!