Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Вивчайте Adding Multiple Sources | Project Structure and Organization
Quizzes & Challenges
Quizzes
Challenges
/
Introduction to CMake

bookAdding Multiple Sources

As your project grows, it is common to split your code into multiple files to keep it organized and easier to maintain. In CMake, you add all the source files that belong to your program inside the add_executable command.

In this example, main.c contains the program entry point, while helper.c contains supporting functions. Both files are compiled and linked into a single executable by CMake.

main.c

main.c

helper.c

helper.c

helper.h

helper.h

CMakeLists.txt

CMakeLists.txt

copy
1234567
#include <stdio.h> #include "helper.h" int main() { print_message(); return 0; }

When working with multiple source files, functions shared between files must be declared in header files. In this example, the function print_message() is declared in helper.h and implemented in helper.c. Including the header file allows main.c to use the function correctly.

In CMakeLists.txt, you list every source file that should be compiled:

  • CMake compiles each .c or .cpp file separately.
  • The linker combines them into one executable.
  • Header files are not compiled but tell the compiler how functions and data are defined.

CMake handles the build process automatically, but it is your responsibility to manage function declarations using header files.

question mark

How do you specify multiple source files for an executable in CMakeLists.txt?

Select the correct answer

Все було зрозуміло?

Як ми можемо покращити це?

Дякуємо за ваш відгук!

Секція 3. Розділ 2

Запитати АІ

expand

Запитати АІ

ChatGPT

Запитайте про що завгодно або спробуйте одне із запропонованих запитань, щоб почати наш чат

Suggested prompts:

Can you show me an example of how to declare and use a function in a header file?

What should I do if I have more than two source files?

How do I organize my header and source files in a larger project?

bookAdding Multiple Sources

Свайпніть щоб показати меню

As your project grows, it is common to split your code into multiple files to keep it organized and easier to maintain. In CMake, you add all the source files that belong to your program inside the add_executable command.

In this example, main.c contains the program entry point, while helper.c contains supporting functions. Both files are compiled and linked into a single executable by CMake.

main.c

main.c

helper.c

helper.c

helper.h

helper.h

CMakeLists.txt

CMakeLists.txt

copy
1234567
#include <stdio.h> #include "helper.h" int main() { print_message(); return 0; }

When working with multiple source files, functions shared between files must be declared in header files. In this example, the function print_message() is declared in helper.h and implemented in helper.c. Including the header file allows main.c to use the function correctly.

In CMakeLists.txt, you list every source file that should be compiled:

  • CMake compiles each .c or .cpp file separately.
  • The linker combines them into one executable.
  • Header files are not compiled but tell the compiler how functions and data are defined.

CMake handles the build process automatically, but it is your responsibility to manage function declarations using header files.

question mark

How do you specify multiple source files for an executable in CMakeLists.txt?

Select the correct answer

Все було зрозуміло?

Як ми можемо покращити це?

Дякуємо за ваш відгук!

Секція 3. Розділ 2
some-alt