C++ компиляция опция - с
Introduction | |
Пример | |
Другие статьи о С++ |
Introduction
Опция -c помогает сэкономить время и не компилировать то, что не ненужно
Пример
Есть три файла Main.cpp , Support.cpp и Support.h
ll
total 15K -rw-r--r-- 1 andrei aredel 84 Sep 20 16:13 Main.cpp -rw-r--r-- 1 andrei aredel 79 Sep 20 17:05 Support.cpp -rw-r--r-- 1 andrei aredel 25 Sep 20 16:13 Support.h
Main.cpp
#include "Support.h" using namespace std; int main() { log(); return 0; }
Support.cpp
#include <iostream> void log() { std::cout << "This is Support.cpp\n"; }
Supoort.h
#pragma once void log();
To запустить этот проект достаточно выполнить
g++ Main.cpp Support.cpp -o main
./main
This is Support.cpp
Прозошла компиляция и линковка, создан исполняемый файл main а промежуточные файлы удалены
Если код станет большим, появится много других зависимостей и компилировать
всё сразу будет долго.
Поэтому, если изменится только файл
Support.cpp
выгоднее перекомпилировать только его а потом залинковать с
Main.o
Рассмотрим отдельно компиляцию. Здесь нам и пригодится флаг -c
С его помощью мы сообщаем компилятору, что не нужно делать всё и сразу
- сперва можно скомпилировать .cpp файлы в .o файлы
g++ -с Main.cpp Support.cpp
ll
total 20K -rw-r--r-- 1 andrei aredel 84 Sep 20 16:13 Main.cpp -rw-r--r-- 1 andrei aredel 1.6K Sep 20 17:15 Main.o -rw-r--r-- 1 andrei aredel 79 Sep 20 17:05 Support.cpp -rw-r--r-- 1 andrei aredel 25 Sep 20 16:13 Support.h -rw-r--r-- 1 andrei aredel 2.7K Sep 20 17:15 Support.o
Появилось два .o файла Main.o и Support.o
To максимально быстро получить исполняемый файл нужно выполнить
g++ Main.o Support.o -o main
total 40K -rwxr-xr-x 1 andrei aredel 17K Sep 20 17:17 main -rw-r--r-- 1 andrei aredel 84 Sep 20 16:13 Main.cpp -rw-r--r-- 1 andrei aredel 1.6K Sep 20 17:15 Main.o -rw-r--r-- 1 andrei aredel 79 Sep 20 17:05 Support.cpp -rw-r--r-- 1 andrei aredel 25 Sep 20 16:13 Support.h -rw-r--r-- 1 andrei aredel 2.7K Sep 20 17:15 Support.o
Внесите изменение в файл Support.cpp
#include <iostream> void log() { std::cout << "New Support.cpp\n"; }
Можно заново выполнить
g++ Main.cpp Support.cpp -o main
ll
total 40K -rwxr-xr-x 1 andrei aredel 17K Sep 20 17:20 main -rw-r--r-- 1 andrei aredel 84 Sep 20 16:13 Main.cpp -rw-r--r-- 1 andrei aredel 1.6K Sep 20 17:15 Main.o -rw-r--r-- 1 andrei aredel 79 Sep 20 17:20 Support.cpp -rw-r--r-- 1 andrei aredel 25 Sep 20 16:13 Support.h -rw-r--r-- 1 andrei aredel 2.7K Sep 20 17:15 Support.o
Обратите внимание на время обновления файлов - оно изменилось только у main и Support.cpp
./main
New Support.cpp
Вручную можно перекомпилировать Support.cpp
g++ -c Support.cpp
И перелинковать
g++ Main.o Support.o -o main
./main
New Support.cpp
total 40K -rwxr-xr-x 1 andrei aredel 17K Sep 20 17:30 main -rw-r--r-- 1 andrei aredel 84 Sep 20 16:13 Main.cpp -rw-r--r-- 1 andrei aredel 1.6K Sep 20 17:15 Main.o -rw-r--r-- 1 andrei aredel 79 Sep 20 17:30 Support.cpp -rw-r--r-- 1 andrei aredel 25 Sep 20 16:13 Support.h -rw-r--r-- 1 andrei aredel 2.7K Sep 20 17:30 Support.o
Обратите внимание - время обновления файлов Main.cpp и Main.o не изменилось
Если файлов становится много - бывает полезно создать
Makefile
Подробнее читайте в статьях