I don't understand this.

Pages: 12
I'm using Code::Blocks IDE and I can't understand this part of a tutorial.It doesn't work for me.




If you’re using a command-line based compiler

Paste the following into a text file named HelloWorld.cpp:

#include <iostream>

int main()
{
std::cout << "Hello world!" << std::endl;
return 0;
}

From the command line, type:

g++ -o HelloWorld HelloWorld.cpp

This will compile and link HelloWorld.cpp. To run it, type:

HelloWorld (or possibly ./HelloWorld), and you will see the output of your program.
Last edited on
These instructions are for Linux and Mac systems.

For example, you can open a terminal window and type this command :
g++ -o HelloWorld HelloWorld.cpp

Make sure the file "HelloWorld.cpp" exists.

After running the command, you type ./HelloWorld and the terminal will run the program for you.
Last edited on
These instructions are for Linux and Mac systems.


Can be done on Windows as well from the cmd prompt
I tried typing it in my Hello world! program in the cmd but the application closes down immediatley after I press any key.
you need to add the compiler to the PATH variable, then in cmd prompt get to the folder where the .cpp file is saved and run it with the g++ (for compiler) -o (for make executable) prompts which will create the executable HelloWorld.exe from the source file HelloWorld.cpp. Then to run HelloWorld.exe from cmd just type HelloWorld (w/o the .exe)
This video explains it all in great detail https://www.youtube.com/watch?v=k3w0igwp-FM
GunnerFunner I added MinGW to the top box above system variables did I do it right?Or do I need to add Code::Blocks.
Last edited on
Unlike code where you can post it online it's difficult to second guess your system configurations from here. But if you have a C::B with compiler then you need to include the location of this compiler in your your PATH variable- the video, around 4:40 into it, shows how. If you don't have a complier (but I suspect you do) then you can download one here for Windows: https://nuwen.net/mingw.html(download the smaller file, w/o git) and then pick up the video again from around the same time line. The main thing is to get the compiler location into your PATH variable.

Also, this link has pretty much all the command prompts you'll need to execute this simple program: http://www.digitalcitizen.life/command-prompt-how-use-basic-commands

Good luck!

edit: btw, I just re-read your instructions:
If you’re using a command-line based compiler
... so you don't HAVE to, do you?
Last edited on
If you're using Code::Blocks you don't need to do this. Instead you probably have a nice "compile" button that you can press.

You can of course do what the tutorial is telling you. After all, it's good to know how things work "under the hood" so to speak. But in that case you should open the command prompt from outside Code::blocks. Probably the easiest way is to press Windows flag+R, type "cmd" and press enter. That will open up a new command prompt window where you can type commands and it will not automatically close itself.
Last edited on
this command is from linux kernal
bird1234 wrote:
this command is from linux kernal

What command? No, none of the mentioned commands is.

(If you do mean the 'g++', it might refer to a program within GCC and GCC itself is older than Linux by many years. See https://gcc.gnu.org/wiki/History Besides, Linux kernel is only the kernel, not "commands" and furthermore the kernel is written in C rather than C++.)


StrawBerry Lips wrote:
the application closes down immediatley

When you do something in a terminal, the terminal will not close after a program quits.

However, when you let GUI execute a console app, the GUI starts a temporary terminal for the app and by default closes it when the app quits. A low-life workaround is to not quit the app immediately. See:
http://www.cplusplus.com/forum/beginner/1988/
Last edited on
@keskiverto this command is use to run c++
code in linux

g++ -o filename.cpp compilefilename
./compilefilename // to run the c++ program

Last edited on
Not quite. The
./foo

is interpreted by a shell (and not by any "kernel") to mean:
look from current working directory a file named 'foo' and execute it, if possible

The dot refers to current working directory and the slash is a directory separator. Microsoft's command interpreters tend to use backslash as directory separator.

The 'foo' does not need to be a binary compiled from C++ sources. It does not even have to be a binary file; ASCII files are fine too, if the shell knows how to interprete them; a comment on first line of a file can tell the used interpreter (if not shell).

There are several different shells from various sources; in no way Linux-specific or related.
GunnerFunner I use windows 10 and I added a new path in the system variables did I do it finally right?It looks alot different than his path it is a buch variables on top of other variables.
Last edited on
did I do it finally right?
well there's only way to find out, did it work?
The route to your PATH is Control Panel -> System -> Advanced System Settings -> Environment Variables -> System Variables -> Path -> Edit ...
Did you watch the video link I sent you, all this is explained quite clearly there?
It seems to have worked but when I tried doing:g++ -o HelloWorld HelloWorld.cpp it doesn't find the file.
from above:
then in cmd prompt get go to the folder where the .cpp file is saved and run it with the g++ (for compiler) -o (for make executable) prompts
I'm sorry I don't get what you mean.
i mean you have to do the g++ -o thingie only when you're inside the folder that has the HelloWorld.cpp file and to get to that folder, if you need help, look at this list also sent earlier:
http://www.digitalcitizen.life/command-prompt-how-use-basic-commands
Show me an example cause I don't get it and nothing is working.
OK, here's an example, when I launch the Command Prompt window the blinking cursor is initially at
C:\Users\gunnerfunner>
And my HelloWorld.cpp file is in the D drive root folder. So to go from C:\Users\gunnerfunner to D:\ I first type where the cursor is blinking:
d:
and now the cursor blinks next to
D:\>
this means now we're in the D:\ drive and if your HelloWorld.cpp file is in this folder and your compiler has been correctly added to PATH you can now type at the cursor:
g++ -o HelloWorld HelloWorld.cpp
If everything runs smoothly this will create an executable HelloWorld.exe - you can look into the D:\ folder yourself and check if this file has now appeared. If it has then you need to run it, so go back to D:\ in the Command Prompt window and simply type
HelloWorld
If everything's OK then you'll see the output of the program i.e Hello World! just underneath where you typed. Unlike within Code::Blocks program output does not appear in a new window, in this case its just a single line within the Command Prompt window

I hope this works for you
Pages: 12