MinGW Distro installation issue - c++

I'm trying to install MinGW distro following the steps here: http://nuwen.net/mingw.html#install
But, when I try to run: gcc --version
at my command line I get the following:
'gcc' is not recognized as an internal or external command,
operable program or batch file.
Why is that?
Thanks.

You need to set up your PATH environment variable so that the command interpreter finds the gcc program.
E.g. (since you’re working in the Windows [cmd.exe] command interpreter)
set path=%path%;%programfiles%\mingw\bin
You have to use the path that applies to your system and your installation of the compiler.
You can set up a more permanent path via the Environment button in the System applet in the Control Panel. You can also do it more low level via the registry (e.g. command regedit). And in other ways.
By the way,
for C++ programming use g++, do not use gcc directly.
At least if you want to avoid trouble. Of course you can do manually what g++ does. But much easier to use g++; that’s what it’s for.
Cheers & hth.,

The command prompt cannot find the program 'gcc'. It's most likely because you have something wrong with the environment variable. If the pathname given in the environment variable is at all incorrect or non-existent (if you have the folder in another place), then command prompt will not be able to find it.

Related

gnuplot command not working with system() function in C++

My gnuplot command is not working in my C++ program. I am trying to run gnuplot command in C++ program like this:
system("gnuplot");
It gives the error:
'gnuplot' is not recognized as an internal or external command. etc.
when I try to run gnuplot in cmd, it works fine.
Any one have idea that why gnuplot command in not working with system() function in C++?
The reason is probably that the environment you execute your C++ program in doesn't tell your operating system to look in the right places to find your gnuplot executable.
Try the full path to the GNU Plot executable, include the file suffix (.exe) if you're on Windows, or make sure to set the PATH environment variable to include the directory in which your gnuplot executable is.
Check whether the path to the gnuplot is in your PATH environment variable. Or alternatively, you can use the absolut path to it when calling system()
Thanks you for answers friends,
I found the solution finally: After editing the environment variable $path, one has to restart the visual studio to get the latest value of environment variable. When I restarted my visual studio, it works.
So when ever you change your any environment variable, just restart visual studio (in case you are using it).
Thanks and kind regards,
Awais

How do I compile and run a C++ program from Vim?

Whenever I use the :!make % command, vim returns "make is not recognized as an internal or external command, operable program, or batch file." I have tried set makrprg=\"C:\\Program\ Files\ (x86)\\Microsoft\ Visual\ Studio\ 14.0\\VC\\bin\\cl.exe\". However, the same message appears. I believe the error may be in the path I have set, or the format of my statement; however, I am not sure if there is any other underlying cause.
I would greatly appreciate any input. Thanks in advance!
FYI:
I use a Windows 8 computer, and the compiler I typically use is the Microsoft Visual Studio 14.0 compiler.
! is a VIM command that invokes the shell. !make tells the shell to run whatever the shell can fund under the name make. If you want to use VIM's makeprg, you need to use the VIM command :make.
Having said that, setting makeprg to sonething that is not a real make-style program is probably going to work only in the very simplest scenario.
You can run the compiler directly with !cl %. You need to put cl.exe in your PATH and probably set up other environment so that cl can find libraries and include files.
This is because you do not have the make executable installed, which is what vim is looking for. If you're looking to compile on the command line with make, I would recommend switching from the Visual Studio compiler to MinGW
make is a Unix tool, and while it is also available for Windows (in various flavors, native, Cygwin, or MinGW), it is usually not what you will be using together with MS Visual Studio.
It is difficult to be specific, since you told us nothing about the project you are trying to compile, but I will try.
If your project is set up as a Visual Studio solution, you can compile it using devenv:
devenv /build release mysolution.sln
devenv /build release /project mysolution/myproject/myproject.vcxproj
Your project might also be set up for NMake (which is a make-like tool shipping with MSVC):
nmake [target]
The two commands above require the current shell to be properly set up, which can be achieved by starting a "Visual Studio Command Line" from the start menu, or running %VS120COMNTOOLS%\..\..\VC\vcvarsall.bat from whatever shell you happen to work from. (Adjust VS120COMNTOOLS to whatever version of MSVC you are using.)
Or your project might actually be set up using "real" makefiles, in which case I second Levi: It seems like make is not installed, or has not been added to your PATH environment variable.
make [target]

Need assistance with PATH variables

I am compiling C++ programs in Notepad++ and using command prompt to execute and run the programs. Initially I had this problem where the cmd said
"g++" was not a recognized command.
I changed path variable and it was fine. But later when I was started Eclipse, it would not start. I had to change PATH variable again.
So my question is do I have to change the PATH variable every time I switch to running c++ and Eclipse or is there an alternative?
Here is a simple tutorial if you're on windows 7 :
http://geekswithblogs.net/renso/archive/2009/10/21/how-to-set-the-windows-path-in-windows-7.aspx
Once you set PATH variables, make sure to restart your computer. I've been in a similar situation and it was me just getting ahead of myself.

Compile C++ code under windows for linux, as ready to run

Im new about compile code for linux. It's propably Debian 5.0. And I need compile my cpp code for it as ready to run, i mean the other person can easly run program like in Windows, by just clicking on it.
Anybody can help?
I use virtualbox for this. It's easy and convenient. You can run multiple Linux distros and multiple versions of Windows provided you have the proper licenses. You can also run subversion, etc on each virtual machine so that you can sync your changes across all of them when building.
Assuming you want to be able to compile something on Windows and have it work on any Linux machine, that's simply not possible. Debian and Ubuntu both support many architectures, many of which have absolutely no binary compatibility. If you know what type of hardware your friend has you can build a binary targeted to that architecture.
If you want a quick and dirty answer, you can build for i386 since a 64bit machine can probably still run it fine (not guaranteed though).
Once you compile it, you can easily create a shortcut on the Desktop -or add an entry on a menu- to launch your program via a script; something like:
#!/bin/bash
/path/to/your/progam
Save it as launch.sh -for example- and give it ugo+x permissions as such
chmod ugo+x launch.sh
When you create the shortcut, you can associate a icon to your script exactly in the same way you do it in Windows.
UPDATE
If you are sending the compiled program to your friend (let's assume via email). You can simply instruct your friend to launch the terminal window in the same directory where he downloaded your file and run the following:
chmod ugo+x your_program
./your_program
Or you can send him 2 files: one with your program and one with a "launch" script as I described above. Since both files will be downloaded to the same directory, you can change your launch script to:
#!/bin/bash
./your_program
When he clicks on launch.sh, your program will be executed.

How to make a linux command-line program work in windows?

I'm a beginner with programming, and I've been doing work in C/C++ in Ubuntu. When I tell something to cin/cout/cerr or printf/scanf or take arguments from the command line, this all happens from the linux terminal in Ubuntu.
Now if I want to run these same programs (very simple programs, beginner-level) and run them in Windows, how do I run them from the Windows command line? A previous course I've taken had us download cygwin to simulate the linux command line in windows, but what if I want to just run the program from the ordinary windows command line? Is that possible, and does it require modification of the software?
You can cross-compile the program for Windows from linux.
On Ubuntu, process is basically this:
sudo apt-get install wine mingw32 mingw32-binutils mingw32-runtime
...
i586-mingw32msvc-g++ -o myProgram.exe myProgram.cpp
Easy, right? Google for "ubuntu cross-compile windows," there's a ton of information out there.
It's exactly the same. You run cmd and write the command (almost) exactly as you would in Linux.
For example, if you build your program to program, you would run it in Linux like this:
./program --option1 -o2 file1 file2
And in Windows, first you have to make the output have a .exe suffix and then in cmd you would write:
program.exe --option1 -o2 file1 file2
Basically saying, cmd is Windows' terminal. It's nowhere near as good as the Linux terminal, but that would be all you get without installing additional software.
cin/cout/cerr and printf/scanf/fprintf(stderr, ...) use the standard C preopened files stdin, stdout and stderr which are defined both in Linux and Windows. Once you run the application from Windows' terminal (cmd), you see the input/output exactly as you would in the Linux terminal. I/O redirection is also very similar.
cin and cout, and printf and scanf, work much the same in Windows as they do in Linux. (I'm pretty sure cerr does too, but that one i'm not 100% sure about. At the very least, though, it's there and works.) The biggest difference is that Windows typically won't expand wildcards (stuff like *.txt) before running your program; you have to do that yourself in most cases.
Basically, as long as the app doesn't use anything specific to Linux or GCC, you could just recompile it on the target machine using whatever compiler you like to test.
If you don't want to recompile...well...good luck with that. Even Cygwin won't run native Linux binaries. You'd need a virtual machine with Linux on it.
Well, if you program is portable and not using any features specific to Linux, you would have to compile it from source on Windows to make it work on Windows.
You would need the GCC tool-chain for windows to do that, which you can get from the TDM-GCC homepage. Its MinGW internally and the installer allows you to choose the features you want to install as well as the target directory for installation. It also adds itself to Windows path so that the compiler commands are available from the shell prompt.
I have to do the cross compilation regularly and it works without any issues for me. There is one change which you must make if your project is using Makefiles. For the target binary, such as <target>.out in linux, you would have to edit your Makefile and rename it to <target>.exe so that it runs on the command line. If you are not using Makefiles and just doing gcc <file.c>, the a.exe is produced by default (similar to a.out in Linux).
Say you have this program code you want to run on UNIX and Windows:
#include <stdio.h>
int main()
{
printf("Hi\n");
return 0;
}
When you type a command in a UNIX shell it will be something like this.
/usr/home/bobby# gcc main.c
/usr/home/bobby# ./a.out
Hi
/usr/home/bobby#
On Windows you'd have to first choose your development environment/compiler. Without going to something like Cygwin, you could install the Windows SDK or Visual studio (although if the latter you might just want to develop in the GUI).
Start -> Run -> cmd /k ""C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\vcvarsall.bat"" x86
C:\Windows\system32>cd c:\bobby
C:\bobby>cl main.c
C:\bobby>main.exe
Hi
C:\bobby>
When a C program is compiled into an executable this is done in a system dependent way. On Ubuntu the ELF format is used and on Windows we have PE.
When you start a process the ELF or PE is read giving instructions/map on how to allocate memory and where to put various pieces of the process in a virtual memory table. Further it links up to dynamically loaded libraries, already in physical memory, that it share with other processes which is using the same libraries. Or if the dynamic libraries is not present load them. (Linux .so, windows .dll). If it has static libraries these are allocated and linked in (Linux .a, Windows .lib). - Very simplified.
Memory restrictions etc are inherited from previous process.
Environment variables are put into the running environment for the process. This being paths, arguments, etc. Then main() is added to the stack and called.
Now everything happening before main is called and how linkage etc are resolved, and so many other things, depends on the system. This is why one simply can't run an executable compiled on Linux on Windows.
Using cygwin one is simply creating a virtual environment where those linkages etc are the same and would work. One create an ELF environment.
To get it linked for native Windows command line one would have to compile for Windows. On that matter I see there is lots of answers already.
The ELF and PE, as on different systems, also have different ways of handling environment variables etc. What these are etc. So i.e. file expansion is handled differently. However both running processes has the default streams like stderr, stdout and stdin. But below your code in C they are not the same.
It is like driving a diesel vs a petrol car. Much is the same but under the hood quite a few things is different.
Be aware that i.e. signals are handled different on Windows.