Using g++ and ./a.out - c++

Hello everyone I have finally decided to learn to code, even if it is just a hobby, I once picked it up years ago and I am gong through the tutorials on codeacademy, I have a question for everyone.
Tonight I downloaded Codeblocks with Ming compiler, the programs work fine, the ones I have tried to rewrite from memory from my lessons, but I am missing something that became second nature during my lessongs. After writing the code required for the challenge, I would go into a folder with the extension .cpp and add the command g++ temperature.cpp(for example) -o temperature and then once the a.out file was created I would go into that and do the ./temperature command to execute the program.
I like being able to manually compile and than executre my code, and would like to be able to do this in codeblocks. I know it may sound stupid, and I know Codeblocks does it for me, but is there any way, I can do it for myself? Is there any way I can write my code and have the terminal on the right side of the screen like codeacademy has it?
Or should I just forgot about the g++ and ./ commands? I figure that I can do it throug the command prompt but that would require getting to the folder etc, and have not yet fully familiarized myself with the command prompt.
Thank you in advance for the help.

I first commented, then I decided to make it a full answer, since I kind of answered the heart of the question in the comments by accident anyway.
Yes, you can absolutely do this yourself. Using an IDE (Integrated Development Environment) like Codeblocks will make things easier for you, but there is no harm in learning how to do it under the hood if that interests you--especially if you're doing this for a hobby and not a work situation with time constraints.
To understand what g++ is fully capable of, I recommend consulting the official documentation. That will explain how to properly use it better than me and probably anyone else could.
While you're at it, I would also recommend learning how to use make if your platform supports it. It is a tool that can make compiling easier than working with the compiler directly, but offers more control than your IDE might (though this depends on the IDE).

You mentioned that you used the Ming compiler for Codeblocks, so I'm assuming that you're on Windows. To use the g++ command here, you'll have to
Add it to path, in order to make the g++ commands available for you throughout the system. This video explains adding the ming compiler to the path pretty well.
Call the command (from the command prompt, or some other kind of terminal)
You will have to get just the basic commands down for the command prompt, but it's not that daunting. cd to change directory and dir to list directory, and navigate to your project directory in codeblocks.
Finally, keep in mind that because you're on windows, when you compile with g++ you'll get an .exe file. .out files are produced when you compile on linux. If you want to get a linux shell on windows, I'd recommend looking into either Cygwin or WSL.
IDEs make compiling and executing much more convenient, but it's not a bad idea to learn how it all works through the command line. Best of luck!

Related

VScode says there are include errors when I try to write c++ programs

I haven't been able to compile c++ programs on vscode (or any IDE for that matter) and I can not for the life of my figure out why. This happened after I factory reset my computer due to other issues. Everything was working fine until then. I re-installed gcc through MinGW and checked that it is installed by typing gcc in command prompt. I can even compile programs through command prompt, which proves that gcc is installed. I would much prefer to use code though, and I was wondering if anyone knew why code is complaining about include paths.
Here's an image of the relevant file with the paths
ANY HELP IS GREATLY APPRECIATED!!!
Install C++ extension, you can find more information here:
https://code.visualstudio.com/docs/languages/cpp

Cppdepend handling from command line

I´ve downloaded the CppDepend static code analyzator, to analyze my .cpp programs. It seems to have a nice visual APK to manage my files, however, I´m more looking for something that can be controlled from the command line, so that I can create another program which runs that and does the appropriate stuff.
Visual application seems to have a quite rich Doc here: https://www.cppdepend.com/Doc/Getting-Started-with-CppDepend.pdf
but so far I have not found anything about it´s command version.
Does anyone have any experience with this or anything helpful to share?
Yes you can execute CppDepend from the terminal or any script using CppDepend.Console.exe

Trying to use Eclipse for c++ project on Mac

I am a total noob at programming and IDEs. I am attempting to open a project for some research that I am doing. I have a Macbook Pro using Mac OS High Sierra 10.13.6.
The project that I'm trying to open is a c++ project, but every time I open it, there are error messages everywhere. It appears that the header files aren't even being recognized. A screenshot is linked below. From what I've read so far, the issue seems to be that I don't have a proper debugger for c++. I downloaded XCode after I downloaded Eclipse like some guides have recommended, but it still hasn't fixed the issue.
Other sources I've found on this site use highly technical language (Eclipse GDB MacOSX Mavericks), so I'm struggling to figure out how to fix the problem. If anyone has any ideas, I'd love to hear them.
Here is a better solution for beginners.
Create a file called HelloWorld.cpp
and copy paste the following inside:
#include <stdio.h>
int main(int argc, char **argv)
{
printf("Hello world\n");
return 0;
}
Then open your terminal ( like that ) and write this:
$ g++ -g -o main HelloWorld.cpp
If this goes smooth it means you have gcc installed (good!).
After that try to run this:
$ gdb ./main
If this goes smooth it means you have gdb installed (great!).
Move to create a new Eclipse project with this file only
and update your post if there are any problems.
If this goes smooth gradually migrate your stuff to HelloWorld.cpp.
If gcc or gdb steps above fail, you have to install them first.
There are many online guides on how to do that. Good luck!
I am by no means a Mac expert or do I know much about eclipse CDT or Xcode but I can offer what I know. This is not a complete answer but I just want to share as I also know what it is like to struggle with C code on OSX.
First,
You need to make sure the C/C++ compilers are installed on the Mac properly. They come with XCode and I believe the compiler is Clang. Then you need to make sure the standard libs were installed correctly.
That whole issue is your first problem and should be asked to an XCode programmer and what not.
Second, to use eclipse CDT you need to tell it where and what compiler you are using. As well as the linked and debugger. You can do this in the project properties or settings. The stuff should usually be put in the path variable but once eclipse CDR knows where to find all that and everything is installed correctly it should pick up the header files and then include them in every project!
That’s all I can really provide you and I hope you can find out more.

VSCode c++ configuring for vc toolchain

I'm trying to start a work in vscode , latest C++ plugin version supports ms debugger, so as I'm mostly working under windows it was a signal to try this.
c++ tools plugin from MS and cmake tools were installed.
my test project was however not built . the problem is clear , but I need an advice from experienced vscode users how to solve this right.
cmake doesn't see vc compiler.
(after using QtCreator) I expected that vscode could detect vc installation... however that's not so. ok, I have a two ways:
fill environment variables INCLUDE/LIB/PATH with a headers, libs and binaries
just run vcvarsall.bat x64
Second way is a simple and reliable. so final question is:
how to run .bat at the begining of vscode start?
(I don't mean write another bat/cmd, prepare the environment and run vscode inside the same script after that)
Although the question is fairly old, I'll try to give a useful answer, in case others stumble across, just like I did.
I believe what you are trying to achieve is not possible. Code inherits the environment it was started with. If you did not launch it from a developer command prompt, you will not be able build and debug. (Building might be possible if every task first starts the vcvarsall.bat, but that slows things down by a lot. Debugging I think will still not work.)
If you are willing to, you can check out vector-of-bools CMake Tools extension which does build automation as well as automatic MSVC detection. It builds using CMake (thus you need to write your build scripts using CMake), but it will take care of building just by pressing F7, like what most VS users are familiar with.

How do you run programs through TextWrangler?

I have just started to get experiment with C++ on my MacBook Pro, and am very new to it all. Through research, a few different people have suggested to use TextWrangler for writing the code. I have put together some of the code that the beginning of a tutorial has given me, but I am now unsure how to run it through TextWrangler. I know that you are supposed to compile it first, but that is mostly where I'm stuck. I have downloaded Xcode so I have gcc, but I don't know where to go from there. And after I have compiled it, is there a simple way to run the file? Like I said, I am very new to all of this and may be looking at it in the wrong way.
TextWrangler is only a text editor, you still need a compiler to compile your code and then you need to run your code.
Xcode (which is an IDE) does all that for you so the confusion is understandable.
for now opening a terminal and doing g++ filename.cpp to compile and ./a.out to run your program will get you going. But you really have to learn to use a IDE or how to write a makefile.