Make all linux project

Makefile для самых маленьких

Не очень строгий перевод материала mrbook.org/tutorials/make Мне в свое время очень не хватило подобной методички для понимания базовых вещей о make. Думаю, будет хоть кому-нибудь интересно. Хотя эта технология и отмирает, но все равно используется в очень многих проектах. Кармы на хаб «Переводы» не хватило, как только появится возможность — добавлю и туда. Добавил в Переводы. Если есть ошибки в оформлении, то прошу указать на них. Буду исправлять.

Статья будет интересная прежде всего изучающим программирование на C/C++ в UNIX-подобных системах от самых корней, без использования IDE.

Компилировать проект ручками — занятие весьма утомительное, особенно когда исходных файлов становится больше одного, и для каждого из них надо каждый раз набивать команды компиляции и линковки. Но не все так плохо. Сейчас мы будем учиться создавать и использовать Мейкфайлы. Makefile — это набор инструкций для программы make, которая помогает собирать программный проект буквально в одно касание.

Для практики понадобится создать микроскопический проект а-ля Hello World из четырех файлов в одном каталоге:

#include #include "functions.h" using namespace std; int main()
#include #include "functions.h" using namespace std; void print_hello()
#include "functions.h" int factorial(int n) < if(n!=1)< return(n * factorial(n-1)); >else return 1; > 
void print_hello(); int factorial(int n); 

Все скопом можно скачать отсюда
Автор использовал язык C++, знать который совсем не обязательно, и компилятор g++ из gcc. Любой другой компилятор скорее всего тоже подойдет. Файлы слегка подправлены, чтобы собирались gcc 4.7.1

Программа make

Если запустить
make
то программа попытается найти файл с именем по умолчание Makefile в текущем каталоге и выполнить инструкции из него. Если в текущем каталоге есть несколько мейкфайлов, то можно указать на нужный вот таким образом:
make -f MyMakefile
Есть еще множество других параметров, нам пока не нужных. О них можно узнать в ман-странице.

Процесс сборки

Компилятор берет файлы с исходным кодом и получает из них объектные файлы. Затем линковщик берет объектные файлы и получает из них исполняемый файл. Сборка = компиляция + линковка.

Компиляция руками

Самый простой способ собрать программу:
g++ main.cpp hello.cpp factorial.cpp -o hello
Каждый раз набирать такое неудобно, поэтому будем автоматизировать.

Самый простой Мейкфайл
цель: зависимости [tab] команда 
all: g++ main.cpp hello.cpp factorial.cpp -o hello 

Обратите внимание, что строка с командой должна начинаться с табуляции! Сохраните это под именем Makefile-1 в каталоге с проектом и запустите сборку командой make -f Makefile-1
В первом примере цель называется all . Это цель по умолчанию для мейкфайла, которая будет выполняться, если никакая другая цель не указана явно. Также у этой цели в этом примере нет никаких зависимостей, так что make сразу приступает к выполнению нужной команды. А команда в свою очередь запускает компилятор.

Использование зависимостей

Использовать несколько целей в одном мейкфайле полезно для больших проектов. Это связано с тем, что при изменении одного файла не понадобится пересобирать весь проект, а можно будет обойтись пересборкой только измененной части. Пример:

all: hello hello: main.o factorial.o hello.o g++ main.o factorial.o hello.o -o hello main.o: main.cpp g++ -c main.cpp factorial.o: factorial.cpp g++ -c factorial.cpp hello.o: hello.cpp g++ -c hello.cpp clean: rm -rf *.o hello 

Это надо сохранить под именем Makefile-2 все в том же каталоге

Читайте также:  Alsa linux debian install

Теперь у цели all есть только зависимость, но нет команды. В этом случае make при вызове последовательно выполнит все указанные в файле зависимости этой цели.
Еще добавилась новая цель clean . Она традиционно используется для быстрой очистки всех результатов сборки проекта. Очистка запускается так: make -f Makefile-2 clean

Использование переменных и комментариев

Переменные широко используются в мейкфайлах. Например, это удобный способ учесть возможность того, что проект будут собирать другим компилятором или с другими опциями.

# Это комментарий, который говорит, что переменная CC указывает компилятор, используемый для сборки CC=g++ #Это еще один комментарий. Он поясняет, что в переменной CFLAGS лежат флаги, которые передаются компилятору CFLAGS=-c -Wall all: hello hello: main.o factorial.o hello.o $(CC) main.o factorial.o hello.o -o hello main.o: main.cpp $(CC) $(CFLAGS) main.cpp factorial.o: factorial.cpp $(CC) $(CFLAGS) factorial.cpp hello.o: hello.cpp $(CC) $(CFLAGS) hello.cpp clean: rm -rf *.o hello 

Это Makefile-3
Переменные — очень удобная штука. Для их использования надо просто присвоить им значение до момента их использования. После этого можно подставлять их значение в нужное место вот таким способом: $(VAR)

Что делать дальше

После этого краткого инструктажа уже можно пробовать создавать простые мейкфайлы самостоятельно. Дальше надо читать серьезные учебники и руководства. Как финальный аккорд можно попробовать самостоятельно разобрать и осознать такой универсальный мейкфайл, который можно в два касания адаптировать под практически любой проект:

CC=g++ CFLAGS=-c -Wall LDFLAGS= SOURCES=main.cpp hello.cpp factorial.cpp OBJECTS=$(SOURCES:.cpp=.o) EXECUTABLE=hello all: $(SOURCES) $(EXECUTABLE) $(EXECUTABLE): $(OBJECTS) $(CC) $(LDFLAGS) $(OBJECTS) -o $@ .cpp.o: $(CC) $(CFLAGS) $< -o $@ 

Источник

How to Use make and makefile in Linux

make

One of the main reasons why Linux is popular among C/C++ programmers is the support provided by Linux to them. This includes the g++ compiler suite and its related tools used for program development such as the make command.

In this tutorial, you will learn about the make command in Linux, its use, the basis of the makefile, and how it is used with the make command.

Table of Contents

What is the make command?

Large and complex software applications consist of hundreds of source code and other files. Compiling and linking these files is not easy and can be erroneous. During the build process of these applications, several object files are also created. To manage these files and the entire software development project, the make command is used in Linux.

Consider the case when the programmers have their projects in simple folders. They don’t have any IDE (Integrated Development Environment) such as Eclipse or Visual Studio available to compile and handle their project. The only available option is to use the terminal to compile the source code. Instead of remembering all the commands to compile their files manually, or keeping track of files that are changed and need to be recompiled, they can simply use the make command to handle things for them automatically.

Instead of performing the compilation steps individually and remembering the file names and commands, the make command can be used to automatically perform these tasks. In short, make allows you to automatically build, compile, execute or even install complex software applications using a single command. Thus, it makes the life of a C/C++ programmer easier.

Читайте также:  Какие бывают операционные системы линукс

Makefile

The basis of the make command is a text file called Makefile. The Makefile contains the instructions regarding options passed to the compiler.

Makefile is usually created as a text file without any extension, like this

In this file, you can write a set of commands to perform various tasks. For example,

g++ main.cpp -o example_code ./example_code

You can consider Makefile as a simple bash script that contains commands to build, compile, and run your code files. Now if you enter the make command like this

it will execute the instructions (commands) written in the Makefile. In the example given above, it will first compile the main.cpp file and will create an executable file named ‘example_code’, then it will execute the ‘example_code’ file.

In this way, you can write multiple commands in the Makefile and execute them all using the simple make command.

Wait, you might be thinking that a Makefile is just like a simple bash script, so what is the big deal? No, the make command can do more than what you just thought, so keep on reading.

Structure of a Makefile

You can set up several targets in a Makefile, which you can then use with the make command. A target can be specified by writing its name followed by a colon ( : ), for example,

all: g++ main.cpp -o example_code run: ./example_code

here ‘all’ and ‘run’ are targets. By default, the make command will execute the instructions written after the first target (‘all’ in this case).

If you want to execute instructions written after a specific target, you have to specify its name, for example,

This command will run the executable ‘example_code’, as mentioned after the target ‘run’ in the above example.

File dependency

The other important thing you can specify in a Makefile is file dependency. It means that Makefile can specify the code modules which are required to build the program. In addition, it can also specify the required source code files for a particular code module. Therefore, you can use a Makefile for dependency checking.

In a Makefile, you can specify a dependency after the target name is separated by a space, like this

The above line says that the object file ‘main.o’ has a dependency on ‘main.cpp’. In other words, to execute this target the ‘main.cpp’ must exist.

Using make command to delete files

You can use the make command to delete all object and executable files created during the build process. For that, you have to create a target in the Makefile similar to the example given below.

clean: 'rm -rf *o example_code'

this target when executed (using $ make clean ) will delete all object files and the executable named ‘example_code’.

The use of variables

You can declare and use variables in a Makefile to represent values that might change. For instance, you can represent the name of the compiler using a variable like this

Читайте также:  Wine для calculate linux

Suppose you want to change the compiler (say to gcc), then you need to change only this value if you are using a variable. There would be no need to change it in all the occurrences in the Makefile because there the name of the variable is used.

You can see a variable is specified with the help of a $ sign. You can also specify options using variables, like

And then use them as shown below:

Remember before using the make command

  • The make command can be used with a simple Makefileor with complex Makefilescontaining several macros or commands. The use of macros with the make command brings application portability. It means the application can be used on other operating systems.
  • If no file is specified, the default Makefilewill be used. If you want to use your Makefilethen you have to specify its name using the -f option.
  • The make command can also be used to install a program. For that you need to specify a target in the Makefile, mentioning the program and pathname in the install command, like the one shown below:
install -g root -o root skel /usr/local/bin

Now, it is also possible to build and install a program in a single step (i.e., by executing the make command). If you want only installation, you can use the following command.

  • If you change any element of a target (such as a source code file), the make command will rebuild the target automatically.

Advantages

Here are some advantages of the make command and the Makefile, which will show you, its importance.

  • It makes the codes easier and clearer to read and removes errors from them.
  • If you make any changes in the program files, you do not need to compile them again and again. Instead, the make command will automatically compile those files only where changes have been done.
  • Makefileis also used to present a project in a more systematic, organized, and efficient way. You can divide a large application program into smaller parts and use the Makefileto handle these smaller parts in different ways.
  • Make command allows us to compile multiple files at once so that all the files can be compiled in a single step which is time-efficient as well.
  • In the case of compiling multiple files, there is no need to type the names of all the files at the command prompt. Remembering their names is difficult and typing their names can be an error-prone task. So, it is easy to write their names once in the Makefileand let the make command handle everything.

Conclusion

In this tutorial, you have studied the make command and Makefile in detail. The make command is used to manage large development projects comprising of tens of source code files. The Makefile is simply a text file that is being used by the make command to set up targets. It allows us to represent the whole project systematically and efficiently thereby making it easier and more readable to debug.

Источник

Оцените статью
Adblock
detector