Sign in with Google to keep private notes

C++ Compiler and Linker

Taken from learncpp.com

0.2

In modern computing, programs often interact with more than just hardware -- they also interact with other software on the system (particularly the operating system). The term platform refers to a compatible set of hardware and software (OS, browser, etc…) that provides an environment for software to run. For example, the term “PC” is used colloquially to mean the platform consisting of a Windows OS running on an x86-family CPU.

A program that can be easily transferred from one platform to another is said to be portable. The act of modifying a program so that it runs on a different platform is called porting.

low level languages hinder portability. thats probably why we use a higher level language like C++ to handle that porting

Machine code output by the compiler can then be packaged into an executable file which can be launched by the operating system. Interestingly: running the executable file does not require the compiler to be installed.

Rule: I cannot not do it
Best Practice: I should do it, but I can not
Warning: I should not do it, but I can

0.3

C++ was started in 1979. It was a superset to C until C99 which introduced features C++ doesn't have. C++'s biggest difference is supporting object oriented programming out of the box. C++'s degree of freedom allows you to hurt yourself if you don't know what you're doing. C++ gives you a degree of freedom when you think you would know better than a machine. Hint hint.

C++ excels in situations where high performance and precise control over memory and other resources is needed. Here are a few types of applications that C++ would excel in:

  • Video games
  • Real-time systems (e.g. for transportation, manufacturing, etc…)
  • High-performance financial applications (e.g. high frequency trading)
  • Graphical applications and simulations
  • Productivity / office applications
  • Embedded software
  • Audio and video processing
  • Artificial intelligence and neural networks

C++ also has a large number of high-quality 3rd party libraries available, which can shorten development times significantly.

It's a tradition of people who care a lot about performance.

0.4

Name your primary file main,cpp.

#include <iostream>

This is a macro, it's called a preprocessor directive. It's called a preprocessor directive because it's effect is done before the compiler starts processing the code. It's finding a file called iostream and pasting all of its contents before the file begins.

main() is a special case, where the return type must be int. This is because main() is the entry point for your operating system, so you should be allowed to not return anything.

I dicked around with my compiler for awhile

  1. You don't need to specify which order the files come in when building. the linker does this for you automatically

  2. This is what happens when it can't find a function to link to

Starting build...
/usr/bin/g++ -fdiagnostics-color=always -g -std=c++20 -pedantic-errors -Wall -Weffc++ -Wextra -Wconversion -Wsign-conversion /home/dhelmy990/cpp/*.cpp -o /home/dhelmy990/cpp/main
/usr/bin/ld: /tmp/ccLSwxHf.o: in function `main':
/home/dhelmy990/cpp/main.cpp:11: undefined reference to `checkStandard()'
collect2: error: ld returned 1 exit status

I played around a little bit in C++. Apparently if i declare a function which is implemented as int do_something(); , but i set the function signature to char do_something(); that would allows a successful link, but would override the original functional signature.

Also, if those declarations are faulty (e..g mispeled) but the functions are never called, then the linker does not complain

Ok there's some nuance to it. For example the following code will throw LNK error "undefined reference to `Log(char const*)'"

void Log(const char* message);

int Multiply(int a, int b) {
    Log("Message");
    return a * b;
}

int main() {
    std::cin.get();
}

The reason the error is called even though Log() is never called in main() and hence, probably, compiler folded, is that it's not actually being compiler folded.

A link attempt is still made because a different function might try to call this function.

But if you use the static keyword (and you use O2 optimisation) then it's all good in the hood. Because it compiler folds it away since we know everything within this translation unit, and we know that it will not be called within it.

Static Keyword

void Log(const char* message);

static int Multiply(int a, int b) {
    Log("Message");
    return a * b;
}

int main() {
    std::cin.get();
}

Because saying it's static means it only belongs to this one file.

Preprocessor Directives

https://www.youtube.com/watch?v=3tIqpEmWMLI

#INCLUDE <iostream> //this will just copy paste it in
#DEFINE INTEGER int //this ctrl + fs and replaces instances of INTEGER with int
#IF 1 //you can use this to choose to include or exclude certain sections of code based 
#ENDIF  //on conditions at compile time

Compiler folding: gets rid of redundant lines (that are not used later in the program), performs calculations like 2 * 5 at compile time

Files don't actually exist in Cpp. Cpp is mostly just the compiler converting text files into machine code. It's just a standard.
If you configure the file to take in .Diego instead and you write you .Diego files as if they were cpp, then the compiler would work.

I think there are some standards of how the compiler is meant to treat for example .h files.

translation units: normally 1 file is a translation unit. each translation unit is converted into an .obj file. Unless you #INCLUDE multiple cpp files into the same cpp file, then that group of files becomes a single obj file.

all programs need an entry point. You can configure your compiler to select a different entry point. Doesn't have to int main

Linker

https://www.youtube.com/watch?v=H4s55GgAg0I

-If you use the inline feature on functions, their code bodies get hard dropped directly in whenever they are called. Pretty cool.

-If you #include in header files the same function multiple times, you will get a linker error due to redefintion

TODO: Look into make and build2 as extensions i can use to automate tests

Articles to read next