分页: 1 / 1

[问题]在TC下编写的两个小程序如何移植到ubuntu8.04下?

发表于 : 2008-05-26 16:02
jqwzz
小弟是一菜鸟,在TC下编写了两个C程序,请各位大虾指点一下如何将其在ubuntu8.04下编译通过并运行?
主要可能是库函数的问题:
程序一SUNO1.C:
#include<time.h>
#include<stdlib.h>
#include<math.h>
#include<stdio.h>
main()
{ int i,M=1024,N=20;
double fm=2000000;
double x,y,pi=3.14,a1,a2,a3,w,c,r,t;
c=sqrt(2/(double)M);
w=2*pi*fm;
srand((int)time(0));
for(t=1;t<N+1;t++)
{x=0,y=0;
for(i=1;i<M+1;i++)
{ a3=(2*pi*i-pi+(2*pi*rand()/32767-pi))/(4*M);
a1=2*pi*rand()-pi;
a2=2*pi*rand()-pi;
x=x+c*cos(w*t*cos(a3)+a1);
y=y+c*sin(w*t*sin(a3)+a2);
}
r=sqrt(x*x+y*y)/sqrt(2);
printf("%f\n",r);
}
getchar();
}
在ubuntu下运行 gcc -o SUNO1 SUNO1.C,错误如下:
/tmp/ccAvpfUp.o: In function `main':
SUNO1.C:(.text+0x6c): undefined reference to `sqrt'
SUNO1.C:(.text+0x17a): undefined reference to `cos'
SUNO1.C:(.text+0x18b): undefined reference to `cos'
SUNO1.C:(.text+0x1aa): undefined reference to `sin'
SUNO1.C:(.text+0x1b8): undefined reference to `sin'
SUNO1.C:(.text+0x1ec): undefined reference to `sqrt'
/tmp/ccAvpfUp.o:(.eh_frame+0x11): undefined reference to `__gxx_personality_v0'
collect2: ld 返回 1


程序二GRAPDB.C:
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <graphics.h>
main()
{ int gdriver,gmode,posi;
double Kdb=-4294967295;
double X,Y,G1,G2,C=1,a,R,K,S,A;
int N=600,i;
K=pow(10.0,(Kdb/10));
a=1/(2*(K+1));
srand((int)time(0));
gdriver=DETECT;
initgraph(&gdriver,&gmode,"c:\\turboc2\\bgi");
setbkcolor(BLUE);
cleardevice();
setcolor(10);
for(i=0;i<N;i++)
{do
{X=1-2.0*rand()/32767;
Y=1-2.0*rand()/32767;
S=X*X+Y*Y;
}
while(S>=1);
G1=X*sqrt(-2*C*C*log(S)/S);
G2=Y*sqrt(-2*C*C*log(S)/S);
R=sqrt(a*((G1+sqrt(2*K))*(G1+sqrt(2*K))+G2*G2));
A=20*log10(R);
posi=240-8*A;
line(i,240,i,posi);
}
getchar();
closegraph();
}
在ubuntu下运行 gcc -o GRAPDB GRAPDB.C,错误如下:
GRAPDB.C:5:22: 错误: graphics.h:没有该文件或目录
GRAPDB.C: In function ‘int main()’:
GRAPDB.C:14: 错误: ‘DETECT’在此作用域中尚未声明
GRAPDB.C:15: 错误: ‘initgraph’在此作用域中尚未声明
GRAPDB.C:16: 错误: ‘BLUE’在此作用域中尚未声明
GRAPDB.C:16: 错误: ‘setbkcolor’在此作用域中尚未声明
GRAPDB.C:17: 错误: ‘cleardevice’在此作用域中尚未声明
GRAPDB.C:18: 错误: ‘setcolor’在此作用域中尚未声明
GRAPDB.C:31: 错误: ‘line’在此作用域中尚未声明
GRAPDB.C:34: 错误: ‘closegraph’在此作用域中尚未声明

程序二中有#include <graphics.h> Linux下好像没有这个库,请问该如何解决?
跪谢各位大侠!!

发表于 : 2008-05-26 16:09
dbzhang800
第二个程序你自己都说原因了,偶只说第一个程序。

代码: 全选

gcc -o SUNO1 SUNO1.C
这个命令有问题,
1 .C 是C++ 源文件的后缀, .c 才是C语言源文件的后缀。
2 程序中用了 math.h ,链接时要链接数学库 libm.a

代码: 全选

gcc -o SUNO1 SUNO1.c -lm

发表于 : 2008-05-26 16:39
jqwzz
非常感谢这位大虾的热情回复,谢谢!
链接时要链接数学库 libm.a ,请问如何链接该数学库?使用什么命令?该数学库在什么地方存放?
另外第二个程序因用到include <graphics.h>,我该怎么解决啊?
非常感谢!

发表于 : 2008-05-26 16:42
Strange
2 换用其他图形库。(当然需要改动代码)

发表于 : 2008-05-26 16:47
jqwzz
谢谢楼上这位大哥,请问换什么图形库呢?麻烦赐教!对Linux下的库我不是很清楚!非常感谢!

发表于 : 2008-05-31 23:29
jqwzz
请求高人指点!谢谢啊!

发表于 : 2008-05-31 23:58
weilichun
用SVGA
安装libsvga相关的包
这两个头文件里面有图形函数,你看看吧
vgagl.h
vga.h

代码: 全选

/* VGAlib version 1.2 - (c) 1993 Tommy Frandsen                    */
/*                                                                 */
/* This library is free software; you can redistribute it and/or   */
/* modify it without any restrictions. This library is distributed */
/* in the hope that it will be useful, but without any warranty.   */

/* Extended for svgalib by Harm Hanemaayer and Hartmut Schirmer */

发表于 : 2008-06-02 18:02
jqwzz
libsvga这个安装不上,太老了!郁闷啊!