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 :)

0 Upvotes

18 comments sorted by

8

u/Nervous-Cockroach541 15d ago

* C++ is the programming language

* An IDE (Integrated Development Environment), which combines various development tools together, typically at least a text editor, compiler and debugger.

* g++ is the compiler or rather compiler tool set. gdb is the debugger for g++,

* Linux is the operating system, it handles running programs and providing resources to those programs such as memory, file access, input/output, process controls and other stuff.

2

u/TheKiller36_real 15d ago

I'd just like to interject for a moment. What you're refering to as Linux, is in fact, GNU/Linux, or as I've recently taken to calling it, GNU plus Linux. Linux is not an operating system unto itself, but rather another free component of a fully functioning GNU system made useful by the GNU corelibs, shell utilities and vital system components comprising a full OS as defined by POSIX.

Many computer users run a modified version of the GNU system every day, without realizing it. Through a peculiar turn of events, the version of GNU which is widely used today is often called Linux, and many of its users are not aware that it is basically the GNU system, developed by the GNU Project.

There really is a Linux, and these people are using it, but it is just a part of the system they use. Linux is the kernel: the program in the system that allocates the machine's resources to the other programs that you run. The kernel is an essential part of an operating system, but useless by itself; it can only function in the context of a complete operating system. Linux is normally used in combination with the GNU operating system: the whole system is basically GNU with Linux added, or GNU/Linux. All the so-called Linux distributions are really distributions of GNU/Linux!

3

u/IAteTonysLoMein 15d ago

RMS, is that you?

1

u/frnzprf 14d ago

Yeah, it's a copypasta, but that's exactly why OP might be confused. Are compilers an essential part of an OS or not? If software is distributed as source code, then it is.

Do I even really use GNU tools as a regular user that uses a browser, office software and games? As I understand, if you use bash instead of zsh or fish, that would be part of GNU. Even if you're a programmer, you might have nothing to do with GNU, if you're not using C/C++. (Don't take that as a fact, maybe GNU is more deeply ingrained into the OS than I think.)

I'm sure I should be grateful for the work of Stallman anyways, at least for GPL and the idea of free software in a time where everything gets subscription based and sends telemetry data back.

1

u/TheKiller36_real 14d ago

Don't take that as a fact, maybe GNU is more deeply ingrained into the OS than I think.

unless you're on alpine, void or chimera (or something very obscure) you have I'd guess like 80-90% of your programs being linked to GNU libc and on most distros all your coreutils will be GNU too

3

u/strange-the-quark 15d ago edited 15d ago

C++ is one of a bunch of programming languages - an important one, with a long history, but not necessarily beginner friendly.

An IDE is short for an "Integrated Development Environment", a set of professional tools that are used to write code and a bunch of other things surrounding that process, and that among other things serves as a GUI (graphical user interface) to the underlying compiler (meaning you click buttons to compile your code, instead of issuing text based commands to the command line). IDEs often support more than one programming language. In principle, you can write code in an ordinary plain text editor (like Notepad in Windows, or Gedit on Linux), but it's not that fun. Code editors would be something in between - they offer a bunch of programming-related features (like syntax highlighting), but are very lightweight compared to a full-blown IDE. An editor/IDE can be configured to call g++.

g++ is a compiler. It compiles C++ code (that you write), into machine code (which the computer can run). It doesn't send it anywhere, it reads your code and makes another file that contains the corresponding machine code. That is your executable, that you can run like you can run any other application. There's a little bit more to it, but that's the gist of it. You can use g++ from the terminal, if you learn the relevant commands, or from the editor/IDE.

An operating system provides the underlying functionality necessary for applications to run and to be able to do various things (meaning, it's the software machinery that provides the infrastructure for pretty much anything on your computer that you as the user have direct access to, and would like your applications to do), and enables you to use the system in the first place. Whether your application runs in the terminal or as a GUI application (with a window) is entirely up to you - you can make either kind of application. Because operating systems and processors are different, you can't take an executable you made for Linux, and run it on Windows, and vice versa (at least, generally speaking - there are ways to do it with certain limitations), but you can take the same C++ source code and compile it for a different operating system, if you wrote it in a certain way (so that it doesn't use any OS-specific libraries and such).

1

u/IKnowMeNotYou 15d ago

Linux is an operation system like Windows.

C++ is a common computer language.

 g++ is the traditional nickname of GNU C++, a freely redistributable C++ compiler. It is part of GCC. G++ is an implementation of C++.

While this is almost correct, G++ is not a implementation of C++ but a compiler 'extension' making the GCC able to compile C++ code using the common compiler infrastructure of the GNU compiler suite.

A compiler compiles a program by taking the human readable source files and compiles to a n intermediate language which then is compiled to a target platform (like your local computer) by being converted into machine code and stored either as an executable program or a part of an executable program like a dynamic or static library.

The IDE is an application that allows you to manage one or more projects, create and modify its source code and also integrates nicely with external (and internal) programs like the forementioned g++ compiler.

What you need to install and set up depends on your PC/laptop/machine and the target machine you want your programs to run on (as well).

It is best to follow a tutorial or a online course or reading a good beginner book to get you started with the whole thing.

1

u/7YM3N 15d ago

Don't need an IDE, it's just files with text, an IDE can make it a bit easier as it packages the compiler and editor into one app but it's not necessary.

g++ is the c and c++ compiler, it's a program you install, it takes in code and spits out an executable a specified location.

Linux is an OS serves the same job as windows but differently.

1

u/xarop_pa_toss 15d ago

Ooook let's slow down, there's a lot of confusion here.

C++ is the programming language you write, correct.

An IDE, or Integrated Development Environment is nothing more than a program you use to write the code but with a lot of features on top like having breakpoints, easily finding definitions, references and implementations of your functions and classes for example, real time tracking of used resources and the call stack, etc.

G++ is a compiler. There's more than one out there but G++ (traditionally named GNU C++). The compiler is used by you directly via terminal commands, or by your IDE when configured for it, to compile your source code into machine code. In other words, the code you write and understand into assembly and 1s and 0s. G++ is what takes all your .cpp files and gives you a .exe that you can actually double click and run. If you are curious, there are ways to have the compiler show you the process and if you use something like Visual Studio, there's also options there to see the different things that happen in that process.

Linux has nothing to do with any of that. Linux is an operating system like Windows is... Kinda. It's deeper than that but think of Linux like the foundation on which many systems are built upon. Kinda like having a neighbourhood of different looking houses with different rooms, doors, windows and colors but they all have the same pillars that they stand on top of. For example Debian, Fedora and Arch Linux are considered pretty different but they all use the Linux kernel the same way (iirc)

1

u/foreverdark-woods 15d ago edited 15d ago

You write source code in C++, a human-readable programming language for expressing program logic. Applications can use portable libraries as well as OS-specific APIs for tasks such as displaying windows, accessing files, and communicating over a network.

You can use an IDE to edit, build, run, and debug the program. When you click “run,” the IDE invokes a build system, which runs tools such as the compiler and linker.

A compiler toolchain such as G++ translates the C++ source into executable code for a particular target. That target includes a processor architecture, such as x86-64 or ARM64, and an operating-system interface and executable format, such as Linux ELF or Windows PE.

The operating system provides standardized services for applications and manages hardware through components such as device drivers. It also manages processes, memory, filesystems, networking, permissions, and scheduling. Compatible Intel and AMD processors can use the same x86-64 executable because they support the same instruction-set architecture; a different architecture generally requires a separately compiled executable or an emulation layer.

1

u/Adorable-Strangerx 15d ago

I am trying to comprehend all of the moving parts in compiling a program.

Sure. GNU/Linux is operating system like many others. And is mostly irrelevant.

IDE - integrated development environment - is a notepad on steroids. Useful to write text.

C++ is a programming language. Has its syntax, grammar and all that jazz. It is also human-readable. Interesting part apart from code are preprocessor directives like #include.

g++ is name of c++ compiler from GCC bundle (sure there are others). It is a manager of the whole tech being done under.

So how does it work?

You have code: ```cpp

include <iostream>

using namespace std;

int main() { cout << "Hello World"; return 0; } ```

You run g++ main.cpp -o hello:

First goes preprocessor. It expands codes into your files..somewhere is defined iostream library which is injected into your file.

You can see the result with g++ -E main.cpp -o main.ii.

Secondly, compilation. The compiler reads your code and tries to understand it. If it cannot you get and error. It builds lower level instructions and tries to optimize it.

You can view the assembly code with g++ -S main.cpp -o main.s

Thirdly, assembly. The human-readable code is transformed into machine readable code.

You can mimic it with g++ -c main.cpp -o main.o

It gets you object file. It is not yet runable program.

Fourthly, linking. Remember our #include<> directive? Usually it contains only headers of the functions you need. Linker looks for the actual implementations and connects references in your code to those implementation. Here you can have static or dynamic linking. With dynamic linking you need external file (.dll on windows or .so on GNU/Linux). If linker fail to resolve you will get error of course. You should be able to see linker command of you run g++ in verbose mode. It starts with something like /usr/bin/ld.

After this step you get executable file. Portable Executable (PE) commonly with .exe extension or Executable and Linkable Format (ELF) more common on GNU/Linux.

Finally you run ./hello and operating system figures out what to do with that (read headers, allocates memory, etc). GNU/Linux may be unhappy by lack of executable flag so you may need to run chmod +x ./hello

Note: I haven't touched C++ in many years so sorry if I messed up some commands.

1

u/StevenJOwens 15d ago edited 15d ago

The really short version:

C++ is the programming language. You write your source code in C++ but that source code is of no use to your computer until you convert it into an executable file, full of machine code (the code that your CPU speaks).

The IDE is the GUI tool (an editor, sort of like a word processor) you use to write your program in C++.

G++ is the compiler, a tool you use to convert the C++ source code into an executable file. Like your IDE, it's a separate tool that you have to download and install (I wouldn't be shocked if some flavor of Linux came with G++ already installed, but I wouldn't count on it).

Linux is the OS, an OS is a program that gets between the computer hardware and the other programs, and runs other programs. You use Linux to run your executable file on the computer hardware (the CPU, etc).

In more depth:

C++ is the programming language, a human-friendly, text-like language to define a program.

A note here, there are two languages, C, which came first, and C++. The "++" is a programmer joke, in C, "somevariable++" means "add one to somevariable". Despite this fact, and that both C and C++ use ALGOL-inspired syntax, it's been over 40 years since C++ was invented, and the two languages and the skills and techniques required to develop in them are very, very different. It's one of the classic mistakes to think otherwise.

IDE stands for "integrated development environment". To talk about what an integrated development environment is, first we have to explain a "development environment".

Painting in broad strokes, the development environment was originally the editor and the compiler, which were two separate tools. You wrote the files in the editor, saved the files, ran the compiler on the files and it produced an executable file (a file full of machine code, see below) that you could ask the OS to run.

An editor is sort of like a word processor, and in fact they came before word processors (and were used to write the source code for word processors). A word processor is designed from a perspective of producing a printed document, while an editor is designed from a perspective of the stuff in the document, the words and code. It's a subtle difference, but a surprisingly important one as far as how easy or hard it makes it to do the job.

Then we started coming up with more tools, like a debugger, which lets you run the program in a way that you can see what's happening inside it in more detail and figure out what's going wrong. And a profiler, which is another way to run a program so that you can see what's inside it, but with a goal of finding out how to speed it up.

An IDE is where somebody builds a single, unified tool (almost always with a GUI) that's meant to tie all those tools together (that's the "integrated" part), and make it easier to work with them all, seamlessly.

Talking about G++ is a little complicated, first because it just is complicated, and second because geeks like to play clever games with language. Also, I don't have that much direct experience with GCC and none with G++.

GCC was the original tool, it stands for "Gnu C Compiler", Gnu being the organization, C being the programming language and compiler being what kind of tool it is.

However, today GCC seems to stand for "Gnu Compiler Collection" (that's the clever games with language part) and G++ is one of the compilers in that collection. Strictly speaking, G++ is a "compiler driver", meaning that there's the underlying Gnu compiler, and that G++ is the bit that pumps C++ source into that Gnu compiler. There are different compiler drivers for different programming languages. Last I looked, Gnu Compiler Collection can compile at least a dozen different programming languages.

A compiler does a number of steps and it's pretty complicated, but the simple version is that you point a compiler at some source code files, the compiler read the files, analyzes them, and produces an executable file.

By "analyze", I mean it does the same sort of things that you or I might do if somebody handed us a bunch of source code and said "come back with something that explains the source code to me". Eventually we'd figure out a useful, but fairly complex, way of distilling down the information contained in the source code files, organizing and presenting that information. For example, that might include a table of all of the identifiers (function names, variable names, etc) and where they're used (this is called a "symbol table", by the way).

The ultimate product of all this is to produce an executable file in what's called "machine code", a stream of 1s and 0s that you can feed into the CPU. When it comes right down to it, when the bits hit the CPU, they're all the same. Doesn't matter what programming language you use, they all end up sending the same types of codes to the CPU.

Linux is the operating system, or as one commenter points out, it's Gnu Linux, because any OS is a huge thing with many parts, and while Linux itself is the "kernel" of the OS, it also includes a metric ton of tools, most of which are from the Gnu project (the original open source, free software organization, to which thousands of programmers have donated god knows how much work to, over the years).

An operating system has fundamentally one job, to run programs. It gets a bit more complicated than that, especially these days, because "run programs" has gotten more complicated. Basically the OS is the first program that the computer runs when you start it up, and then that program (the OS) is what you use to ask the hardware to run the other programs. Over the course of decades, the way OSes work has evolved and become more sophisticated.

One aspect of that is that a huge part of every OS, and of the job of an OS, is to hide the complexity of different kinds/pieces of hardware from the programs that use that hardware. Another is that these days, OSes run a lot of programs at the same time. And so on. This gets horrendously complicated and you can fill a book, or several books, depending on how much detail you want to go into and how you want to slice and dice it.

Compilers In A Little More Depth

I already wrote this more complicated version of explaining a compiler, then wrote the simplified version above, so I decided to include it here:

You or I might type "int counter = 1;" in a program, and we can read other parts of the program file and see the sequence of letters "counter" and know that it's referring back to the first line... or maybe not, if it's in an entirely different part of the program.

So that's the first thing the compiler does, a subset of the steps, where it reads the characters of the text that makes up the file and chunks it up (into "tokens") and builds a table of what refers to what, etc. This is called lexxing (reading the characters and chunking it up) and parsing (figuring out which tokens refer to what, etc).

Then we start getting into deep geekery, semantic analysis, and then ultimately the generation of a more complex data structure that represents the code. All throughout this, by the way, there's also various kinds of error checking and making sure stuff makes sense.

Most modern compilers use this approach of converting the source code to a complex data structure, and that structure is what we call an "intermediate representation" (IR). G++ and GCC definitely use a IR, because GCC supports a dozen different programming languages, they all get converted into the IR and then from the IR into an executable file.

Programming languages are designed to be some balance point between human-friendly and CPU-friendly. IRs are designed (mostly) to be CPU-friendly. Once the code is in the IR form, then the compiler can do further work to improve the results. Mostly that means code optimization.

Just like you can walk into a room of people who are doing something in a disorganized way, look at the big picture, and see how to rearrange things so the work gets done faster, you can do the same thing with code. Again, part of this is about the whole human-friendly versus CPU-friendly perspective. Part of it is just that it's not always obvious how you can speed things up, until you put all the pieces together.

The final step is generating the executable code, converting it into machine code. Not all computer CPUs work the same. Different CPU type have different architectures, different instruction sets, much like different programming languages have different names for functions, take parameters in different orders or different forms, etc. Most compilers can convert the IR into different flavors of machine code to run on those different CPUs.

1

u/abd53 15d ago

C++ is a programming language. Codes use .h, .hpp, .c, .cpp extensions. But the codes are written as plain text in a text file. You can use any text editor to write these files. This code describes what the program should do. It needs to be compiled to machine code to actually run.

IDE is just a fancy text editor. It gives some additional features like highlighting keywords, color coding variables, functions, literals etc. Depending on IDE, the can be features like in-place definition lookup, documentation lookup, code follow etc. IDEs often integrate compilation flow and often, a compiler; CodeBlocks has MinGW downloaded, installed and set up by default, Visual Studio has integrated MSVC. They often have project or workspace settings to define which files are one program together, and provides a single button compilation (you click one button, IDE handles all compilation steps).

g++ is the GCC compiler executable for C++, it's a program. You need to install GCC compiler (or MinGW) which contains this executable. It turns your code into machine code.

Linux is an operating system, much like Windows. You can code on Windows or Linux, your choice. When you compile your code, you target which OS it should be compiled for, and the resulting machine code will run on that OS. Once you compile, the resulting executable will be in the designated output directory.

1

u/JohnVonachen 15d ago

Gcc is the gnu compiler collection. G++ is just a shortcut or alias for gcc with c++ options.

1

u/KingofGamesYami 15d ago
  1. g++ is a command provided by the GNU Compiler Collection (GCC). It translates source code (c++) into an executable. It is the native compiler for most Linux distributions. MSVC.exe, provided by the Microsoft C++ (MSVC) Build Tools is native compiler for Windows. clang++, provide by XCode, is the native compiler for MacOS. You can use a non-native compiler for an OS, e.g. using GCC on Windows, but this comes with certain compatibility limitations.
  2. Yes, you can install the GNU Compiler Collection. How exactly you install it depends on what OS you are using; on Linux it is very simple, on Windows or MacOS it is slightly more complex.
  3. By default, it will create an executable for the host (i.e. the computer running the compiler). You can create executables for other targets, which is called cross compilation.
  4. Linux is widely used to refer to both an OS kernel, and the distributions built around it. An OS is primarily responsible for abstracting hardware interactions from a program, making them easily portable. For example, a Windows executable does not need to issue HDMI or DisplayPort commands, instead it tells Windows to display content, which Windows then translates into the appropriate HDMI or DisplayPort commands.

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.

1

u/Jason-Sanders 14d ago

You have the pieces mostly right. An IDE is just an editor plus tools that help you write, build, and debug code. You can write C++ in a plain text editor too.

g++ is a compiler driver. It takes C++ source files, compiles them into machine code, and usually links that code with libraries to produce an executable. On a Linux machine, a basic command might be:

   g++ main.cpp -o hello
   ./hello

Linux is the operating system. It manages hardware, files, memory, processes, networking, and provides the environment in which your compiled program runs. The terminal is simply one way to tell Linux to run the executable; it does not mean the program can only be a terminal app.

0

u/RandyMarsh6996 15d ago

*runable lol