Understanding CLI Commands for Testing/Running Beginning C++ Programs


So, you want to learn C++? Great, but you have to pass another challenge first. That namely is, understanding how to make your IDE work. What's an IDE? The short acronym for an Integrated Development Environment, an IDE is where to draft, test, compile and execute your C++ program before finalizing it to become a permanent executable program.

Whether in Windows or Linux, your programming code has to be tested, removed of bugs and syntax glitches, and "cleaned" to run smoothly. The process doesn't get rid of all bugs, such as soft logic ones like users not following directions and entering weird stuff for input, but it does get rid of the mechanical problems that would stop a program cold, like an undefined variable or a missing end line semicolon, etc. 

Damn you, IDE! My Kingdom for a Clean Run!

The frustration, however, comes with the IDE itself. Whether using JGrasp, VSCode, Visual Studio or something other, it's going to become a bit of headache trying to configure an IDE to actually run a C++ program. Even in the Linux environment, nice IDEs like VSCode won't work, telling you it can't find the main.exe when attempting to run it. So, fortunately, there's a workaround. Your CLI.

No, I'm not trying to be funny. CLI stands for your command line interface. When you work in the IDE, you get a terminal view option. Use it. This basically operates like a CLI terminal in Linux, and you can use similar commands to move around and get things done in it, or you can use it to compile your program and run it. The two big commands to remember are g++ to compile, and then ./a.out or ./a.exe to run it, depending on your OS. I'll show you the difference.

The first thing to remember is to make sure when opening your code files in the IDE, you open up the folder they are all in, not just the singular file. You can see the two options in the VSCode file menu below:

ffdbd11bf8fe83255afceac7ef89ebac7dd39aa9f40a99d893feb78c1f946146.jpg

Why? This tells the IDE to pay attention to all the files in the folder, not just the file you're looking immediately. Depending on your IDE, it's going to then show all the files, which you can then drag into the work-edit window or reference, like below:

7087bf39ab8360c51d74e149522c0be712796baec926d71a0876e7e31921755a.jpg

Ok, so here's where the fun begins. If you just open up the files, and then try to hit the run and compile button on your IDE, one of two things is going to happen. Either, it's going to work because your IDE is perfectly configured for C++ (rare), or it's going to error out because the IDE can't find the command to execute the run to turn your main file into an executable (very common and frustrating as hell). 

517b323ca292900e2d0f16f6558ac930f1c4e56e4832fe5db8d6fd7737cddff0.jpg

Now, at this point you could spend hours scanning the Internet for a configuration method fix, which gets really complicated depending on your IDE, or you can use the CLI method to compile and run. Wut? huh?

The CLI Method

Just like using CLANG for C Standard, you can use a CLI command for C++. It's just a different command. To compile you run C++ and the file names (all of them) to run your program. That means, you include the main, header and function file names in the command at the same time as below (just a bit redacted from my identifiers, lol):

f8ec61a4df01214f1f658c6f4699c227a879ff94ac64fdc68039ef9334cf4b03.jpg

So, g++ and the files were entered and... nothing happened? Is that good? YES! It means the files compiled without errors and are ready to run.

Now, the next step is the run part, or execution. Remember, just hitting the IDE button didn't work because the IDE couldn't find the exe file. Well, with this method, it created a temporary generic exe file. Depending on your OS, then you just type the next command as such:

./a.out for LINUX

./a.exe for Windows

[why are you asking me about MAC, why would you do such a thing?!?]

My example is in Windows below:

e41bb50f4ed0fbed5f0f359aeb76623e90174b22911655d7ddd40ed4af1cebbe.jpg

And there you have it, my vector program executed and displayed the output as expected. 

I know there are folks who are very advanced and can figure out IDE configuration really way on the fly. But the reality is, especially for beginners, nothing is more frustrating than not being able to even practice learning coding because the stupid IDE is too much of challenge to get it to work. This simple CLI approach allows you to focus on learning and avoid the headache of configuring a complicated CLI. It works in JGRASP, VSCode and others. Just remember which OS you're in and which command to use to run the code.

There are options to g++ to, as well as other methods of compile that create a named executable instead of a generic one above. You might also need to think about authority to read/write/execute in the given folder as well, especially in a Unix-environment (i.e. chmod, etc.). But the above method is a good step forward for a beginning coder in C++ versus getting stuck from something having nothing to do with your code written.

Happy scripting!

 

PS. MACOS has a slight variation to the above but it is practically the same approach as Linux. See below in this example from Geeks for Geeks website:

6800915655b9dc16983e0e96f1da53e106d745612134bad37a898200c8aa8e65.jpg

How do you rate this article?

11


WinterYeti
WinterYeti

A professional freelance writer for the last 20 years and a budding photographer by hobby.


The Intersect of Crypto Musings & Consumer Impacts
The Intersect of Crypto Musings & Consumer Impacts

A blog focused on ongoing government regulation for crypto or consumer issues with crypto with wide range of topics from pitfalls to avoid to opportunities to grab.

Send a $0.01 microtip in crypto to the author, and earn yourself as you read!

20% to author / 80% to me.
We pay the tips from our rewards pool.