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
ls -l
total 12 -rw-r--r-- 1 agent agent 84 Nov 10 22:03 Main.cpp -rw-r--r-- 1 agent agent 75 Nov 10 22:03 Support.cpp -rw-r--r-- 1 agent agent 26 Nov 10 22:03 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 start the project is enough to execute
g++ -std=c++23 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++ -std=c++23 -c Main.cpp Support.cpp
ls -l
total 40 -rw-r--r-- 1 agent agent 84 Nov 10 22:03 Main.cpp -rw-r--r-- 1 agent agent 1352 Nov 10 22:03 Main.o -rw-r--r-- 1 agent agent 75 Nov 10 22:03 Support.cpp -rw-r--r-- 1 agent agent 26 Nov 10 22:03 Support.h -rw-r--r-- 1 agent agent 1624 Nov 10 22:03 Support.o -rwxr-xr-x 1 agent agent 18552 Nov 10 22:03 main
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 40 -rw-r--r-- 1 agent agent 84 Nov 10 22:03 Main.cpp -rw-r--r-- 1 agent agent 1352 Nov 10 22:03 Main.o -rw-r--r-- 1 agent agent 75 Nov 10 22:03 Support.cpp -rw-r--r-- 1 agent agent 26 Nov 10 22:03 Support.h -rw-r--r-- 1 agent agent 1624 Nov 10 22:03 Support.o -rwxr-xr-x 1 agent agent 18552 Nov 10 22:03 main
Make a change to the file Support.cpp
#include <iostream> void log() { std::cout << "New Support.cpp\n"; }
You can do it again.
g++ -std=c++23 Main.cpp Support.cpp -o main
ls -l
total 40 -rw-r--r-- 1 agent agent 84 Nov 10 22:03 Main.cpp -rw-r--r-- 1 agent agent 1352 Nov 10 22:03 Main.o -rw-r--r-- 1 agent agent 71 Nov 10 22:04 Support.cpp -rw-r--r-- 1 agent agent 26 Nov 10 22:03 Support.h -rw-r--r-- 1 agent agent 1624 Nov 10 22:03 Support.o -rwxr-xr-x 1 agent agent 18552 Nov 10 22:04 main
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++ -std=c++23 -c Support.cpp
And relink
g++ Main.o Support.o -o main
./main
New Support.cpp
total 40 -rw-r--r-- 1 agent agent 84 Nov 10 22:03 Main.cpp -rw-r--r-- 1 agent agent 1352 Nov 10 22:03 Main.o -rw-r--r-- 1 agent agent 71 Nov 10 22:04 Support.cpp -rw-r--r-- 1 agent agent 26 Nov 10 22:03 Support.h -rw-r--r-- 1 agent agent 1616 Nov 10 22:04 Support.o -rwxr-xr-x 1 agent agent 18552 Nov 10 22:04 main
Please note: the update time of Support.o changed and main was relinked; Main.cpp and Main.o were not touched.
If there are a lot of files, it is useful to create them.
Makefile
Read more in the articles
The code on this page was run with g++ 15.2.0 on Alpine Linux 3.24.3 (x86_64) in a Docker container.
Article author: Andrei Olegovich