External Libraries & Program Creations

To infinity and beyond

This chapter wraps up the basics of C++ and programming and briefs you on what to do next. Once you've completed this chapter, you should feel ready to take on your projects.

We'll cover how to make various programs using your current knowledge.

A large part of development is using others' code. We'll cover how to use external libraries and APIs to speed up development. We'll follow up by discussing where you should look next to continue improving as a developer. We'll go through projects, resources, tools, and skills that will likely aid your development journey.

Other Types of Programs

A C++ program is a blank canvas. It doesn’t include a console or any other interface. The programmer decides what components or libraries to include based on the application's needs. The console is only there because you specified your program as a console program.

int main() {
    return 0;
}

No console interactions, graphical interfaces, or additional functionalities are present unless you specify them to be. You can use this type of program to create a background process.

Console applications are great for learning and making utilities that can do their job with a simple text-based user interface. Experimenting with libraries and frameworks lets you expand what your programs can do and the environments in which they can operate.

Environments and Frameworks

Many frameworks extend the capabilities of C++, letting you focus on creating new things rather than reinventing the wheel. Some of these tools are simple libraries you include in your projects, while others are complete ecosystems that utilize C++ as a component.

These frameworks can vary from user interfaces and game engines, to more straightforward tools that help you save data, or play sounds.

This section won't cover any of these tools in depth, but it will briefly touch on some of them to give you an idea of the applications you can make.


This section is meant to serve as an example of the available tools. You should do your own research before selecting tools for your projects.

Graphical User Interfaces (GUIs):

Qt is a framework for developing cross-platform desktop applications with responsive GUIs. It uses C++ as the primary language for creating and managing user interfaces and core application logic. Google earth, DaVinci Resolve, and VLC media player all use Qt.

wxWidgets is also a toolkit for creating cross-platform GUI applications with native interfaces. It uses C++ for the application's logic and interface. wxWidgets is integrated directly into your C++ project and is known for its simplicity and robustness. Audacity, BitTorrent, and Code::Blocks all use wxWidgets.

Game Engines

Unreal Engine is a game engine known for its high-quality graphics and performance in game development. It uses C++ for gameplay logic. It's worth noting that Unreal Engine has a blueprint system, which lets you create scripts visually with nodes that you can combine with C++ code for more flexibility and control. Many AAA games use Unreal Engine: Fortnite, Borderlands, and the Batman Arkham series all use Unreal Engine.

SDL is a low-level library that provides access to multimedia, input devices, and rendering, primarily used in 2D game development. It is included directly into your C++ project, where you use its functions to manage windows, handle events, and render graphics.

External libraries

Creating your own tools is great for practice. However, using others' code has many benefits. "Outsourcing" creation of common functionalities saves a lot of time, which gives you more time to develop the unique aspects of your projects. It also leverages others' expertise, ensuring high reliability and performance (for established libraries).

An external library is simply a collection of code that you've included in your project. You don't have to understand why or how the code from your external library works. The only thing you need to worry about is what it does.

To use an external library, you must download and configure your development environment to recognize it. This differs based on the type of library. The simplest and most common external library is a collection of code files (.h and .cpp) that you can include using #include.

Pre-compiled Libraries

Pre-compiled libraries remove the need to compile the library or resolve dependencies manually. It also protects the distributor's intellectual property, as it doesn't expose the underlying source.

There are two primary types of pre-compiled libraries: Static Libraries are collections of object files (compiled source files). The code is copied into your executable during compilation. They typically have a ".lib" extension on Windows or ".a" on Linux & MacOS. Dynamic Libraries, or shared libraries, are linked at runtime rather than compile time. The code is not included in your executable, which makes the application smaller and lets multiple programs share the same library code at runtime. They typically have a .dll extension on Windows or .so on Linux & MacOS. In both cases, you interact with the library through a header file containing the declarations for functions, classes, and variables but not the implementation.

Including Static Libraries

The library's header file tells the compiler how to use functions and structures. However, you must tell the compiler to connect the declarations with the library. This is generally done through your project settings or command-line flags.

In Visual Studio, you select the .lib file in the Linker>Input>Additional Dependencies section. This tells the linker that the implementations to declarations in the header files are in the .lib file. If you're using a different IDE, you'll have to find a guide for it.

How to include dynamic link library in [name of your IDE]

Look at this chapter's practice tasks for examples and walkthroughs of external libraries. Completing the tasks should clarify any questions about the inclusion process.

API:

An Application Programming Interface(API) enables communication between software systems. This lets you exchange data and use the features of other applications.

There are too many ways for applications to communicate to cover all of them. However, most APIs have documentation detailing how to use it. We'll cover the most common API types to give you a general knowledge of how to work with APIs effectively.

You can find API examples in the practice section.


Types of API

Library APIs are the easiest to use. They function like an external library and give you functions and structures to work with. You've already used Library APIs. std::cout, std::cin, std::ofstream, and std::ifstream all interact with the operating system to manage input/output to files/the console. These functions enable you to communicate with other systems or programs by calling them.

System APIs let you interact with the hardware and system resources. The Standard Library already simplifies these interactions for you. When you use std::ifstream to read a file, the Standard Library handles the system calls, making your job easier.

Web APIs let you interact with web services over the internet. These APIs enable your application to send requests and receive data from servers. A weather application might use a Web API to fetch the latest weather data from a remote server. The response you get is often some file you must process before using in your program. While it might sound complex, libraries like cURL make handling Web API communication in C++ easier.

Tips for using APIs & External libraries

External libraries and APIs all have their differences. You need to research the ones you want to work with to learn how to use them.

The best place to find information about an API or library is their documentation. Most libraries and APIs will have documentation detailing their capabilities, how to use them, and how to set them up. Referring to the documentation will save you a lot of time and is essential when working with APIs and pre-compiled libraries since you can't access the source code.

The second best place to go is your favorite search engine. Documentation can't cover all scenarios. Luckily, if you're running into issues, your question and the solution might already be on the internet.

AI tools can help if you're working with a well-known library/API. They are less reliable for lesser-known libraries. They also often get confused and mix libraries, sending you down rabbit holes that don't lead anywhere.

Licenses

Many projects have licenses attached to them. You should ensure that you understand the license's contents when you're using a library or an API.

Other programming languages

A lot of programmers limit themselves to one language. Some people do it out of love for the language. However, most people do it out of fear. They fear the difficulty of learning another language. They might feel like learning their first language would be a waste if they don't stick to it. In reality, the only difficult thing to learn is programming. The hardest skill in this course isn't C++; it's programming. Programming languages have a lot in common. Learning a second programming language is more like learning how to drive a different car than the one you learned to drive in. Learning to drive a different car than the one you'll buy isn't wasted.

It's easier to learn to use a saw than to cut wood with a hammer. Don't let inexperience with a language hold you back from doing a project you want to do. Once you feel like you can confidently say "I know how to program in C++"; you should open up to learning new languages as a part of your projects.

The language barrier

A big door opens when you learn your third language. Learning a second language can be tricky because you don't know what is specific to a programming language and what is shared. It becomes unbelievably easy to pick up new programming languages once you figure out their commonalities and differences. The third language you learn will demonstrate this.

How to learn other languages fast

The 80-20 rule in programming.

The Pareto principle states that for many outcomes, roughly 80% of consequences come from 20% of causes. From my experience, this ratio is closer to 90%:10% in programming.

When working with a new language, you'll encounter problems you know how to solve in C++. By figuring out how to do the same thing in your new language, you strengthen your knowledge of it. Once you've run into 10-20 cases of this, you likely have the minority knowledge that covers most scenarios you could run into.

Things you'll find in almost all languages

Almost all languages share the same fundamental building blocks. The first step of learning a language is identifying the syntax for these building blocks. Variables, Conditionals(if), Functions, Loops, and Data Structures (arrays, vectors, etc.)

Useful tools

The environment you're working in needs to support your language. If you're using a C++-exclusive IDE, you should install an IDE that supports your new language. Visual Studio Code has a wide range of language support.

The fastest way to pick up a new language is through online practice websites like Codingame, LeetCode, and HackerRank. These offer low-commitment language-specific tasks that help you develop your knowledge quickly. YouTube walkthroughs of projects created in a language can help you quickly pick up on language syntax, capabilities, and practices.

Differences between languages

Things unique to C++

You'll notice that many languages make themselves more accessible by abstracting away concepts required to work with C++.

Higher-level languages abstract away memory management, pointers, and references.

Many languages have some form of struct/class system, but features like inheritance, especially multiple inheritance, are rare.

Dynamic typing

Many languages use dynamic typing. A variable's datatype isn't fixed and can change at runtime. Dynamically typed languages often have a single declaration word like let or var that declares all variables.

let example = 555; // example holds an int
example = "Crazy stuff, right?"; // now it holds a string

This dynamic typing also means that templates are redundant, as declaring function return types is unnecessary.

// Functions don't even have to return anything.
function sum(x, y) {
	let sum=x+y;
	return sum;
}

These variables still have a type, but dynamic typing prioritizes flexibility and ease of use. C++ prioritizes performance, control, and safety by keeping data consistent with singular allocations. These languages can use dynamic typing because the performance hit from reallocations isn't important to them.

Different syntaxes

Here are some key structural differences to look out for in other languages: Semicolons: Some languages (like Python) do not require semicolons to terminate statements. Brackets: Some languages replace the curly brackets ({}) for code blocks with indentation (Python) or keywords like (like end in Lua).

### Python and Lua Examples
def greet(name):
    if name:
        print(f"Hello, {name}!")
    else:
        print("Hello, World!")

greet("Alice")

Indentation is very important in Python.

function greet(name)
    if name then
        print("Hello, " .. name .. "!")
    else
        print("Hello, World!")
    end
end

greet("Alice")

Where to improve further

Congratulations! You have just finished the last chapter of section 1. This concludes the basics of programming.

The best way to continue is to question what you want programming for and learn to use it for that. In 90% of cases, this means getting started with your projects and learning whatever you need to complete them.

I recommend using the projects and tasks from this course until you nail down the fundamentals. However, no tasks section can compare to undertaking your own projects.

I recommend checking out the Algorithms, Optimization, and Recommended tools chapters of section 2, even if your projects require another language. The Smart pointers, STL Libraries, Advanced memory, and Exception handling sections are great additions to your C++ skill.

GOOD JOB!

YOU HAVE FINISHED AN ENTIRE SECTION!

This would be a good time to practice what you've just learned.

With the completion of this section you'll also unlock a set of projects that you can undertake.

Check them out now!

PracticeNext Section

Questions

Questions

Questions

Questions

Questions

Questions

Questions

Questions

Questions

Questions