11

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 :--)

4
  • Keep in mind that if you build it as a DOS executable, it won't run on 64-bit Windows, since it'll be a 16-bit executable and 64-bit Windows doesn't include the 16-bit NTVDM system which is used to run the DOS executables. Commented Jan 14, 2010 at 20:12
  • It doesn't matter, i just need to run it in strict DOS environment. Commented Jan 14, 2010 at 21:20
  • trying removing every line of #include <windows.h> and being sure that DJGPP is compiling 16 bit code and non-PE .exe files. Also, I'd suggest something more like SDCC for 8086 programming because DJGPP sucks really bad.
    – Earlz
    Commented Jan 14, 2010 at 22:38
  • 1
    Can You elaborate a little on "DJGPP sucks really bad" ? Commented Jan 15, 2010 at 8:26

9 Answers 9

9

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.

7
  • DJGPP, with its GCC 4.3 port, will do modern C++ just fine. Commented Jan 14, 2010 at 21:01
  • When used in C++ programs, the standard C header files now make their contents visible in namespace std as well as in the global namespace - cited from DJGPP website. Does it mean it support's namespaces ? Commented Jan 14, 2010 at 21:02
  • why not SDCC? It supports the 8086. Course, it's also not C++, but who needs that!? :)
    – Earlz
    Commented Jan 14, 2010 at 22:41
  • 1
    @earlz:"who needs that?" From the tags applied, the original poster does. Commented Jan 14, 2010 at 22:49
  • I don't see anything about C++ in his actual post though.
    – Earlz
    Commented Jan 14, 2010 at 23:28
3

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.

6
  • DOS is 16-bit, so you'd need to use memory segmentation to address 0xB8000, which (as far as I know) requires dropping back to ASM
    – user47322
    Commented Jan 16, 2010 at 12:28
  • Third is pretty straightforward and definitely faster. A fourth option: IIRC the old Borland compilers had their own pretty good Curses-style UI libraries (which write directly to B800:0000 internally).
    – Porculus
    Commented Jan 16, 2010 at 12:33
  • @Charlie: Again IIRC, you can do memory segmentation in C/C++ by casting 32-bit integers to far pointers.
    – Porculus
    Commented Jan 16, 2010 at 12:37
  • Ahh, can you? Did not know this. Can you link to a pastebin showing that? It would definitely help me :)
    – user47322
    Commented Jan 16, 2010 at 12:39
  • I'm no expert on Borland's stack but the "pretty good Curses-style UI libraries" by Borland mentioned by @Porculus is probably Turbo Vision (a text-mode GUI toolkit), which was open-sourced and has been ported to DJGPP. Downloads here and instructions here.
    – ssokolow
    Commented Oct 16, 2017 at 22:58
2

Have a glance here: MinGW compile for MS DOS

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

1

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!

1

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

1

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

0

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!

1
  • Interesting, considering the fact that there's so much open source OS, the licensed and maybe unsupported DOS is still being used :)
    – Krassi Em
    Commented Oct 4, 2017 at 10:19
0

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.

0

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.

Not the answer you're looking for? Browse other questions tagged or ask your own question.