I am totally new to the coding world and I’m trying to learn it in my days off. After a little introduction to coding with python, now I’m trying to learn c++.
The problem is that every IDE I use, it does not return neither the “Hello World!” program, so I cannot start learn it and get some first experience with it because I’m totally stucked because the compiler. In a first attempt, I was thinking about some IDE’s configuration but now I’m guessing it could be an environment variable to set in the system.
Maybe it is the simplest thing in the world to solve but I can’t understand where “put my hands in” to solve it.
Same behaviour about Vala using Gnome-Builder.
I searched along on the net but the only half-solution is about the CMakeList.txt file (that is not in my project folder when I create a project using one of the softwares above).
So, what I have to do to make it works?
Is it an app problem or a missing system setting?
You don’t need an IDE to write basic, toy programs for learning purposes, period (as @Kresimir so kindly pointed out). Just use the command line first. In fact, it is better to use the command line interface when you’re learning so that you know what’s going on rather than just relying on “magic” behavior.
If you just need to compile and run some C++ code, just use g++
, which is a program provided by the pre-installed gcc
package.
Let’s say you have a simple helloworld
program:
// helloworld.cpp
#include <iostream>
int main()
{
std::cout << "Hello world.\n";
}
To compile and run this, open a terminal, cd
into the directory containing the helloworld.cpp
file and run:
g++ helloworld.cpp -o helloworld-exec
The compiler will produce an executable file called helloworld-exec
inside the directory that you can execute from the command line with
./helloworld-exec
As for why building with your IDE didn’t work, we won’t know the reason until you post the full error messages describing the problem.
Personally, I can’t stand CMake. It’s awful. The good old GNU Make is much better, but if a project is simple enough, I just write a Bash script for building it. Using a more complicated build system only makes sense when it saves you time and effort.
To program in C++, I recommend Kate (KDE’s Advanced Text Editor), with the package clang
installed, which provides IDE-like features to Kate (an LSP).
To configure LSP for Kate I create the following file:
~/.config/clandg/config.yaml
with the following contents:
If:
PathMatch: [.*\.cpp, .*\.hpp]
CompileFlags:
Add: [-Wall, -Wextra, -std=c++17]
This sets up clangd
(the language server) to use C++17 standard.
For debugging, I use gdb
from the command line, even though Kate has a pretty nice interface to it.
For building my little personal projects, like I said, 9 out of 10 times I just write a Bash script, but when there are many compilation units, I use the good old GNU Make (but I use Make much more with LATEX, for example).
You never really need an IDE. It can be helpful, but it’s a personal preference.
My daily job is as a C++ software developer. I never use an IDE, even at work, where I work with a huge codebase with hundreds of compilation units. My company recommends using Eclipse, but I find IDEs to be slow and annoying, I prefer a good text editor, with an LSP support.
Also, for beginners, relying too much on an IDE just cripples the learning process, in my opinion.
True. I use neovim for everything nowadays.
Agree. Most IDEs are very resource hungry. Bloated as well – they come with a bunch of features that most developers don’t even use. And they require more use of the mouse, which doesn’t fit into my keyboard-centric workflow.
Sadly a lot of tutorials and training do not even bother to explain compilation/linking or at most limit to compilation of a single hello.c
file. Then dynamicaly linking library and header file inclusion becomes confusing for the beginners. The reasoning is you can just use IDE and there are too many compilation parameters and it would take too much time to explain.
As long as your text editor does not mix tabs and spaces. Whoever made that design choice had really sick sense of humor.
GNU makefile must have tabs, not spaces. Most good text editors know they are dealing with a makefile so they take care of that. Kate certainly does.
Yes, semantically meaningfully whitespace is a terrible idea. Python is a terribly designed programming language, too.
And apparently, these IDEs with seemingly magical properties can’t even run a simple helloworld program, at least not in a way so intuitive that allows beginners to get it to work with a few clicks.
@Flusso_Canalizzatore You should post the error messages if you still intend to resolve the original issue.