Creating 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
CMakeLists.txt
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
CMakeLists.txt
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.
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.
Дякуємо за ваш відгук!
Запитати АІ
Запитати АІ
Запитайте про що завгодно або спробуйте одне із запропонованих запитань, щоб почати наш чат
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?
Awesome!
Completion rate improved to 6.67
Creating 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
CMakeLists.txt
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
CMakeLists.txt
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.
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.
Дякуємо за ваш відгук!