This commit is contained in:
Attila Body 2025-05-26 10:22:03 +02:00
parent 0c66ff89b3
commit 9b8308eb4a
Signed by: abody
GPG key ID: BD0C6214E68FB5CF
11 changed files with 121 additions and 0 deletions

17
libs/lib1/CMakeLists.txt Normal file
View file

@ -0,0 +1,17 @@
# Defines a static library target named 'lib1' from its source file.
add_library(lib1 STATIC src/lib1.c)
# Specifies include directories that are part of lib1's public interface.
# - PUBLIC: Means that targets linking to lib1 (like cmake_tutorialApp) will
# inherit this include path. This allows them to find lib1.h.
# ${CMAKE_CURRENT_SOURCE_DIR} refers to the current directory where this CMakeLists.txt resides.
target_include_directories(lib1 PUBLIC
${CMAKE_CURRENT_SOURCE_DIR}/inc
)
# Links lib1 to lib2 and lib3.
# - PRIVATE: Indicates that lib1's implementation depends on lib2 and lib3,
# but targets linking to lib1 do NOT need to know about lib2 or lib3.
# CMake automatically propagates the PUBLIC/INTERFACE include directories from lib2 and lib3
# to lib1, so explicit include directories for them are not needed here.
target_link_libraries(lib1 PRIVATE lib2 lib3)

6
libs/lib1/inc/lib1.h Normal file
View file

@ -0,0 +1,6 @@
#pragma once
// No need to include lib2.h or lib3.h here unless their types/functions
// are part of lib1.h's public interface. For simple calls in lib1.c,
// including in lib1.c is sufficient.
void lib1_function();

11
libs/lib1/src/lib1.c Normal file
View file

@ -0,0 +1,11 @@
#include "lib1.h" // Include lib1's own header
#include "lib2.h" // Include lib2's header to call lib2_function
#include "lib3.h" // Include lib3's header to call lib3_function
#include <stdio.h> // Include standard I/O here, as printf is used in this source file.
void lib1_function() {
printf("This is a function from Library 1.\n");
lib2_function(); // Call function from lib2
lib3_function(); // Call function from lib3
printf("Lib1 has called functions from Lib2 and Lib3.\n");
}

10
libs/lib2/CMakeLists.txt Normal file
View file

@ -0,0 +1,10 @@
# Defines a static library target named 'lib2' from its source file.
add_library(lib2 STATIC src/lib2.c)
# Specifies include directories that are part of lib2's public interface.
# - PUBLIC: Means that targets linking to lib2 (like cmake_tutorialApp or lib1) will
# inherit this include path. This allows them to find lib2.h.
target_include_directories(lib2 PUBLIC
${CMAKE_CURRENT_SOURCE_DIR}/inc
)

3
libs/lib2/inc/lib2.h Normal file
View file

@ -0,0 +1,3 @@
#pragma once
void lib2_function(); // Function declaration for lib2

7
libs/lib2/src/lib2.c Normal file
View file

@ -0,0 +1,7 @@
#include "lib2.h" // Include lib2's own header
#include <stdio.h> // Include standard I/O here, as printf is used in this source file.
void lib2_function()
{
printf("This is a function from Library 2.\n");
}

13
libs/lib3/CMakeLists.txt Normal file
View file

@ -0,0 +1,13 @@
# Defines an INTERFACE library target named 'lib3'.
# - INTERFACE: This type of library does not compile any source files itself.
# It is used purely to propagate usage requirements (like include directories)
# to targets that link to it.
add_library(lib3 INTERFACE)
# Specifies include directories that are part of lib3's interface.
# - INTERFACE: Means that targets linking to lib3 (like cmake_tutorialApp or lib1) will
# inherit this include path. This allows them to find lib3.h.
target_include_directories(lib3 INTERFACE
${CMAKE_CURRENT_SOURCE_DIR}/inc
)

11
libs/lib3/inc/lib3.h Normal file
View file

@ -0,0 +1,11 @@
#pragma once
#include <stdio.h> // Standard I/O header is needed here because printf is used within this header-only function.
// Declared as static to prevent multiple definition errors.
// Since this is a header-only function, it will be included and compiled
// into every translation unit that includes lib3.h. 'static' limits its
// linkage to the current translation unit, preventing linker conflicts.
static void lib3_function()
{
printf("This is a function from Header-Only Library 3.\n");
}