From notebook code to the actual program with C++

Hey.
So as some of you might know, I’ve been trying to get into program coding, with my chosen language being C++.
I got a book on it and tried to follow its instructions but something went wrong. The sticking point: How to actually compile the code from the text program.
Not sure if I am barking up the right tree either, the book may be out of date or I may have the wrong type of computer (I have an HP Laptop 14).
I tried downloading the compiler from a site called OSDN. If this is the way to go, I must have set it up wrong, but being a novice I don’t know how to go about fixing it.
ANY help is appreciated, and thank you very much :slight_smile:

You can download GCC for free:

Yeah, if you are just writing basic C++ code GCC (stands for the GNU Compiler Collection) should work just fine.

Stranger

Depending on your comfort level with command line tools, if you’d rather use a IDE (An UI based application for programming). Visual Studio Community Edition is free. Tutorial here:

Yes, a good IDE makes life easier.
One can write the code in one window while the IDE helps with on the fly corrections and hints. Such as punctuation in the code.
Also can include a one click compile and run button. With some good warnings provided and clues to mistakes.

Now the IDE itself will require a bit of learning to take advantage of its power. But is well worth it.

IDE, schmIDE.

Create the following file; call it foo.cpp:

#include <iostream>

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

Next, compile it:
g++ foo.cpp

Finally, run the program:
./a.out

For starters, what operating system are you using on your HP laptop? Windows? And what’s the name of the book you’re using?

I’ve heard good things about Visual Studio Code on windows:

There are instructions there for how to install and start working with GCC.

It would also help us help you for you to say what you are trying to do and what happens when you try it - if you expect one thing to happen, but something else happens instead. Don’t be afraid to walk us through what you’re doing in detail.

If you wanted to start with the most difficult possible language, with the greatest potential for failing to climb the learning curve, IMO you could not have done better than to pick C++.

If you have some particular reason for choosing that as a self-taught hobbyist, I’d be very interested to hear it.

People can certainly get religious about which languages are good or bad or whatever. But pretty much everybody agrees C++ is an amalgam of several generations of different approaches to programming. Which means there are lots of ways to cut yourself trying to do simple stuff.

Agreed, there are many other languages better suited for starting out of the gate.

If you specifically need the C family, then as a learner C is better than C++.

If you want to focus on OO, then Java is a good choice.

If it’s something else, Python is a good choice.

Those are just a few alternatives. There are many more that might be better based on your interests and goals.

What a succinct way of pointing out how unpleasant it is to get started in C++.

After a few years of developing in C, happily writing software for those Symbol portable bar code scanner terminals in the 90s, I wanted to try my hand at C++ and suffered miserably as I really worked at getting my head around Visual C++ (compounded by all of its GUI framework stuff).

Then Java came out and I never looked back. Today, I wouldn’t even suggest a new coder start with Java, much less C++. Perhaps Python (a language I have only a passing familiarity with).

I can imagine someone wanting to learn C++ if they are going to do microcontroller work, such as Arduino, but I suspect that one can do that all in C, while steering clear of C++…

Without knowing your endgame, or if you even have one, I’d suggest you look at Arduinos. They’re coded in C++, Arduino IDE is (mostly) user friendly and (mostly) aimed at beginners, and there are a lot of resources out there. Plus, arduino boards are so cheap, they’re great for playing with and they give you the ability to make your code do something in the physical world.

I don’t know if you have any experience doing this in the past, but I haven’t done any programming since my HS comp sci classes (AP, but still, 25+ years ago) and that’s plenty for me to find bits of code around the internet and mash it together. And made even easier with modern compilers that will offer a lot of help with things like syntax.

Right now I have Arduino IDE open on my desktop as I’m writing a small program to drive bank of relays on and off at random intervals with an Ardunio board. When it’s done it’ll be 200ish lines of code (and would probably be 50 if I wrote it differently), very little of which I wrote from scratch. I found the code to drive the relays. I found the code to generate the random numbers, I found the code to create the delays etc. It was just a matter of making it work together.

Regarding your compiler, I don’t know anything about the one you’re using or what the exact nature of the problems you were having were, but both VS Code and Arduino (and probably whatever you used) have their own quirks that can sometimes make it difficult to get started. It’s not uncommon (at least IME) to have a compiler not work at all or throw errors about ‘missing libraries’ or things that mean nothing to you. More often than not, some googling of the errors will point you in the right direction.

Anyways, my suggestion is that you come up with a goal. Something you’d like to have your code do. Do you eventually want to be able to create a game? Do you have a 3D printer* you want to tinker with? Do you want to make something in the physical world (IoT device, RC car)? Give yourself some direction and work towards it. You’ll learn plenty along the way and have some motivation so it doesn’t feel like homework.

*If you happen to do any 3D printing, a lot of machines run (or can run) open source firmware written in C++ and are designed in such a way as to be easy for the casual coder to make changes. And just like with a lot of other pre-written code, you don’t even have to be particularly fluent in the language to just go in and make changes, but it’s helpful for learning and you’ll quickly gain the confidence to make bigger or more drastic changes, even when you’re just barely this side of literate (WRT the coding language).

Arduinos work in C or C++. Honestly, I don’t remember the differences between them, at all. I’m surprised C++ isn’t as common as I would have expected it to be. I still run across it regularly, but maybe that’s just because if I’m doing anything with any type of coding, it likely involves either an arduino or a 3D printer.

I’m glad I spent so long learning BASIC when I was younger. It’s given me the ability to pick up the basics of a new language somewhat quickly. You mentioned Java, I know nothing about it, but just glancing at some samples of code, I feel, maybe incorrectly, confident I could edit pre-written code to make changes without getting in over my head. Given the amount of pre-written code available online, for my purposes, that’s generally all I need to be able to do.

TLDR: My suggestion, Arduino. But also, what’s your goal here? If you can come up with a easy-ish goal, we can probably give you some better direction. Write a game? Make some lights turn on and off? etc.

It’s common for books about a programming language to just cover the language itself. How to compile the code is implementation detail that dependent on the environment you are using.

What you might want to do is look around for a C++ tutorial course on the web or YouTube. Some tutorials will just cover C++ code itself, but some will be end-to-end where they cover how to compile. They will tell you what compiler to use, how to get it, and how to run it.

But I agree with others that C++ is a very difficult language to learn as your introduction to programming. It has advanced concepts and it is trivially easy to make errors. Even experienced programmers have difficult times debugging C++ errors. It would be better to start with a simpler language to get familiar with programming in general and then move to C++ later on. Trying to learn how to program by starting with C++ would be like trying to learn how to drive by starting with a race car.

I am pretty sure all that Arduino IDE stuff uses GCC, which was mentioned in Post #2.

I was hesitating making a comment about the choice of language, but I’ll chime in as well.
C++ is a travesty of a language. One that deserves to die. It was bad idea when invented and it hasn’t got any better since. As one of my colleagues observed years ago, you get all the pain of C with none of the real advantages of OO. OK, C++, if you restrict yourself to the most basic bits, like simple encapsulation, makes for a useful step up from C for coding to the metal, but that is as far as I would go. But you are still coding with your feet stuck in the 1980’s.

Take this from someone who has quite a few years coding C++ professionally, but also from someone who has taught programming at university level. C++ is a terrible language to learn coding in.

As a few have said, the choice of language to learn depends on the intended use.
Personally, I would start with Python. Download the free community edition of JetBrains’ Pycharm IDE. Their development tools are first rate. I have the full professional set, which isn’t expensive. (I do use their C++ IDE if I am forced to code in C++.)

Python has the advantage that it has libraries and bindings that allow you to code almost anything, with access to really powerful stuff. And do it with ease and speed.

If you want or need to code down to the metal, learn Rust. That would be my other choice.

So, if you want to do embedded coding, get the Rust book, and the Embedded Rust Book, and the the STMicro STM32F3DISCOVERY development board (which costs $16) and get to it. (Well, get competent with Rust before hitting the embedded book.)

Indeed, the pairing of Rust and Python provides a very good basis for doing nearly anything you could conceive.

Bah. REAL programmers work in assembler.

I agree with this.

Well, yes. Recent editions of Knuth use a 64-bit RISC design.

I’ll play devil’s advocate here and say C++ is a perfectly fine language to learn programming with. It’s probably a bit of learning curve compared to other languages (Python and Java would be obvious alternatives). But you’ll learn useful things along the way regarding memory layout and pointers (but be aware you should probably never use those things in the real world)

I would advise against trying to learn with Rust (even higher learning curve, impossibly high IMO) JavaScript (you will learn terrible terrible things ;)) or C (it’s just a subset of C++ there is no reason the restrict yourself to the C bits of C++)

As others have said, C++ is a terrible place to start learning programming. You should start with a simple language where you can directly execute lines of code. The original forms of BASIC were ideal for this. I know there are Javascript and Java websites that provide that kind of facility. You don’t need to know anything about memory organizations or datatypes yet, and you should have a better command of procedural coding before starting into Object Oriented coding.

Make it easier on yourself, find someone who can help you directly in the initial steps. And also someone who can explain the principles of programming better than tutorials can. Anything you do will help you get started faster. It’s should only be a bumpy ride at the beginning. There’s so much to learn, and so many mistakes to make ahead of you, but they don’t need to be showstoppers because that education and correcting mistakes is what coding really is about.

Coincidentally, C was barely above the assembler level, and C++ was a bizarre attempt to wrap it in OO. See @Francis_Vaughan 's comments above.

I wouldn’t say ‘barely’… it has enough abstractions to shield it from most ideosyncracies of CPU architecture (though vestiges of PDP11 assembler are discernable if you look for them)!

I agree with you and Francis_Vaughan that C++ was a horrible mistake. Vastly more complexity for very little payback in better productivity or safety. Just more and more kitchen-sink junk bolted on to the groaning chassis of C.

For the OP: what is your goal? Is this a hobby, or are you thinking of getting into programming as a job?
If the latter: what kind of position? E.G. are you going to be writing drivers to attach new devices to a system, or are you going to be creating web servers or other user-facing applications?

For the former, I would have said learn C (and it is still in demand), though nowadays I would echo Francis_Vaughan and look at Rust. It seems to have the ability of C to work down to the hardware level while providing more powerful facilities. And seems to be getting a lot of traction in the industry these days.
I’m looking at it myself, and I would agree that there is quite a learning curve: but worth it if you think you might be working in the industry.

For more user-oriented stuff, I would agree with Python as a higher-level language.
Though as a dyed-in-the-wool C programmer I still HATE that ‘significant whitespace’ design choice…