C++ compilation option - c
Introduction
Option. -c It helps to save time and not compile what is unnecessary.
Example
There are three files. Main.cpp , Support.cpp and 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 start the project is enough to execute
g++ Main.cpp Support.cpp -o main
./main
This is Support.cpp
Compilation and linking went through, an executable file was created main a Intermediate files deleted
If the code gets big, many other dependencies will appear and compile.
It'll take a long time.
If only the file changes.
Support.cpp
It is better to recompile only it and then link with
Main.o
Consider the compilation separately. This is where we need the flag. -c
It tells the compiler that you don’t have to do everything at once.
You can first compile .cpp files into .o files.
g++ -c 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
There are two .o files. Main.o and Support.o
To get the executable file as quickly as possible, you need to execute
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
Make a change to the file Support.cpp
#include <iostream> void log() { std::cout << "New Support.cpp\n"; }
You can do it again.
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
Pay attention to the time of updating files - it has changed only in the main and Support.cpp
./main
New Support.cpp
You can manually recompile Support.cpp
g++ -c Support.cpp
And relink
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
Please note: File Update Time Main.cpp and Main.o changed
If there are a lot of files, it is useful to create them.
Makefile
Read more in the articles
Article author: Andrei Olegovich