C++ компиляция опция - с
Introduction | |
Пример | |
Другие статьи о С++ |
Introduction
Опция -c помогает сэкономить время и не компилировать то, что не ненужно
Документация: -c flag directs the compiler to suppress linking with ld(1) and to produce a .o file for each source file Compile or assemble the source files, but do not link. The linking stage simply is not done. The ultimate output is in the form of an object file for each source file. By default, the object file name for a source file is made by replacing the suffix .c, .i, .s, etc., with .o. Unrecognized input files, not requiring compilation or assembly, are ignored.
Пример
Есть три файла 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"; }
Support.h
#pragma once void log();
To запустить этот проект достаточно выполнить
g++ -o main Main.cpp Support.cpp
./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++ -o main Main.o Support.o
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++ -o main Main.cpp Support.cpp
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++ -o main Main.o Support.o
./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
Подробнее читайте в статьях