修复 ms 编译器 c 代码以在 gcc 中编译
fix ms compiler c code to compile in gcc
你好,我在 15 年前读了一本 c 和一本 c++ 书大约一半后,对 c/c++ 语言有了一些非常基本的了解。
大约在那个时候,在 1998 年 9 月左右,我买了一本书 "Black Art of 3D Game Programming, writing your own high-speed 3d polygon video games in c"。出版商 - 韦特,作者 - 安德烈拉莫特。
它对自己的定位是,您不必将 c 语言作为学习本书的先决条件。当时真的很想学,但是又卷入了其他的事情,被其他的项目分心了。大约在那个时候,我发现了其他几种语言。我尝试过 perl,并且非常喜欢它。我很快就学会了它的基础知识,并在 3 个月 + 3 个月内用 perl 编写了我的第一个大项目,修复和微调它。从那以后,我开始学习更多并提高 perl,所以真的没有时间学习 c.
学习游戏和图形编程的愿望从未离开过我,所以我决定 return 那本游戏编程书。我知道,你现在可以学习 OpenGL 或 WebGL,但是在我看来,那本书有很多低级概念,如果你不学习,你就不会像游戏编程那样擅长,但是,本书要求您拥有 MS C/C++ 7.0 编译器。从那以后,我搬到了 linux(5 年多前)并且不想回到 windows。另外,无论我学到什么,我都希望它是跨平台的,所以我宁愿弄清楚如何修改本书的代码以在 gcc 中编译,然后在 wine 下安装 MS C/C++ 7.0 编译器,或者在windows 虚拟机。
书中:
//input driven event loops
//includes
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
void main(void){
int done=0,
number,
num_tries=0,
guess;
//removed far from the unsigned line, because compiler complains:
//error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘*’ token
// unsigned int far *clock = (unsigned int far *)0x0000046CL; //clock pointer
unsigned int *clock = (unsigned int *)0x0000046CL; //clock pointer
//section 1
printf("\nI'm thinking of a number from 1-100.");
printf("\nTry and guess it!\n");
srand(*clock);
number = 1 + rand() % 100;
printf("test");
}
书上的代码,同上,只是注释掉的未签名行是原来的,未注释的是修改过的。更改无符号行后,它使用 gcc file_name.c 进行编译,但是当编译后的二进制文件执行到 srand 行时,程序会因段错误而退出。我想,"far" 这件事与 ms 编译器有关,也许整行都与获取时钟指针有关。关于如何修复它有什么建议吗?
已更新:
好的,所以我知道时钟指针线在现代编程中没有用,我应该改用 time.h,所以我将时钟指针更改为 time() 函数。另外,我已经按照建议将 return 和 int 添加到主函数中。这是新代码:
//input driven event loops
//includes
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>
int main(void){
int done=0,
number,
num_tries=0,
guess;
//section 1
printf("\nI'm thinking of a number from 1-100.");
printf("\nTry and guess it!\n");
srand(time(NULL));
number = 1 + rand() % 100;
return(0);
}
它现在工作正常,没有分段错误。谢谢。
far
是一个非标准的 C 关键字。除非您确定需要它,否则请忽略它。
从代码来看,这是一个简单的猜数游戏,clock
应该是为随机生成器提供种子。 0x0000046CL
又是一些导致分段错误的不可移植代码。
为了简单使用,请改用当前时间作为种子。
srand(time(0))
好吧,这是从时钟获取时间的不推荐方法。
使用
srand(time(NULL)); //#include <time.h>
它会给你当前时间,
我猜你的代码有一些安全问题
你好,我在 15 年前读了一本 c 和一本 c++ 书大约一半后,对 c/c++ 语言有了一些非常基本的了解。
大约在那个时候,在 1998 年 9 月左右,我买了一本书 "Black Art of 3D Game Programming, writing your own high-speed 3d polygon video games in c"。出版商 - 韦特,作者 - 安德烈拉莫特。 它对自己的定位是,您不必将 c 语言作为学习本书的先决条件。当时真的很想学,但是又卷入了其他的事情,被其他的项目分心了。大约在那个时候,我发现了其他几种语言。我尝试过 perl,并且非常喜欢它。我很快就学会了它的基础知识,并在 3 个月 + 3 个月内用 perl 编写了我的第一个大项目,修复和微调它。从那以后,我开始学习更多并提高 perl,所以真的没有时间学习 c.
学习游戏和图形编程的愿望从未离开过我,所以我决定 return 那本游戏编程书。我知道,你现在可以学习 OpenGL 或 WebGL,但是在我看来,那本书有很多低级概念,如果你不学习,你就不会像游戏编程那样擅长,但是,本书要求您拥有 MS C/C++ 7.0 编译器。从那以后,我搬到了 linux(5 年多前)并且不想回到 windows。另外,无论我学到什么,我都希望它是跨平台的,所以我宁愿弄清楚如何修改本书的代码以在 gcc 中编译,然后在 wine 下安装 MS C/C++ 7.0 编译器,或者在windows 虚拟机。
书中:
//input driven event loops
//includes
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
void main(void){
int done=0,
number,
num_tries=0,
guess;
//removed far from the unsigned line, because compiler complains:
//error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘*’ token
// unsigned int far *clock = (unsigned int far *)0x0000046CL; //clock pointer
unsigned int *clock = (unsigned int *)0x0000046CL; //clock pointer
//section 1
printf("\nI'm thinking of a number from 1-100.");
printf("\nTry and guess it!\n");
srand(*clock);
number = 1 + rand() % 100;
printf("test");
}
书上的代码,同上,只是注释掉的未签名行是原来的,未注释的是修改过的。更改无符号行后,它使用 gcc file_name.c 进行编译,但是当编译后的二进制文件执行到 srand 行时,程序会因段错误而退出。我想,"far" 这件事与 ms 编译器有关,也许整行都与获取时钟指针有关。关于如何修复它有什么建议吗?
已更新:
好的,所以我知道时钟指针线在现代编程中没有用,我应该改用 time.h,所以我将时钟指针更改为 time() 函数。另外,我已经按照建议将 return 和 int 添加到主函数中。这是新代码:
//input driven event loops
//includes
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>
int main(void){
int done=0,
number,
num_tries=0,
guess;
//section 1
printf("\nI'm thinking of a number from 1-100.");
printf("\nTry and guess it!\n");
srand(time(NULL));
number = 1 + rand() % 100;
return(0);
}
它现在工作正常,没有分段错误。谢谢。
far
是一个非标准的 C 关键字。除非您确定需要它,否则请忽略它。
从代码来看,这是一个简单的猜数游戏,clock
应该是为随机生成器提供种子。 0x0000046CL
又是一些导致分段错误的不可移植代码。
为了简单使用,请改用当前时间作为种子。
srand(time(0))
好吧,这是从时钟获取时间的不推荐方法。
使用
srand(time(NULL)); //#include <time.h>
它会给你当前时间, 我猜你的代码有一些安全问题