编译一个程序在DOS模式下运行

发布于 2024-08-17 12:27:23 字数 1716 浏览 7 评论 0原文

我写了一个简单的程序,在DOS模式下运行。一切都可以在 Win XP / Vista / 7 的模拟控制台下运行,但不能在 DOS 中运行。错误提示:该程序无法在 DOS 模式下运行。我想知道是编译器标志的问题还是更大的问题。

对于编程,我使用 Code::Blocks v 8.02 进行编译设置:

-Wall -W -pedantic -pedantic-errors

in Project \ Build options \ Compiler settings

我尝试过干净的 DOS 模式,从 cd 启动,还设置在虚拟机中启动DOS。出现同样的错误。

我应该打开更多编译器标志吗?一些特定的 386 / 486 优化?

更新

好的,我已经下载、安装并配置了 DJGPP。甚至解决了库和包含的一些问题。还有两个问题。

1) 我无法编译调用 _strdate_strtime 的代码,我已经仔细检查了包含内容,正如 MSDN 所说的那样time.h,但仍然错误提示:_strdate 未在此范围内声明,我什至尝试添加 std::_strdate,但随后我有 4 个错误,而不是 2 个错误相同的

2)第二个代码是关于gotoxy,它看起来像这样:

#include <windows.h>

void gotoxy(int x, int y)
{
  COORD position;
  position.X = x; position.Y = y;
  SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), position);
}

错误说没有windows.h,所以我已经把它放在适当的位置,但是然后还有更多错误说 windows.h 中缺少某些内容,我认为它不会工作,因为这个函数严格适用于 Windows,对吗?有没有办法为DOS编写类似的gotoxy

UPDATE2

1) 使用 time(); 解决,而不是 _strdate();_strtime() 代码

time_t rawtime;
struct tm * timeinfo;
char buffer [20];

time ( &rawtime );
timeinfo = localtime ( &rawtime );

strftime (buffer,80,"%Y.%m.%d %H:%M:%S\0",timeinfo);
string myTime(buffer);

; 这是现在在 DJGPP 下编译的

UPDATE3

仍然需要使用 gotoxy 解决代码 - 将其替换为其他可编译的代码(在 DJGPP 下)。

谢谢大家的帮助。刚刚学习了一些关于编译的新知识(标志、旧 IDE,如 DJGPP、OpenWatcom)并刷新了设置 DOS 工作的记忆:--)

I write a simple program, to run in DOS mode. Everything works under emulated console in Win XP / Vista / Seven, but not in DOS. The error says: this program caonnot be run in DOS mode. I wonder is that a problem with compiler flags or something bigger.

For programming i use Code::Blocks v 8.02 with such settings for compilation:

-Wall -W -pedantic -pedantic-errors

in Project \ Build options \ Compiler settings

I've tried a clean DOS mode, booting from cd, and also setting up DOS in Virtual Machine. The same error appears.

Should i turn on some more compiler flags ? Some specific 386 / 486 optimizations ?

UPDATE

Ok, i've downloaded, installed and configured DJGPP. Even resolved some problems with libs and includes. Still have two questions.

1) i can't compile a code, which calls _strdate and _strtime, i've double checked the includes, as MSDN says it needs time.h, but still error says: _strdate was not declared in this scope, i even tried to add std::_strdate, but then i have 4, not 2 errors sazing the same

2) the 2nd code is about gotoxy, it looks like that:

#include <windows.h>

void gotoxy(int x, int y)
{
  COORD position;
  position.X = x; position.Y = y;
  SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), position);
}

error says there is no windows.h, so i've put it in place, but then there are many more errors saying some is missing from windows.h, I SUPPOSE it won't work because this functions is strictly for windows right ? is there any way to write similar gotoxy for DOS ?

UPDATE2

1) solved using time(); instead of _strdate(); and _strtime(); here's the code

time_t rawtime;
struct tm * timeinfo;
char buffer [20];

time ( &rawtime );
timeinfo = localtime ( &rawtime );

strftime (buffer,80,"%Y.%m.%d %H:%M:%S\0",timeinfo);
string myTime(buffer);

It now compiles under DJGPP.

UPDATE3

Still need to solve a code using gotoxy - replaced it with some other code that compiles (under DJGPP).

Thank You all for help. Just learnt some new things about compiling (flags, old IDE's like DJGPP, OpenWatcom) and refreshed memories setting DOS to work :--)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

梨涡 2024-08-24 12:27:23

从声音来看,您当前正在编译一个 Windows 控制台程序。尽管它是一个控制台程序,但它仍然需要 Windows 才能运行。

要编译一个程序在真正的 DOS 上运行,您需要找到一个(可能非常旧的)编译器和(特别是)以 DOS 为目标并可以生成 DOS MZ 可执行文件的链接器。最后一个针对 MS-DOS 的 Microsoft 编译器是 VC++ 1.52c。如果没记错的话,Borland 后来继续以 DOS 为目标,直到 Broland C++ 5 左右。

我相信如果你检查 Digital Mars 网站,他可能仍然有一个针对 DOS 的 C++ 编译器。否则,您将不得不寻找用过的且相当旧的东西。

编辑:查看其他答案让我想起了 DJGPP 和 OpenWatcom。我很抱歉之前没有提及它们。

请注意,从 C++ 的角度来看,Borland 和 Microsoft 都是非常古老的编译器——它们根本不支持命名空间,并且模板支持各不相同,从 Microsoft 编译器中不存在到 Borland 中的平庸。 DJGPP 基本上是一个 DOS 扩展器,gcc 已移植到该扩展器上;它过时(或现代)的程度取决于所涉及的 gcc 版本。如果我没记错的话,Digital Mars 编译器比 Borland 编译器更现代,但 Walter Bright 现在将大部分时间花在 D 而不是 C++ 上,因此 C++ 编译器并没有真正与 gcc 或 MSVC 竞争,更不用说像 Comeau 或 Intel 这样基于 EDG 前端的东西了。

From the sound of things, you're currently compiling a Windows console program. Even though it's a console program, it still needs Windows to run.

To compile a program to run on real DOS, you'll need to find a (probably really old) compiler and (especially) linker that targets DOS and can produce DOS MZ executables. The last Microsoft compiler to target MS-DOS was VC++ 1.52c. If memory serves, Borland continued to target DOS somewhat later, up through something like Broland C++ 5 or so.

I believe if you check the Digital Mars web site, he may still have a C++ compiler available that targets DOS. Otherwise, you're going to be stuck looking for something used and quite old.

Edit: looking at other answers reminded me of DJGPP and OpenWatcom. My apologies for not mentioning them previously.

Be aware that from a C++ viewpoint, Borland and Microsoft are really old compilers -- they don't do namespaces at all, and template support varies from nonexistent in the Microsoft compiler to mediocre in Borland's. DJGPP is basically a DOS extender to which gcc has been ported; the degree to which it's out of date (or modern) will depend on which version of gcc is involved. The Digital Mars compiler is somewhat more modern than the Borland one if I'm not mistaken, but Walter Bright now spends most of his time working on D instead of C++, so the C++ compiler doesn't really compete with gcc, or MSVC, not to mention something like Comeau or Intel that's based on the EDG front-end.

黑色毁心梦 2024-08-24 12:27:23

您所说的“模拟控制台”与模拟或 DOS 无关。您可能仍在生成 32/64 位 Windows 可执行文件,只是使用控制台子系统。

首先将编译器更改为能够生成 16 位代码的编译器。我非常确定 OpenWatcom 仍然支持开箱即用的 16 位 MZ 目标。 IIRC DJGPP 也有,但我不确定也不知道它是否仍在维护。

编辑:关于 gotoxy,您可以:

  1. 使用 PDCurses 并且不关心什么是内部使用(不过,我认为它是 INT10)
  2. 在您的设备上使用 interrupt 0x10自己
  3. 直接写入0xB8000处的VGA内存

我从未真正为DOS开发过,所以我不知道哪种方法被认为是最好的。不过,第三似乎是最快的。

What you're referring to as "emulated console" has nothing to do with emulation or DOS. You probably are still generating 32/64-bit Windows executables, just using console subsystem.

Start by changing your compiler to one capable of generating 16-bit code. I'm pretty sure that OpenWatcom still supports 16-bit MZ target out of box. IIRC DJGPP too, but I'm not sure and don't know if it's still maintained.

Edit: about gotoxy, you could:

  1. Use PDCurses and don't care what is uses internally (I think it's INT10, though)
  2. Use interrupt 0x10 on your own
  3. Write directly to the VGA memory at 0xB8000

I've never actually developed for DOS, so I don't know which method would be considered the best. Third seems to be the fastest one, though.

只想待在家 2024-08-24 12:27:23

看一下这里: ​​MinGW 为 MS DOS 编译

基本上,使用 DJCPP 作为后端到代码::块。

Have a glance here: MinGW compile for MS DOS

Basically, use DJCPP as the back end to Code::Blocks.

浮云落日 2024-08-24 12:27:23

我习惯使用 DJGPP 在普通的 ms-dos 下编译东西.. 主页

它是一个完全免费的编译器套件,仍然应该无缝工作!

I was used to use DJGPP to compile things under plain ms-dos.. home page.

It's a full free compiler suite that should still work seamlessly!

时光礼记 2024-08-24 12:27:23

您可以使用 DJGPP 附带的 conio.h 标头中的 gotoxy

You can use gotoxy from conio.h header that comes with DJGPP.

就像说晚安 2024-08-24 12:27:23

使用 conio.h 库中的 gotoxy 。 (dos 友好 ;) )

#include <conio.h>
#include <stdio.h>

void main()
{
    gotoxy(5,5);
    printf("Printing at (5,5)");
}

简单

同时查看 Borland 的文档

use the gotoxy from the conio.h library. (dos friendly ;) )

#include <conio.h>
#include <stdio.h>

void main()
{
    gotoxy(5,5);
    printf("Printing at (5,5)");
}

Simple

Also check Borland's documentation

别低头,皇冠会掉 2024-08-24 12:27:23

你想告诉你的编译器以 dos 为目标。我不知道该怎么做,抱歉,但这也许可以作为谷歌搜索的提示...

查看 Code::Blocks 网站,您的 IDE 似乎可以支持各种编译器(GCC、MSVC++ 等)。因此,首先,弄清楚您正在使用哪个编译器。然后检查编译器文档。

一旦你知道你正在使用什么编译器,那么你就可以重新表述(完善)你的问题 - 我相信 stackoverflow 上的有人会知道。

根据 此线程,似乎 Open Watcom ]2 编译器目标为 dos。此外,您的 IDE (Code::Blocks) 也支持它。

祝你好运!

You want to tell your compiler to target dos. I don't know how to do that, sorry, but maybe this will serve as a hint for googling...

Looking at the Code::Blocks website, it seems your IDE can support various compilers (GCC, MSVC++ and others). So first, figure out which compiler you are using. Then check that compilers documentation.

Once you know what compiler you are using, then you can rephrase (refine) your question - I'm sure someone here on stackoverflow will know.

According to this thread, it seems the Open Watcom]2 compiler targets dos. Also, it is supported by your IDE (Code::Blocks).

Good luck!

眼眸里的快感 2024-08-24 12:27:23

win32 exe 中有一个 dos 存根标头,在纯 dos 上运行时会显示此消息。您应该使用 dos 编译器(例如 Turbo c)对其进行编译,或者如果代码块中包含 DOS,则提供目标平台。这是软件问题而不是硬件平台。

There is a dos stub header in win32 exe that display this message when ran on pure dos. You should compile it with a dos compiler e.g turbo c or provide target plateform if DOS is among it in case code blocks. Its a software issue not hardware platform.

盛夏已如深秋| 2024-08-24 12:27:23

Linux 或 Windows 上托管的 ia16-elf 具有 GCC 6.2 来链接 .COM 文件。 (它在 Mac 上托管,使用 Winery 和 MinGW 作为 bash shell 并提取 Windows 存档)。

使用 DosBox(或 Mac 上的 Boxer)、QEMU、VirtualBox 进行测试,而不是重新启动您关心的真实物理机。

ia16-elf hosted on Linux or Windows has GCC 6.2 to link .COM files. (It hosts on Mac using Winery and MinGW for a bash shell and extracting the windows archive).

Use DosBox (or Boxer on Mac), QEMU, VirtualBox for testing, not rebooting a real physical machine that you care about.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文