Learn Linux Series (#3) - Introduction to programming

in #utopian-io6 years ago (edited)

Learn Linux Series (#3) - Introduction to programming

What Will I Learn?

  • What is the build-essential package and how to handle with it
  • How to start programming in different languages
  • Libraries and flags
  • Specialized tools

Requirements

  • Linux system installed
  • Basic knowledge of terminal operation
  • Average knowledge of linux commands
  • Motivation when something goes wrong

Difficulty

  • Intermediate

Learn Linux Series (#3) - Introduction to programming

  • build-essential package


    Wanting to program in the linux system we need to know the package "build-essential". This is a very important programming package. We need it for system programming and compilation of programs from sources. It contains things like C compiler, make program, standard C language header files. To download the package, we must execute the command:
sudo apt-get install build-essential


  • Programming in different languages

    Java

    Java focuses on object-oriented programming aimed at maximizing the efficiency of software development. To begin the adventure with Java, you need to obtain the Java Development Kit.
    It is included in the sun-java5-jdk package or sun-java6-jdk, depending on the version.

    Sample program:
public class hello
{
        public static void main (String [] args)
        {
                System.out.println ("Hello World!");
        }
}

We compile the source code with the command

javac hello.java

We will get the hello.class file containing the compiled hello class.
The program is run by the command:

java hello



Pascal


In Linux, we have two compilers to choose from: Free Pascal and gpc. Free Pascal is a cross-platform, individual compiler. The compiler is located in the repositories in the fp-compiler package.

Sample program:

program Hello;

begin
        writeln ('Hello World!')
end.

Compile with the command:

fpc hello.pas



C++


C ++ is a language developed mainly for objectivity. The main tool for working with C ++ is g ++, a C ++ compiler. The program is used very much like with gcc. A common mistake is to use gcc to compile C ++ code.
Sample prgram:

#include <iostream>

using namespace std;

int main (int argc, char *argv[])
{
        cout << "Hello World!" << endl;
        return 0;
}

Compile with the command:

g++ -Wall -o hello hello.cpp



.NET/C#


The .NET environment is Microsoft's response to Java. .NET programs, at least in theory, are independent of the platform on which they were compiled. Unlike Java, the .NET environment does not require writing code in one language, you can choose one of many, but the most common one is probably C #. The runtime environment itself is in the mono package, which should be installed with the system. You also need a C # compiler. It is in the mono-mcs and mono-gmcs packages, which differ in that mcs supports .NET version 1.1 and gmcs version 2.0. Next I will focus on gmcs. It's also good to have a mono-devel package.

Sample program:

using System;

public class hello
{
        public static void Main (string [] args)
        {
                Console.WriteLine ("Hello World!");
        }
}

Compile with the command:

gmcs hello.cs

As a result, we will get the file hello.exe, which we execute with the command:

mono hello.exe

or simply:

./hello.exe

The first method works always, the other works if we have support for binfmt_misc.
A lot of additional Mono libraries can be found in repositories in packages with names ending in -cil


C


Most Unix and Unix-like systems are written entirely in C. Also, a large part of the application software for these systems is created in this language. For programming in C, we need a compiler. The most popular is gcc, which installs with the build-essential package.
Sample program:

#include <stdio.h>

int main (int argc, char *argv[])
{
    printf ("Hello World!\n");
    return 0;
}

Compile with the command:

gcc hello.c -o hello

  • Libraries and flags


    Libraries are a collection of code ready for use by applications. When compiling programs that use libraries, you must remember that you usually need to install, besides the libraries themselves, packages with header files, static libraries, etc. in addition to attaching the appropriate header files in the sources, you must also provide the appropriate flags for the linker so that he can consolidate the output program with the appropriate libraries. In the case of gcc, this flag is -llibraryname, so for the libjpeg library listed above, the compilation of the sample program will look like:
gcc -Wall -o test test.c -ljpeg

A useful tool is the pkg-config program, which gives the necessary flags, which should be passed to the program. Using the program is simple:

pkg-config --cflags library
pkg-config --libs library

The first call returns the flags for the compiler, the second flags for the linker. Calls can be combined, as well as using the `, operator in bash, which inserts the result of one command into the second command:

gcc -Wall `pkg-config --cflags --libs gtk+-2.0` -o hellogtk hellogtk.c
  • Specialized tools

    make - a tool for building applications.

    Manually entering commands that call the compiler can be a very tiring task. Fortunately, there are tools that help in building programs. On Unix systems, the make program plays such a role. Make uses scripts, called Makefiles describing the process of building applications. Sample script:
CC=gcc
CFLAGS=-O3 -Wall
LIBS=-Wall -lm

SOURCES=
    gfx.c 
    main.c 
    menu.c 

HEADERS=
    gfx.h 
    menu.h


OBJS=$(SOURCES:.c=.o)

all: myapp

.c.o:
    $(CC) -c $(CFLAGS) $< -o $@

$(OBJS): $(HEADERS)

myapp: $(OBJS)
    $(CC) -c $(CFLAGS) $< -o $@

The first lines declare variables, respectively compiler, compiler flags, linker flags, files with sources, header files. They can be freely edited. The next one defines the OBJS variable, resulting from the SOURCES variable.



gdb - the GNU system debugger

Debugger is a tool that allows you to track the execution of the program, "preview" the values of variables and, in general, to facilitate the search for errors in the software, hence the name. In Linux, we have the gdb program, the GNU system debugger. Starting work with the debugger, you must remember that the program you want to examine should be compiled by giving the compiler the -g flag, which adds additional information needed to gdb to the output file.

Run the debugger:

gdb programname



doxygen - a tool for creating documentation

Doxygen is a tool that creates technical documentation of the program based on the comments contained in the program's code. The programmer in source files places specially formatted commentators that are read by Doxygen (doxygen package), which on the basis of this creates documentation containing a description of all functions, classes or methods of a given program, in several different formats, including HTML, PDF or man page manual pages. Document each function or class:

/**
 * brief Class representing a rectangle
 **/
class Rectangle
{
    private:
        int width;  /** < Rectangle width */
        int height; /** < Rectangle height */

    public:
        Rectnagle (int w, int h);
        int Area ();
};


/**
 * brief Rectangle constructor
 * param w Rectangle initial width
 * param h Rectangle initial height
 **/
Rectangle::Rectangle (int w, int h)
    : width (w), height (h)
{
}


/**
 * brief Compute rectnagle area
 * return Rectangle area
 **/
int Rectangle::Area ()
{
    return width * height;
}

In the main project directory, place the Doxyfile file which can be generated using doxywizard (doxywizard package). next, we execute the

doxygen

command, and in the doc directory we will find the generated documentation in various formats.

Curriculum



Posted on Utopian.io - Rewarding Open Source Contributors

Sort:  

Hey @vitusc, your contribution was rejected by the supervisor @espoem because he found out that it did not follow the Utopian rules.

Upvote this comment to help Utopian grow its power and help other Open Source contributions like this one. Do you want to chat? Join me on Discord.

Thank you for the contribution. It has been approved.

You can contact us on Discord.
[utopian-moderator]

Hey @creon, I just gave you a tip for your hard work on moderation. Upvote this comment to support the utopian moderators and increase your future rewards!

This tutorial contains mostly basic hello world constructions for several languages. Those are not related directly to Linux.

The posts format should be corrected, centering whole content does not look good and makes the code examples hard to read.

The user puts unrelated tags in their posts.

You can contact us on Discord.
[utopian-moderator]

WOW! great job

thank you for the nice opinion about my article.
I am trying to update my articles on linux very often, so I recommend being on a regular basis. Regards!

You seem to have forgotten </center> somewhere...

Coin Marketplace

STEEM 0.29
TRX 0.11
JST 0.034
BTC 66095.77
ETH 3184.92
USDT 1.00
SBD 4.12