Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Aprenda Building the Project | Creating and Building Projects
Introduction to CMake

bookBuilding the Project

After configuring your project with CMake, the next step is to build the program using the files generated by CMake. Depending on the generator you selected, CMake creates different build files, most commonly a Makefile or a build.ninja file.

Building with Make

When CMake is run with the default Unix Makefiles generator, it produces a Makefile. This file contains all the rules needed to compile and link your program.

Below is an example of a typical Makefile generated by CMake:

# CMake generated file: DO NOT EDIT!
# Generated by "Unix Makefiles" Generator, CMake Version 3.25

all: myapp

myapp: main.o
	$(CXX) $(CXX_FLAGS) -o myapp main.o

main.o: main.cpp
	$(CXX) $(CXX_FLAGS) -c main.cpp -o main.o

clean:
	rm -f myapp main.o

To build your project, open a terminal in the build directory and run:

make

This command executes the instructions in the Makefile and produces the final executable.

Building with Ninja

If you choose the Ninja generator, CMake creates a build.ninja file instead. Ninja is designed to be fast and efficient, especially for larger projects.

Here is an example of a Ninja file generated by CMake:

# CMake generated file: DO NOT EDIT!
# Generated by CMake Version 3.25

rule CXX_COMPILER
  command = g++ -o $out -c $in

rule LINKER
  command = g++ -o $out $in

build main.o: CXX_COMPILER ../main.cpp
build myapp: LINKER main.o

default myapp

To build the project using Ninja, run:

ninja

Ninja only rebuilds what is necessary, making it faster than traditional Make in many cases.

Choosing the Build Command

After configuration, building your project depends on the generator you selected:

  • Use make for Unix Makefiles;
  • Use ninja for Ninja.

Both build systems compile source files, handle dependencies, and link the final executable automatically.

Note
Note

If you experience unexpected build behavior, try cleaning your project by running make clean or deleting the build directory and re-running CMake. This removes old build artifacts and ensures a fresh build based on your current configuration.

question mark

After generating build files with CMake, which command is typically used to compile the project with Makefiles?

Select the correct answer

Tudo estava claro?

Como podemos melhorá-lo?

Obrigado pelo seu feedback!

Seção 2. Capítulo 3

Pergunte à IA

expand

Pergunte à IA

ChatGPT

Pergunte o que quiser ou experimente uma das perguntas sugeridas para iniciar nosso bate-papo

Suggested prompts:

Can you explain the differences between Make and Ninja in more detail?

How do I know which generator CMake used for my project?

What should I do if the build fails with errors?

bookBuilding the Project

Deslize para mostrar o menu

After configuring your project with CMake, the next step is to build the program using the files generated by CMake. Depending on the generator you selected, CMake creates different build files, most commonly a Makefile or a build.ninja file.

Building with Make

When CMake is run with the default Unix Makefiles generator, it produces a Makefile. This file contains all the rules needed to compile and link your program.

Below is an example of a typical Makefile generated by CMake:

# CMake generated file: DO NOT EDIT!
# Generated by "Unix Makefiles" Generator, CMake Version 3.25

all: myapp

myapp: main.o
	$(CXX) $(CXX_FLAGS) -o myapp main.o

main.o: main.cpp
	$(CXX) $(CXX_FLAGS) -c main.cpp -o main.o

clean:
	rm -f myapp main.o

To build your project, open a terminal in the build directory and run:

make

This command executes the instructions in the Makefile and produces the final executable.

Building with Ninja

If you choose the Ninja generator, CMake creates a build.ninja file instead. Ninja is designed to be fast and efficient, especially for larger projects.

Here is an example of a Ninja file generated by CMake:

# CMake generated file: DO NOT EDIT!
# Generated by CMake Version 3.25

rule CXX_COMPILER
  command = g++ -o $out -c $in

rule LINKER
  command = g++ -o $out $in

build main.o: CXX_COMPILER ../main.cpp
build myapp: LINKER main.o

default myapp

To build the project using Ninja, run:

ninja

Ninja only rebuilds what is necessary, making it faster than traditional Make in many cases.

Choosing the Build Command

After configuration, building your project depends on the generator you selected:

  • Use make for Unix Makefiles;
  • Use ninja for Ninja.

Both build systems compile source files, handle dependencies, and link the final executable automatically.

Note
Note

If you experience unexpected build behavior, try cleaning your project by running make clean or deleting the build directory and re-running CMake. This removes old build artifacts and ensures a fresh build based on your current configuration.

question mark

After generating build files with CMake, which command is typically used to compile the project with Makefiles?

Select the correct answer

Tudo estava claro?

Como podemos melhorá-lo?

Obrigado pelo seu feedback!

Seção 2. Capítulo 3
some-alt