Compile a Source Package with cmake in Pisi Linux

Pisi Linux has a lot of packages in the Repositories, but the most time you need a newer version or you will have Software and it is not in the Repository.

You can search in the Playground Repository from the Developers but when you not found it what you need you must compile it from the Source Package.

Before you can start with the compiling you must install in Pisi Linux the build-essentials tools for it.

Type in a Terminal the command

sudo pisi it -c system.devel

it install all what you need for compiling and you can start now.

Download your source package and extract it now

  • Download example.tar.gz
  • Extract with: tar -xzvf example.tar.gz
    -x
    extract the file
    -z
    unzip it (required for .gz files)
    -v
    verbose, print the names of the files as they are extracted
    -f example.tar.gz
    extract the named file, rather than from /dev/rmt0 (tape device)

Read documentation

Look for files called: INSTALL, README, SETUP, README.md or similar.

In this documentation files you found the most time a description what the package need as Dependencies and how you can start the compiling.

Different Compiler commands

You have more than one command to compile a program, here any examples:

cmake ..

configure

python… and more

For the first we compile a program written in cmake:

CMake created depending on the system customized makefiles or projects based on the given by the developer in the project files information. This generated data should not be changed by the user.  Influence on the process should be done solely on the change in the corresponding CMAKE variables. Initially there are few variables that can be changed by the user sense, at the latest when cross-compile it then be significantly more.

When you now take a look in a package create with cmake it looks so

cmake-package

 

It give packages with cmake there must you create in the source package a folder called build. you see it most time when you read the Readme, README.md or the install file, then type the following in the Terminal:

mkdir build

cd build

the Terminal change with the cd command automatically the Directory, you are now in the folder build and can type

cmake ..

With a Built in a different directory and absolute paths

cmake  /Here/IS/The/Source Code
cmake only works if the target directory includes a CMakeLists.txt file. You can usually find this in the root directory of the project. If not, you should see the documentation of the project if there is to be considered something special in this project.

CMake can be adjusted very versatile. This can be done either via the command line, the graphical interface or directly manipulating the CMakeCache.txt (not recommended).

On the command line various options can be set with the help of the superior -D as example:

cmake -DCMAKE_BUILD_TYPE=Debug .. 

-DCMAKE_INSTALL_PREFIX=/usr

-DCMAKE_INSTALL_LIBDIR=/usr/lib
-DCMAKE_BUILD_TYPE=Release

List of Major Options

Most options, particular settings for compiler, CMake finds automatically. Nevertheless, there are some important options that you should know.

Important options

Variable Possible values Declaration
CMAKE_BUILD_TYPE None Debug Release RelWithDebInfo MinSizeRel Sets how the program should be built. If it is a private project, where one is still working, you should take Debug; for beta versions that you test, you should take RelWithDebInfo for finished programs usually sufficient release.
CMAKE_INSTALL_PREFIX path Required if the project is to be installed. Standard is usually / usr or / usr / local. but it can also be any other path can be specified, e.g. if you want to install in his home directory.
CMAKE_TOOLCHAIN_FILE path Reads a file with CMake settings to be added to the current project. This file can contain not only variable but also scripted sequences. It is required as example, when you make cross-compiling.

When you have type cmake .. and all is ok without any issues, you see in the root directory a Makefile

 

makefile

 

About the Makefile

make can be much more than could be described here, therefore, it follows only a brief explanation of what it is actually. In a Makefile Rules can describe how certain “target” (objectives) can be created. This may depend on other locations or files.

For example, to create an object file from a source file, by being compiled. But for the source file must of course be present. Such a rule might look like this:

hello.o: hello.c
$(CC) -c $(CFLAGS) hello.c

the first line tells us, that for creating hello.o the hello.c file is required.
The second tells you how to create hello.o is to accomplish.
Variables are preceded by $. So for instance, includes $ (CC) is usually the name of the C compiler.

Such rules can be provided even with wildcards, so you can create a rule that can be expressed, as generally from a * .c a * is to create .o file:
%.o: %.c
$(CC) -c $(CFLAGS) $<

Here is the special variable $ <is the name of the actual source file as hello.c for example.

 

CC = gcc
OBJECTS = cbg.o
LIBS = -lcurl
CFLAGS = -Wall -O2
BINDIR = $(DESTDIR)/usr/bin
NAME = cbg

cbg: $(OBJECTS)
$(CC) -o $(NAME) $(OBJECTS) $(LIBS)

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

install:
install –mode=755 $(NAME) $(BINDIR)/

clean:
rm *.o $(NAME)

uninstall:
rm $(BINDIR)/$(NAME)

It is now possible to create the individual objectives:

you can type now as next:

make

The make command build the package, when this one is ready build, type now

make install (only for the user) or sudo make install (system wide install )

You can use for make the following commands too:

 

make clean

make uninstall

 

 

Advertisements