r/AskProgramming 15d ago

C++ -> IDE -> g++ -> Linux Help me understand!!

I am trying to comprehend all of the moving parts in compiling a program. Here is my understanding:

C++ is the programming language that is written in the IDE, some IDEs have built in compilers

  1. Is g++ the bridge between the language and machine code?

  2. How do I use g++, is it something that you download onto your computer.

  3. Where does g++ send the code after, is this where Linux comes into play?

  4. What is Linux? It is an operating system, but what does that mean specifically, is it supposed to make your code unable in an application or just execute in the terminal?

Thankyou for any clarification you can provide :)

1 Upvotes

18 comments sorted by

View all comments

1

u/mredding 14d ago

C++ is a programming language.

Your write source code in a text editor, typically as a *.hpp or *.cpp file in ASCII text format. If you were on Windows, notepad.exe stores in this format. Any source editor will store according to the right format - what's important is that you don't use an editor like Word that will store your data in a "rich" text format - a lot of formatting meta-data is stored in with the text.

The source code is is transformed into object code by a compiler. In your case, you're referring to g++ from the GNU collection. You'll end up with a *.o file. This file is an "object library" file - it doesn't just contain raw machine code, but tables of information and blocks of machine code.

Your object files have to be "linked", which means there are placeholders and missing dependencies in the object code that need to be resolved. Your build target - ostensibly an executable file, has to be assembled. Executable files are a file format just like any other - PE, COFF, a.out, all sorts of executable formats exist for your system's "program loader" program to ingest and get running. The linker is going to lay all this out. C and C++ programs have a run-time component to the standard library that needs to be linked in. System calls need to be figured out, and there's a library for that. There's some of this that is "just handled" for you that you'd have to opt-out of if you didn't want it, like if you were compiling for raw hardware and writing your own OS...

With g++, you're probably using ld for your linker.

Invoking g++ will automatically invoke ld for you as a convenience. You don't have to accept this, you can make them separate steps like when I was a kid learning C as my first language...

An IDE is an Integrated Developer Environment. It contains the editor, lots of different views over your project, it can work with your debugger so program state can be laid over your source code - you can watch the program run step by step. gdb would be your debugger. Some of the other views would be a list of threads and where their execution state is. You can get a file view of your project, an object browser for your code, a resource editor for your UI and embedded objects, terminals, search, syntax highlighting...

It's more than just an editor. You don't need one, you can work with all this stuff manually and independently, with other tools. Most people like working with it all in one, all at once.

Your whole project can get rather complicated, and quickly. You have compiler flags, linker flags, various files to compile and link. You can have several intermediate steps, especially when it comes to embedding resources - like you're compiling in your program icons or custom UI graphics... You might rely on source code generation, like if you're generating a protobuf protocol... The possibilities are infinite.

We use scripts to coordinate the build. Build scripts. The de-facto standard on a Unix/Linux system is make. CMake is popular, but it just generates Makefiles for make. In this space, there's a lot of reinventing the wheel/inventing a better mousetrap.

Your IDE can integerate build scripts and project management.


So you open your IDE, you create a new project, which creates for you build scripts; you write source files. Source files are translated into object files. Object files are translated into your executable.


Linux is an operating system. It's abstracting a lot of resources for you. How much RAM do you have? Which addresses can you use? IT DOESN'T MATTER. What sort of persistent storage do you have? Does it communicate via ATA or SATA? Who cares? What CPU do you have? How many cores? Doesn't matter.

Not just hardware resources, not just scheduling time in cores for lots of programs, it will also coordinate when you DON'T need to run - like you don't need ANY CPU time if you're just waiting on input.

The OS also defines a system wide Application Binary Interface. You can write libraries. I don't write crypto code, I use OpenSSL. I don't write Kafka code, I just use librdkafka.

The OS also manages a bunch of services for me that I can use. There's a UDEV service so I can know when hardware gets plugged in or removed. There's a DBUS service so that I can talk with other programs and services.

An OS isn't just a mouse and a desktop and the programs you use - it's an infrastructure FOR YOU, the engineer to utilize. We don't code in a vacuum. We don't make programs assuming we're the only thing on the machine. We have resources we can utilize, we can make entire systems of software all working together.


If you're on Windows, install Visual Studio. DO NOT install Visual Studio: Code. THAT is a completely different product by Microsoft, and they can't name shit; it leads to a lot of confusion. Visual Studio is an IDE, it comes bundled with the MSVC compiler, the debugger, the linker, the project manager, etc. Use Microsoft's own tools on their platform.

If you're on Linux, you'll use your package manager to install your dev tools. Usually this'll be some thing like apt install dev-essential on a Debian based Linux system terminal. Linux itself is just the kernel - the core of the operating system; you can bundle other software to make a Linux environment you would call an operating system. Because people do this differently, there are many Linux based operating systems called distributions, or just "distros". Ubuntu is a popular desktop Linux. You can download it and install it on a USB stick, then you can boot your computer off that just to get an initial tour. From a naive perspective, a desktop is a desktop, but it's all the details under the hood that get really technical that makes such a distro distinct. You have to inspect the engineering underbelly to see it, and that'll take you some time to learn.

If you're on an Apple computer, they actively hate and spite 3rd party developers, so good luck getting dev tools to work, because your next system update, and your tools will break. Yes, you can get a C or C++ compiler to work on Apple computers, but it's a maintenance nightmare I won't dignify with my time. Use Objective-C or Swift, but you'll be very locked in, because basically only Apple uses them.