Axe
Axe

Reputation: 6335

Programming in VC++ to run outside Windows

I had the following question about programming in VC++ 2008

  1. If I make a Win32 Console Program in Windows 7 will it work in any Windows below 7?
  2. Can I make a C++ Application independent of the .NET Framework? If yes, please brief me on how to do it.
  3. Can I make applications in VC++ to be run on other operating systems like Linux provided it can run .exe files? If yes, please brief me on how to do it.

I am learning VC++ and just wanted to get the about doubts cleared. Hope they will be cleared soon!

Thanks in advance and Cheers

Upvotes: 1

Views: 255

Answers (5)

selbie
selbie

Reputation: 104474

For the most part... but bear in mind this:

If you want to program without the .NET framework, choose any of the "Visual C++" projects when creating a new project - but don't use any of the CLR projects.

Native C++ apps (console or windowed) will run on older Windows operating systems so long as you don't link in function calls or APIs that are only available in newer operating systems. For example, don't call StgMakeUniqueName and expect your app to work on Windows XP. But it is rather easy to get a console C/C++ application compiling on the latest version of Visual C++ running on Win7 to produce EXEs that run on XP.

I believe you can prevent accidental usage of using new APIs by making use of the _WINNT_WIN32 define.

The other consideration with native C++ applications on Windows is that by default, Visual C++ will choose dynamic linking for including the C-RunTime (e.g. msvcrt100.dll). These DLLs aren't always available on the machines you copy your EXE to. When you run your EXE on a different computer, you might get an application error about not being able to find MSVCRT*.dll. If this happens, you can either switch to static linking in the project settings (and then just copy the EXE directly to the other machines), or pre-install the appropriate MSVCRT on the other computers you want to run your EXE on. Visual Studio pre-installs the MSVCRT as part of its own setup. And Visual Studio ships with MSI files for installing MSVCRT that you can redistribute. Debug builds of your EXE may require some manual copying of the debug build of MSVCRT DLLs to target machines. Choose static linking for debug builds or just have debug builds of your app link with retail MSVCRT.

The same consideration holds true for MFC and it's associated DLLs if you choose to use that framework for UI Development. (But you said console apps, so you don't have to worry about this).

Your EXE won't run on Linux without some hacking. (Recompiling is easier). But if you program your console application in .NET, the EXE has a high chance of running on Linux with Mono.

Upvotes: 3

harper
harper

Reputation: 13690

Regarding your question 2: When you build a C++ application you will still have to make sure that your target system has all the run-time libraries for the applicaiton. The Visual Studios (and even the VS services packs) come with different version of that run-time libaries. These are installed in the side-by-side cache WinSxS.

When you deploy your application you should add a VCRedist.exe for the run-time libraries you used when building you application.

Upvotes: 1

Grim
Grim

Reputation: 987

  1. It will probably run fine unless you are using WinAPI which has been introduced in later releases of Windows. Generally, if its important for you pay attention to MSDN, it contains a section called "Minimum operating systems".
  2. Already answered by @AlessandroV, generally just turn if off in project settings. I think its Off by default (for Win32 Console Application, that is)
  3. EXEs can be natively executed only on Windows, on linux there is software which allows EXEs to execute (Wine), however the performance is worse than running a natively compiled project for the specific OS. In order to have the best performance, and do things right the project should be compiled with that os's native compiler. This will succeed provided you use functions which are not OS specific (not WinAPI). If you will be using libc you should be fine. There are several open source libraries available which make writing cross-platform code easier, boost is one of them.

Good luck.

Upvotes: 2

dbrank0
dbrank0

Reputation: 9466

3: If you write portable C++ console application (not using Windows specific functions), you can simply recompile for other platform using some compiler targeting that platform.

Upvotes: 0

Loghorn
Loghorn

Reputation: 2807

1> Depends: if don't use any Windows 7 specific API, you will have no problems running the program on Windows Vista; if you don't use any Vista specifica API you will have no problem running it on XP, etc.

2> Sure, you just need to make a native only project instead of a C++/CLI one

3> You will need an emulator like "wine" to run windows executables under linux

Upvotes: 3

Related Questions