After finishing this project, you can:
You need a Linux Operating System to run gcc and git. The recommended operating system is Ubuntu 16.04 LTS
. If you do not know how to install, you can find the tutorial at the appendixes.
It is only optional for you to install a new operating system. If you already have an accessible Linux, you can SKIP this.
Other Unix-like systems (e.g. Mac OS) should also work but they are not recommended, although it is still OK if you insist on it and work well with it. If you don't have such an operating system, a new installation is required.
For new users, a desktop helps a lot when trying a new OS. Remember to install GNOME desktop (the installer will do it by default) if you are not familiar with the new OS.
The REQUIRED GCC version is 5.4.0
. You need to make sure that the version is correct. Try on your own risk if you still want another version.
Git version is not compulsory. Any version is OK if you can perform basic operations (i.e. clone, pull, push, commit, etc.)
First install Ubuntu on your computer or on a virtual machine instance in VirtualBox or VMware workstation. Use Google to help you if you do not know how to this.
If you choose to install on your physical computer for the first time, get someone to help you or follow a trustful tutorial WHENEVER you do not know how to make a choice. An arbitary choice may destroy anything important on your computer.
Then you should install Git.
If you completely know nothing of operating here, directly go on to next section 3.2. This tutorial will help you after you are familiar with some basic operations and commands.
See appendix A.2 to get help of installing git. For quick installation, just type sudo apt-get install git
and continue.
Here are some basic operations for Ubuntu.
This part is ONLY for new users. Skip it if you know everything.
The default desktop of Ubuntu is GNOME. The shortcut for opening a terminal is Ctrl+Alt+T
.
pwd
command to show which directory path you are in.ls
command to show everything in your current directory.mkdir
command to create a new directory.cd
command to change path to another directory.cat
command to show the content of a pritable file (i.e. text file).When you need to install a software, a nice choice is to check whether it is in the APT repository. Most of the handy softwares are compiled and configured in advance into binary files. You just need to pull them down to your own computer.
Once the system has been installed, the default repository is Ubuntu's main repository on ubuntu.com
. This is OK, but it is much slow when we try to pull down softwares. The best choice here is to use USTC's mirror.
On the official website, there's a tutorial. Another way is:
Now try to install cmake
:
cmake
command in the terminal. If not on the computer, cmake
command would fail, and together with which a message shows up: The program 'cmake' is currently not installed...
sudo apt-get install cmake
. Type in yes
or Y
when asked whether to continue.cmake
now. The terminal now shows the usage of cmake
.Now, if you did not install git
in 3.1 section, try to install it by yourself!
You need SSH key for Git connection. Now try to generate a new pair of keys:
(shell) $ ssh-keygen -t rsa
Then press [Enter] and wait.
Now your key is at ~/.ssh/id_rsa
(private) and ~/.ssh/id_rsa.pub
(public). Once your public key is added to the Git server, you will have priviledge to push/pull your repositories.
If you want to change the default output file, use '-f' option. See manual pages of
ssh-keygen
for details.
The command for opening the default editor is gedit
. Now try to do the following:
(shell) $ mkdir test
(shell) $ cd test
test.c
: (shell) $ gedit test.c
Now an editor frame should show up. #include <stdio.h>
int main()
{
printf("Hello, world!\n");
return 0;
}
Click 'Save' and close the window.Now you can compile the testing C source code. The entire compilation process can be done with one single command, but now we try to break down into four stages.
Try the following command to get the result of pre-processing the source code.
(shell) $ gcc -E test.c -o test.i
Now file test.i
contains the extraction of macro definitions and header files.
This stage is when GCC really compiles the code. Try:
(shell) $ gcc -S test.i -o test.s
Now the test.s contains the generated assembly instructions.
Now this stage the compiler uses an assembler to translate the assembly instructions into object code, which consists of the actual instructions that can be run by the target processor. Try:
(shell) $ gcc -c test.s -o test.o
Now the instructions are incomplete, since part of the program is missing (the printf
function). The implementation is actually in libc.so.6
file under path /usr/lib
. The compiler links it here.
(shell) $ gcc test.o -o test
Eventually, an executable binary file is here.
Use ./test
to run. Here .
means the current directory, which tells Ubuntu where to find the executable file.
If you want to start an executable by only calling its name (i.e. use
test
instead of./test
), then the directory of the executable file MUST be in the $PATH variable.
Now after section 3.2.3 you should have Git on your OS. Now we try to use it.
Git is a version control system for tracking changes in computer files and coordinating work on those files among multiple people. (from Wikipedia)
A git repository is where git manages a project of files. In most cases, a repository means a directory. This directory differs from other directories in that it keeps a hidden subdirectory '.git/', which contains the metadata and data of this repository.
We do not need to manually do operations in this hidden directory. git
command helps us with anything.
Now change directory to where you compiled the 'helloworld' program in section 3.3. Try to create a new git repository with command:
(shell) $ git init
Now the repository you've just created keeps track of nothing. That is, git do not know anything in this repository directory. Try to keep track of a file with the command:
(shell) $ git add test.c
It is supported to add a whole directory or subdirectory to git repository as well. Add everything in the current directory with:
(shell) $ git add .
Here '.' means the current directory.
Now git keeps track of everything here, and you need to commit. Commit is more like a checkpoint or a snapshot when the project evolves. Once a commit is done, the repository is able to roll back to this status in the future. What is more, git enables you to show the differences of two commits, which helps developers know which part is exactly changed between versions.
Try to commit with:
(shell) $ git commit -m "First commit"
Each time you commit, a note / message is required to comment on this commit, which helps to determine the purpose of each commit in the future.
The '-m' option helps to include the note in the command. Committing without the option will trigger the opening of a command-line editor (e.g. nano, vi) to force you to write the note.
You can add all changes and commit in one command with option '-a':
(shell) $ git commit -am "First commit"
With this, git automatically executes
git add .
and commit.
So, what is branching? It is hard to define in a few words. Totally, it means a divergent serial of work, in the form of a tree. A more detailed introduction is Here on the official website.
When the repository is created, the default branch you are on is master. Now create a new branch forked from master called adding.
(shell) $ git branch adding
Now shift to branch adding.
(shell) $ git checkout adding
Note: These two commands can be shortened as:
(shell) $ git checkout -b adding
Now make changes to file test.c
as follows:
#include <stdio.h>
int add(int x, int y)
{
return x + y;
}
int main()
{
printf("Hello, world!\n");
printf("1 + 2 = %d\n", add(1, 2));
return 0;
}
Now part of your work (the add
function) is finished. Commit them, and merge the changes to the main branch master.
(shell) $ git commit -am "add func complete"
(shell) $ git checkout master
(shell) $ git merge adding
Then master branch merges all changes in adding. Branch adding now is useless. Delete it with command:
(shell) $ git branch -d adding
Following the tutorial means now the source code has been changed. Now we can look back to history. Try:
(shell) $ git log (--oneline)
Here with '--oneline' option does not show the details. For each commit, there's a hashing ID. You can go back to any commit without doing anything to any commits behind the target commit.
(shell) $ git checkout (commitID)
After this, you can fork another branch and do something else.
If recent changes are determined to be destroyed, use:
(shell) $ git reset --hard/--soft (oldCommitID)
This reset
command will DESTROY ALL newer commits. Think before you act.
The difference for option '--hard' and '--soft' is whether git will remove all newer changes in the files. However, commits will all be destroyed.
If only one commit is considered to cancel, try
git revert
. Search online for more details.
Now finally we come to the remote repositories. What we mentioned above only works in your own LOCAL repository. How do multiple people cooperate?
Git servers hold shared repositories for multiple people to use. Users have the operation of uploading (push
) and downloading (pull
) latest changes.
It is common that you do not start from scratch. In most cases, when a repository is created on the server, you just need to clone to local.
(shell) $ git clone git@domain:/path/to/repo
For example, you can clone the tensorflow source code from Github:
(shell) $ git clone git@github.com:tensorflow/tensorflow.git
As for your own repository URL, find it on the Git server.
Now assume that you have a remote repository on a Git server, you've cloned the repository to local and made changes and committed. Just push your changes to the server.
(shell) $ git push origin master
Here, origin
is an alias for your remote URL. This is automatically set when you cloned the remote repository. The last parameter here means which branch you are working with.
Replacing push
with pull
will do the opposite. If remote repositories contain newer changes, you can pull them down.
In this preliminary project, you should complete:
git
.Official tutorial is HERE.
Chinese tutorial can be found easily, e.g. HERE
Tips:
- You do not need to download ISO file from Ubuntu official site. Use USTC's open-source software mirror instead: http://mirrors.ustc.edu.cn/
- Whenever you do not know how to make a choice, ask someone to help you. Be careful to choose your installation partition on your disk.
GCC is pre-installed in various Linux releases. In Ubuntu 16.04, the default GCC version is 5.4.0. As to Git, you need to install by yourself with the commmand sudo apt-get install git
.
The official tutorial for GCC and Git can be easily found at official websites. It is strongly recommended to download from
Building from source code may be troubling to some extent. You should first get the tarball of your desired version. The mirror that is officially recommended for Git is https://mirrors.edge.kernel.org/pub/software/scm/git/. Follow the steps below:
wget
to download the desired tarball. For example if the 2.9.5 version of Git is desired, use: (shell) $ wget https://mirrors.edge.kernel.org/pub/software/scm/git/git-2.9.5.tar.gz
./configure
to do this. If anything is required but not found, it teaches you to install.Remember to use option
--prefix=
if you do not want to install to the default location/usr/local
make
and make install
It is often the case that you need to
sudo
when installing to system paths, i.e.sudo make install
works ifmake install
doesn't.