3

I've built a programme in C with Visual Studio using standard C libraries + a couple of windows libraries.

The code just acquires user input (with scanf, so through cmd window) does some calculations based on the input and outputs some text files, that's about it.

I'm wondering what would I then need to do to run my exe on another standard Windows computer without it needing to install any additional files e.g. the whole Windows SDK.?

Is it just a case of being able to build the release version (as opposed to the debug)?

Many thanks

SamuraiMelon
  • 297
  • 3
  • 11
  • 2
    If the underneath architecture is the same as the one you used to build the exe, then in principle you can execute it in other machines with same type of OS without issues. – CRM Apr 15 '20 at 13:06
  • 3
    Typically the matching *Visual Studio Redistributable Runtime* needs to be installed. – Some programmer dude Apr 15 '20 at 13:06
  • 2
    _+ a couple of windows libraries_ Are those static or dynamic libs? If dynamic, they need to go on the remote computer. – 001 Apr 15 '20 at 13:12
  • So I'm thinking I also need to link the libraries to my exe too but I'm not sure of how to do that. At the moment, all that is in my code is the #include header files. I've only recently started C programming and this is my first time needing to be able to give someone an executable they can run. – SamuraiMelon Apr 15 '20 at 13:26
  • @SamuraiMelon: what is your program supposed to do? Who are you supposed to send it to? Why cannot you send or share (e.g. on [github](https://github.com/)) your C source code? – Basile Starynkevitch Apr 15 '20 at 13:39
  • @BasileStarynkevitch I've edited for clarification its function. I cannot send just the source code as they don't know how to programme in C, or have a compiler for it. – SamuraiMelon Apr 15 '20 at 14:08
  • Are you sure you are legally allowed to send them your executable program? You probably don't want to be sued by Microsoft.... Why can't you use WebAssembly, as suggested in my answer? If you did, your program would run *locally* on their browser or mobile phone. Is your program so CPU hungry that it cannot run in a browser? – Basile Starynkevitch Apr 15 '20 at 14:28
  • **Please explain what your program is doing** and give some [MRE]. Does it need hours of computer time to run? Does it handles gigabytes of data? Where is that input or output data located? Do you use any kind of [database](https://en.wikipedia.org/wiki/Database), such as [sqlite](http://sqlite.org/) ? Can you afford a 30% decrease in performance? Or is it just some homework? – Basile Starynkevitch Apr 15 '20 at 14:35
  • **Tell us more about your program !!!** How many dozens of thousands of C source code lines? You could use [sloccount](https://dwheeler.com/sloccount/) but any decent [source code editor](https://en.wikipedia.org/wiki/Source-code_editor) such as [emacs](https://www.gnu.org/software/emacs/) or [vim](https://vim.org/) is handling and able to show line numbers. You could also use [zip](https://en.wikipedia.org/wiki/Zip_(file_format)) to backup your C source code and tell us the approximate size of such an archive – Basile Starynkevitch Apr 15 '20 at 14:43
  • For reference the [GCC](http://gcc.gnu.org/) compiler has dozens of millions of source code lines, and the [Linux kernel](http://kernel.org/) is mostly coded in C and has about twenty millions lines of source code. See also [this](https://stackoverflow.com/q/247234/841108) question – Basile Starynkevitch Apr 15 '20 at 14:47
  • The source code is only about 500 lines long. It is not very CPU/memory hungry and finishes in under an hour. The amount of data it handles is probably in the range kb-mb if that. There is no input data. Output data is placed in folders in the directory the programme was ran. If I haven't said I use something e.g. database then surely you assume I don't use them. I very much thank you for you help and your answer on linking should be more than sufficient for me to sort it out. Thank you. – SamuraiMelon Apr 15 '20 at 14:57

3 Answers3

3

Generally speaking, C programs are not "portable" meaning they can't be copied over to other machines and expected to run.

There are a few exceptional cases where C executables can safely be ran on other machines:

  1. The CPUs support the same instruction set, with the same set of compatible side-effects and possibly the same bugs.
  2. The Operating systems support the same system api points, with the same set of compatible side-effects and possibly the same bugs.
  3. The installed non-operating system libraries support the same api points, with the same set of compatible side-effects and possibly the same bugs.
  4. The API calling conventions are the same between the source (platform you built the code on) and destination (platform you will run the executable on).

Of course, not all of the CPU and OS need to be 100% compatible, just the parts your compiled program uses (which is not always easy to see as it is compiled, and not a 100% identical representation of the source code)

For these conditions to hold, typically you are using the same release of the operating system, or a compatibility interface designed by the operating system packagers that supports the current version of the operating system and older versions too.

The details on how this is most easily done differ between operating systems, and generally speaking, even if a compatibility layer is present, you need to do adequate testing as the side-effects and bugs tend to differ despite promises of multi-operating system compatibility.

Finally, there are some environments that can make non-same CPU executables run on an operating system (like QEmu) by simulating the foreign CPU instruction set at runtime, interperting those instructions into ones that are compatible with the current CPU. Such systems are not common across non-Linux operating systems; and, they may stumble if the loading of dynamic libraries can't locate and load foreign instruction set libraries.

With all of these caveats, you can see why most people decide to write portable source code and recompile it on each of the target platforms; or, write their code for an interpreter that already exists on multiple platforms. With the interpreter approach, the CPU is conceptually a virtual one, which is implemented to be identical across hardware, letting you write one set of source code to be interpreted across all the hardware platforms.

Edwin Buck
  • 69,361
  • 7
  • 100
  • 138
3

If you pick the right CPU target (Project Configuration Properties -> C/C++: Enable Enhanced Instruction Set) such that the binary code doesn't include instructions understood by only a very narrow subset of CPUs, and the default for Visual Studio is to use instructions that are supported by the widest set of x86 or x64 CPUs, then your program will run on almost any Windows computer. You need to distribute any DLLs that aren't part of the base Windows installation though, which includes any additional dynamically linked language runtimes such as the Visual C++ runtime.

A good way to derive the list of DLLs that you have to package with your executable is to create a virtual machine with a fresh Windows installation without any development tools in it and then try to run your code there. If you get an error for a missing DLL, add it and repeat. When it comes to the Visual C++ runtime, Microsoft provides installable packages for the different versions that you are allowed to distribute as part of your installation (Visual C++ Whatever Redistributable Package).

Also, mind that programs compiled for 32-bit Windows will mostly run on 64-bit versions, but the opposite is not true.

Hristo Iliev
  • 72,659
  • 12
  • 135
  • 186
  • This is not bad advice, but it really doesn't cover a wide range; because, it assumes "portable among most Windows operating systems." I'm glad that's what was needed for this project, but it is far from even being portable for Microsoft Windows (which once even ran on Alpha CPUs) and doesn't cover the current ARM offerings. – Edwin Buck Apr 15 '20 at 14:37
  • That's my pragmatic view. From the comments: "I've only recently started C programming and this is my first time needing to be able to give someone an executable they can run." So it's not really necessary to burden the OP with topics like code portability and cross-compilation. That will come up naturally later on. Besides, yours and Basile's answers cover it. – Hristo Iliev Apr 15 '20 at 14:59
  • It's only necessary to burden them if they are to learn. If learning something about the machines isn't part of their future, sure, just tell them the right settings now without much understanding. Yes, I think you're answer is good (and probably right for their immediate need). I also think it glosses over a lot, which is OK too. – Edwin Buck Apr 16 '20 at 07:17
0

I've built a programme in C with Visual Studio using standard C libraries + a couple of windows libraries.

I'm wondering what would I then need to do to run my exe on another standard Windows computer without it needing to install any additional files e.g. the whole Windows SDK.?

You don't explain what your program is really doing. Does it have a graphical user interface? Is it a web server? Do you have some time to improve it or enhance it? Can it run on the command line?

Why cannot you share the C source code (e.g. using github)?

If you want some graphical interface, consider using GTK. It has been ported to Windows.

If a web interface is enough, consider using libonion in your program, or find some HTTP server library in C for your particular platform.

But what you need understand is mostly related to linking, not only to the C programming language (read the n1570 specification). Hence read about Linkers and loaders.

You should prefer static libraries. Their availability is platform specific. Read more about Operating Systems. Also, sharing some Microsoft Windows system WinAPI libraries could be illegal (even when technically possible), so consult your lawyer after showing him the EULA that you are bound to.

My opinion is that Linux distributions are very friendly when learning to program in C (e.g. using GCC or Clang as your C compiler). Both Ubuntu and Debian are freely downloadable, but practically require about a hundred gigabytes of consecutive free disk space. Be sure to backup your important data before installing one of them.

Did you consider porting, adapting and compiling your C code to WebAssembly? If you did that, your code would run inside most recent Web browsers. Look also into Bellard's JSLinux.

Related answer here. Notice that C can be transpiled to JavaScript and then run locally inside recent web browsers.

Community
  • 1
  • 1
Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547