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

bookCreating a Simple Project

Before working with CMake, you need to understand the basic structure of a CMake project. Every project requires a source file with your program code and a CMakeLists.txt file that describes how the program should be built. Let’s start with a minimal example in C.

C Project Example

This example shows the smallest working CMake project written in C. The C file prints a message to the console, and the CMake file defines how to compile the program into an executable.

Files used:

  • main.c;
  • CMakeLists.txt.
main.c

main.c

CMakeLists.txt

CMakeLists.txt

copy
123456
#include <stdio.h> int main() { printf("Hello, CMake!\n"); return 0; }

The main.c file contains a basic C program that prints "Hello, CMake!". The CMakeLists.txt file tells CMake the minimum required version, defines the project name and language, and builds an executable called hello_cmake from the source file.

C++ Project Example

CMake works the same way for C++ projects. Only the source file and language setting change. The build process stays exactly the same.

This example demonstrates how to configure a simple C++ project using the same structure as before.

Files used:

  • main.cpp;
  • CMakeLists.txt.
main.cpp

main.cpp

CMakeLists.txt

CMakeLists.txt

copy
1234567
#include <iostream> int main() { std::cout << "Hello, CMake from C++!" << std::endl; return 0; }

The main.cpp file uses C++ syntax and prints "Hello, CMake from C++!" using std::cout. The CMakeLists.txt file sets the project language to C++ (CXX) and builds an executable from the main.cpp source file.

Note
Note

Whether you are using C or C++, the project structure remains almost identical. Only the source file and language setting change, while CMake handles the build logic in the same way. This consistency makes it easy to switch between languages or manage mixed-language projects.

question mark

What is the primary role of the add_executable command in CMakeLists.txt?

Select the correct answer

Tudo estava claro?

Como podemos melhorá-lo?

Obrigado pelo seu feedback!

Seção 2. Capítulo 1

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 show me the contents of the `main.c` and `CMakeLists.txt` files?

How do I build and run the C project using CMake?

What changes are needed to switch from a C to a C++ project?

bookCreating a Simple Project

Deslize para mostrar o menu

Before working with CMake, you need to understand the basic structure of a CMake project. Every project requires a source file with your program code and a CMakeLists.txt file that describes how the program should be built. Let’s start with a minimal example in C.

C Project Example

This example shows the smallest working CMake project written in C. The C file prints a message to the console, and the CMake file defines how to compile the program into an executable.

Files used:

  • main.c;
  • CMakeLists.txt.
main.c

main.c

CMakeLists.txt

CMakeLists.txt

copy
123456
#include <stdio.h> int main() { printf("Hello, CMake!\n"); return 0; }

The main.c file contains a basic C program that prints "Hello, CMake!". The CMakeLists.txt file tells CMake the minimum required version, defines the project name and language, and builds an executable called hello_cmake from the source file.

C++ Project Example

CMake works the same way for C++ projects. Only the source file and language setting change. The build process stays exactly the same.

This example demonstrates how to configure a simple C++ project using the same structure as before.

Files used:

  • main.cpp;
  • CMakeLists.txt.
main.cpp

main.cpp

CMakeLists.txt

CMakeLists.txt

copy
1234567
#include <iostream> int main() { std::cout << "Hello, CMake from C++!" << std::endl; return 0; }

The main.cpp file uses C++ syntax and prints "Hello, CMake from C++!" using std::cout. The CMakeLists.txt file sets the project language to C++ (CXX) and builds an executable from the main.cpp source file.

Note
Note

Whether you are using C or C++, the project structure remains almost identical. Only the source file and language setting change, while CMake handles the build logic in the same way. This consistency makes it easy to switch between languages or manage mixed-language projects.

question mark

What is the primary role of the add_executable command in CMakeLists.txt?

Select the correct answer

Tudo estava claro?

Como podemos melhorá-lo?

Obrigado pelo seu feedback!

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