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

Подробнее читайте в статьях

Article author: Andrei Olegovich

Похожие статьи
Development на C++
Перегрузка функций
Разбиение кода на части
Вектор
Указатели
Классы
SFML
Тетрис на C++ с библиотекой SFML2
SDL
Массив Структур
Как узнать тип переменной C++
Решение задач на C++
Как создать пустую строку в C++
Errors C++
Make

Search this site

Subscribe to Telegram channel @aofeed to follow new articles and updates to old ones

Go to channel

@aofeed

Ask a question in the Telegram group

@aofeedchat

Contacts and cooperation:
I recommend our hosting beget.ru
Write to info@urn.su if you:
1. Want to write an article for our site or translate an article into your native language.
2. Want to place thematically relevant ads on the site.
3. Ads on my site pass maximum censorship. If you see an ad block unsuitable for school-age children, shocking or misleading - please contact us by e-mail
4. Found a mistake, inaccuracy, bug, etc. on the site. ... .......
5. Articles can be shared on social media by clicking a network icon: