+ 2

Compile and Run C++ Program on CLI without IDE

So I had a question about C++. Specifically outputting to the console Code::blocks sets up the console application project for me. But how do I set it up myself? I barely know about any of this. In fact, I really only know how to run it by pressing "run" in code::blocks. Let's say i'm coding in VSC(Visual Studio Code) I can write the code and save it as a cpp file. But if I try to run it, it just opens VSC. How would I get it to output to the console without relying on an IDE to set up the project for me? Sorry for the long question.

14th Oct 2018, 12:50 AM
Daniel Cooper
Daniel Cooper - avatar
10 odpowiedzi
+ 2
To put things short: Write a hello world program in c++ (any text editor will do). Save it to a file with a .cpp extension Run cmd (or bash. im assuming you use windows) Type 'g++ <filename.cpp>' (In my first answer, I told you how to setup mingw so you could run g++) Then, type 'a.exe' (assuming you use windows)
14th Oct 2018, 4:17 PM
qwerty
qwerty - avatar
+ 5
Daniel Cooper In the simplest sense: You don't need an IDE to type code. You just need a basic text editor. To not rely on anything to set up and compile the project for you, would mean to compile it yourself using the cmd/shell/terminal, whatever you call your CLI.
14th Oct 2018, 2:28 PM
Hatsy Rei
Hatsy Rei - avatar
+ 4
You need to have a compiler. Use g++ to run it. In cmd (which can be accessed in vs code), try doing g++ main.cpp (or whatever your file's name is). If it doesn't work then try adding {where you installed CodeBlocks}\MinGW to your PATH variable
14th Oct 2018, 12:57 AM
qwerty
qwerty - avatar
+ 4
nonzyro Yeah I just stopped using windows this year. Linux is way better for me. But I use Ubuntu instead of Debian because my touchpad and wi-fi doesn't work on Debian.
14th Oct 2018, 7:02 AM
qwerty
qwerty - avatar
+ 3
If you're on Linux/BSD, it's so simple. Open a CLI and type "g++ <your-file.cpp> && ./a.out" to compile and run it. Sadly I take it you're on Winblows. Winkey+R opens the "Run" box. Then type cmd.exe and follow the instructions qwerty gave. Personally I find Winblows a nightmare. I recommend a Debian PC for coding or a dual boot or a VM. FYI: I haven't used an IDE in ages. I haven't used Winblows since 7. My life is better for it.
14th Oct 2018, 2:01 AM
non
+ 3
Hatsy Rei I don't think that's what I'm asking. Lol To simplify, I'm trying to figure out how to make a console application without code::blocks setting everything up for me. Like, I don't want to rely on something setting up the project if I don't have to. Is the compiler doing that? And I'm a bit confused now guys. Don't we need an IDE to type code? sorry if these are stupid questions I'm extremely new to all of this lol
14th Oct 2018, 2:25 PM
Daniel Cooper
Daniel Cooper - avatar
+ 2
Daniel Cooper A compiler is a program that makes your executable (.exe) based on the files and options you give. It has nothing to do with setting up your project. If you press "run" on Code::blocks, it calls the compiler (g++) to make the executable (if it wasn't compiled already) and runs it. Without a compiler, you can't run your code.
14th Oct 2018, 3:59 PM
qwerty
qwerty - avatar
+ 1
qwerty That's coz, by default, Debian is Free/Libre/OSS only. However you can use proprietory drivers by adding "non-free contrib" to your sources.list file. If it works on Ubuntu, then it will work on Debian. If you need wifi while installing, use the unofficial Debian + Firmware image: "firmware-[version].iso" Ubuntu does make getting software easier, but I find it less stable than Debian Stable and a lot of its system infuriating (it has a Debian base, but works very differently). It's a personal thing though. Many people find Ubuntu easiest. I find the opposite because of the era I grew up in (286 was my first PC).
14th Oct 2018, 10:31 AM
non
+ 1
ok That helped a lot. Thanks for the explanation.
14th Oct 2018, 9:40 PM
Daniel Cooper
Daniel Cooper - avatar
0
If you have some old Visual Studio installed (I have version 12), you can compile a c++ program that you edited in any text editor using cl.exe (examlple: cl myprog.cpp). Make sure you set the variables using the supplied vcvars32.bat. You have to link myprog.obj to get myprog.exe.
16th Oct 2018, 8:30 PM
Lope Geronilla
Lope Geronilla - avatar