If you'd prefer to watch the youtube video on this topic, please click here.
What Is A Make File?
A "make" file is a simple bash script that is traditionally used to compile/run our code files. This means make files usually live in the "src" directory. (Hey, what's that logo at the top left of this screen say?)
Use make files to build commands to handle different situations you need to compile or clean up your project. See the next section for example situations. How To Make Our Own Make File
Imagine that we're creating a C++ project. For now, let's just start with some simple Hello World code in a file named main.cpp.
To compile this, we'd use the following command in a terminal: g++ main.cpp -o run This compiles our main.cpp file into an executable named "run". To use run, we'd then do the following: ./run And our terminal will output "Hello World, from srcmake". So with that, we have our C++ file, we know the command to compile it, and we know how to run it. To create a make file, we just need to write the following into a file named "makefile": all: g++ main.cpp -o run ./run To use our make file, all we need to do is type "make" into the terminal.
make
And like that, the terminal will execute the commands in sequence. We no longer have to memorize the actual commands. (Keep reading to learn why we need the "all:" part.) But why would we even use a make file? Well...imagine that now we have multiple files. Let's move the hello world part of our program into a separate file, to demonstrate this. In a file named helper.cpp, we have the following code:
helper.cpp contains a simple function that says Hello World. Well, now we need a header file to match this .cpp file, so let's create a file called helper.h
And finally, our main.cpp file will now include this header, and make use of the SayHi() function. Our main.cpp is now:
Our project just got a lot more complex. Now we have 3 files in this folder (not including the make file, or the run file). Not only that, now to compile this, we need the do the following: g++ main.cpp helper.cpp -o run Notice that we had to include the helper.cpp file to be compiled. Well...once your project starts having more than ten files, you really don't want to type all the file names out every single time we compile our code. Let's just tell our make file the file name! all: g++ main.cpp helper.cpp -o run ./run In the terminal, we still have the same simple word to command and run our program!
make
And so, make files make our compiling (and running, and more) a lot easier. Is that all make files are good for? No, they can do much more. Special Commands for Make Files
So far, we've only used the word "make" in the terminal and it does one thing. But actually, that's because we only specified the "all" part of make. Actually, we can make our make file as complex and have as much complexity as we want.
Let's change our make file to the following:
all:
echo "Use 'build' to compile, and 'clean' to remove the run file."
build:
g++ main.cpp helper.cpp -o run
doit:
./run
clean:
rm run
We now have the following commands available to us (to type in the terminal):
make
Our "all" parameter specifies what the default "make" command does. In our case, it gives a message to the user. make build Our "make build" will compile our C++ program. make doit Our "make doit" command will execute the run file (if it exists). Notice that we can't name the parameter "run" but instead must name it something else, like "doit". That's because the make file would assume ./run was referring to the make parameter 'run' (if it existed). Confusing? Yeah. make clean Our "make clean" command removes the executable file.
These are just some examples of what you can do with make files. The way you want to use a make file is up to you and your project, but using one is definitely preferred over memorizing the commands for compiling.
If you'd like to watch the youtube video to see how these commands work, please watch the video embedded below:
Like this content and want more? Feel free to look around and find another blog post that interests you. You can also contact me through one of the various social media channels.
Twitter: @srcmake Discord: srcmake#3644 Youtube: srcmake Twitch: www.twitch.tv/srcmake Github: srcmake
References:
1. Me! Where do you think I came up with the name "srcmake" from?
If you'd prefer to see the youtube video instead of reading, please click here.
What Is a "Git Patch File"? Why Use One?
What happens if we see a mistake in a project on github/gitlab? Sometimes, we'll be able to make a pull request, meaning we can fix the mistake yourself and update the repository, but there'll be times that we can't because we don't have authorization to do so and the project owner doesn't want to grant it to us.
In that case, we can clone the repository, make the changes in the project that we need too, and send the fixed project back to the owner! But sending the entire project is a bit...much...we only really need to send the owner the changes that we made. That's where git patches come in. We can make changes to a project, create a "patch" file that indicates the change(s) to the project that we made, and send that patch file to the owner. Using that patch file, the project owner can quickly review the changes to ensure it's a legitimate change that won't harm their project, and apply it to the project. How To Create A Git Patch File
To learn how to create a git patch file, let's go through a very simple example. I've prepared a repository with a simply Hello World python file here. The python file has the following code:
Unfortunately, it looks like I've misspelled two words: the '3' in Hello should be an 'e', and the '0' in From should be an 'o'. Won't you download the project, fix the spelling, and email me a git patch file so I can update the project? First, let's clone the git repository and move into the folder: git clone https://github.com/delocalized/wrong cd wrong Well now we have the repository, but it's set on the master branch. Let's create a new branch for all the changes we're going to make. git checkout -b SpellingFixesBranch Great! Now we can start making some changes. However, as we're professionals, we're going to handle each change as a separate commit. (It's never good to change too many things at once.) Open HelloWorld.py and change the '3' in Hello to an 'e'. Save the file, which now looks like this:
Looks good so far. Let's add this change to git and commit it with a useful message. git add . git commit -m "Fixed the spelling of Hello" Okay, now let's open HelloWorld.py up one more time, and change the '0' in From to an 'o'. Save the file and it now looks like:
Perfect. Now let's add and commit this change to git. git add . git commit -m "Fixed the spelling of From" We've finally fixed the project! So let's create the patch file to send to the project owner. We'll run the following command to create the patch file:
git format-patch master --stdout > mychanges.patch
That will create a patch file named "mychanges.patch" that tracks all the changes we made to the project. Email/give the file to the project owner, and they can apply the changes you made! Notice that we've created a single patch file. If we wanted to create individual patch files for each commit that we made, then the following command should be run:
git format-patch master
This will create a patch file for every single commit that we made. It's a little messier, but it also makes it easier for the owner to apply individual changes, in case they're picky and will only accept some of the changes but not others. How To Deploy A Git Patch File
In case you happen to be the project owner who receives a patch file and you'd like to use it to update your project, you just have to use one simple command to do so:
git am mychanges.patch For the youtube video demonstrating this code, please watch the embedded video below:
Like this content and want more? Feel free to look around and find another blog post that interests you. You can also contact me through one of the various social media channels.
Twitter: @srcmake Discord: srcmake#3644 Youtube: srcmake Twitch: www.twitch.tv/srcmake Github: srcmake What Is Multithreading?
Multithreading is an important utility for many computer programs, and can help your own programs increase performance efficiency.
Your computer (or phone/tablet) runs all of its software through its CPU, which processes instructions. Often, CPUs come with multiple cores, and each core can process a certain number of threads, meaning that the CPU can handle multiple instructions at the same time. Multithreading lets a program take advantage of this by efficiently dividing workloads over separate threads to get the work done quicker. In the next section, we'll go over an example that demonstrates how multithreading is quicker than single threading. How To Multithread In C++
We know what multithreading is, but how do we implement it in our own programs? In C++, it's quite simple. We need to do the following:
1. Include the thread library in our program. 2. Create a thread worker. 3. Call that thread worker on the function we want to spin off on a separate thread. Let's demonstrate this with an example.
We're going to create a function to kill time. The function, aptly named KillTime, will simply iterate through a while loop.
We're going to call this function three times using the normal way, and then three times by spinning the function off onto a separate thread each time.
You can also find the source code on github, linked here. If you'd like a video demonstration where I walk through the code with you, then please watch this youtube video.
The "NormalWay" calls KillTime, and when it ends it calls KillTime, and when that ends it calls KillTime. Each KillTime must wait for the previous KillTime to finish before executing.
The "MultithreadedWay" sends KillTime off onto a separate thread (if one is available), and the main thread continues executing the next line of code immediately, which will send another KillTime off onto a different thread, and finally the main thread will send the third KillTime off on another thread. Each KillTime will happen simultaneously, without needing to wait for another to finish.
When I ran the code on my own computer, the NormalWay code took 7.8 seconds to finish, and the KillTime took 2.6 seconds to finish. Multithreading is quicker, yay!
When To Use Multithreading
Multithreading makes our program much more efficient, and in many cases quicker. There are two special occasions that we should use multithreading:
1. When we're performing a long calculation that takes time, it may be better to divide the work into separate threads, if possible. 2. If our application has separate features that don't need to be coupled, it's better to separate work off on a separate thread. (This is always true for applications where we have a UI and need to perform a calculation asynchronously.) Use multithreading where necessary to make programs run quicker (and get the most out of your CPU). Warning
Multithreading is a nice feature, but it should be used with caution. There are a few fatal mistakes that are possible if multithreading is misused. In another article, we'll go over how to properly multithread.
As a sneak peak, the things a good multithreading application is aware of is: 1. Not sharing variables between threads. 2. Ensuring that threads are properly disposed of. 3. Only using locking a thread for when necessary. To see the next article that goes over a more in-depth analysis of multithreading, click here.
If you're prefer to watch the youtube video, where I demonstrate what each line of code means, then please watch the video embedded below.
Like this content and want more? Feel free to look around and find another blog post that interests you. You can also contact me through one of the various social media channels.
Twitter: @srcmake Discord: srcmake#3644 Youtube: srcmake Twitch: www.twitch.tv/srcmake Github: srcmake |
AuthorHi, I'm srcmake. I play video games and develop software. Pro-tip: Click the "DIRECTORY" button in the menu to find a list of blog posts.
License: All code and instructions are provided under the MIT License.
|