From 67d90596b4e7127f80325badab8900b47aeb42a3 Mon Sep 17 00:00:00 2001 From: Attila Body Date: Mon, 26 May 2025 10:11:28 +0200 Subject: [PATCH 1/4] initial commit From 36bd307bb8ae72d14c5f8eda708a2a4a7ad8ec54 Mon Sep 17 00:00:00 2001 From: Attila Body Date: Mon, 26 May 2025 10:11:28 +0200 Subject: [PATCH 2/4] Initial commit --- .clang-format | 33 +++++++++++++++++++++++++++++++++ .gitignore | 2 ++ 2 files changed, 35 insertions(+) create mode 100644 .clang-format create mode 100644 .gitignore diff --git a/.clang-format b/.clang-format new file mode 100644 index 0000000..a6724a0 --- /dev/null +++ b/.clang-format @@ -0,0 +1,33 @@ +BasedOnStyle: LLVM +UseTab: Never +IndentWidth: 2 +TabWidth: 2 +BreakBeforeBraces: Custom +AllowShortFunctionsOnASingleLine: InlineOnly +AllowShortIfStatementsOnASingleLine: false +AllowShortLambdasOnASingleLine: true +AllowAllArgumentsOnNextLine: true +IndentCaseLabels: true +AccessModifierOffset: -2 +NamespaceIndentation: All +FixNamespaceComments: false +PackConstructorInitializers: Never +AlignAfterOpenBracket: AlwaysBreak +InsertBraces: true +BraceWrapping: + AfterClass: true # false + AfterControlStatement: false + AfterEnum: true # false + AfterFunction: true # false + AfterNamespace: true # false + AfterObjCDeclaration: true # false + AfterStruct: true # false + AfterUnion: true # false + AfterExternBlock: false + BeforeCatch: false + BeforeElse: false + IndentBraces: false + SplitEmptyFunction: true + SplitEmptyRecord: true + SplitEmptyNamespace: true +ColumnLimit: 140 diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..2e5015f --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +/build/ +/.cache/ \ No newline at end of file From 0c66ff89b363e5f81753d6146c999eae15eb7278 Mon Sep 17 00:00:00 2001 From: Attila Body Date: Mon, 26 May 2025 10:11:28 +0200 Subject: [PATCH 3/4] Initial commit --- .clang-format | 33 +++++++++++++++++++++++++++++++++ .gitignore | 2 ++ 2 files changed, 35 insertions(+) create mode 100644 .clang-format create mode 100644 .gitignore diff --git a/.clang-format b/.clang-format new file mode 100644 index 0000000..a6724a0 --- /dev/null +++ b/.clang-format @@ -0,0 +1,33 @@ +BasedOnStyle: LLVM +UseTab: Never +IndentWidth: 2 +TabWidth: 2 +BreakBeforeBraces: Custom +AllowShortFunctionsOnASingleLine: InlineOnly +AllowShortIfStatementsOnASingleLine: false +AllowShortLambdasOnASingleLine: true +AllowAllArgumentsOnNextLine: true +IndentCaseLabels: true +AccessModifierOffset: -2 +NamespaceIndentation: All +FixNamespaceComments: false +PackConstructorInitializers: Never +AlignAfterOpenBracket: AlwaysBreak +InsertBraces: true +BraceWrapping: + AfterClass: true # false + AfterControlStatement: false + AfterEnum: true # false + AfterFunction: true # false + AfterNamespace: true # false + AfterObjCDeclaration: true # false + AfterStruct: true # false + AfterUnion: true # false + AfterExternBlock: false + BeforeCatch: false + BeforeElse: false + IndentBraces: false + SplitEmptyFunction: true + SplitEmptyRecord: true + SplitEmptyNamespace: true +ColumnLimit: 140 diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..2e5015f --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +/build/ +/.cache/ \ No newline at end of file From 9b8308eb4a7184b4b6a426bce9f7b2bd5314d1de Mon Sep 17 00:00:00 2001 From: Attila Body Date: Mon, 26 May 2025 10:22:03 +0200 Subject: [PATCH 4/4] Working --- .vscode/settings.json | 3 +++ CMakeLists.txt | 27 +++++++++++++++++++++++++++ libs/lib1/CMakeLists.txt | 17 +++++++++++++++++ libs/lib1/inc/lib1.h | 6 ++++++ libs/lib1/src/lib1.c | 11 +++++++++++ libs/lib2/CMakeLists.txt | 10 ++++++++++ libs/lib2/inc/lib2.h | 3 +++ libs/lib2/src/lib2.c | 7 +++++++ libs/lib3/CMakeLists.txt | 13 +++++++++++++ libs/lib3/inc/lib3.h | 11 +++++++++++ src/app.c | 13 +++++++++++++ 11 files changed, 121 insertions(+) create mode 100644 .vscode/settings.json create mode 100644 CMakeLists.txt create mode 100644 libs/lib1/CMakeLists.txt create mode 100644 libs/lib1/inc/lib1.h create mode 100644 libs/lib1/src/lib1.c create mode 100644 libs/lib2/CMakeLists.txt create mode 100644 libs/lib2/inc/lib2.h create mode 100644 libs/lib2/src/lib2.c create mode 100644 libs/lib3/CMakeLists.txt create mode 100644 libs/lib3/inc/lib3.h create mode 100644 src/app.c diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..98f2d1e --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,3 @@ +{ + "sonarlint.pathToCompileCommands": "${workspaceFolder}/build/compile_commands.json" +} \ No newline at end of file diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..1fbb035 --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,27 @@ +# Specifies the minimum version of CMake required to configure the project. +cmake_minimum_required(VERSION 3.10) + +# Defines the name of the project. This name is used for various CMake targets and variables. +project(cmake_tutorial C) + +# Configures CMake to generate a 'compile_commands.json' file in the build directory. +# This file is used by many IDEs and language servers (e.g., VS Code, CLion) +# to provide intelligent code features like autocompletion, go-to-definition, and static analysis. +set(CMAKE_EXPORT_COMPILE_COMMANDS ON CACHE BOOL "Export compile commands for IDEs." FORCE) + +# Includes the CMakeLists.txt files from the specified subdirectories. +# This makes the targets (libraries) defined in those subdirectories available in the current scope. +# The order is important: libraries must be defined before they are linked. +add_subdirectory(libs/lib1) +add_subdirectory(libs/lib2) +add_subdirectory(libs/lib3) + +# Defines an executable target named 'cmake_tutorialApp' from the source file 'src/app.c'. +add_executable(cmake_tutorial src/app.c) + +# Links the executable target 'cmake_tutorialApp' to the specified libraries. +# - PRIVATE: Indicates that 'cmake_tutorialApp' needs these libraries for its own compilation/linking, +# but targets that link to 'cmake_tutorialApp' do NOT need them. +# CMake automatically propagates the PUBLIC/INTERFACE include directories from lib1, lib2, and lib3 +# to cmake_tutorialApp, so explicit target_include_directories is not needed here. +target_link_libraries(cmake_tutorial PRIVATE lib1 lib2 lib3) diff --git a/libs/lib1/CMakeLists.txt b/libs/lib1/CMakeLists.txt new file mode 100644 index 0000000..5ef34ff --- /dev/null +++ b/libs/lib1/CMakeLists.txt @@ -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) diff --git a/libs/lib1/inc/lib1.h b/libs/lib1/inc/lib1.h new file mode 100644 index 0000000..5639815 --- /dev/null +++ b/libs/lib1/inc/lib1.h @@ -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(); diff --git a/libs/lib1/src/lib1.c b/libs/lib1/src/lib1.c new file mode 100644 index 0000000..e3fad55 --- /dev/null +++ b/libs/lib1/src/lib1.c @@ -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 // 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"); +} diff --git a/libs/lib2/CMakeLists.txt b/libs/lib2/CMakeLists.txt new file mode 100644 index 0000000..67ee94e --- /dev/null +++ b/libs/lib2/CMakeLists.txt @@ -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 +) + diff --git a/libs/lib2/inc/lib2.h b/libs/lib2/inc/lib2.h new file mode 100644 index 0000000..e5b2c0f --- /dev/null +++ b/libs/lib2/inc/lib2.h @@ -0,0 +1,3 @@ +#pragma once + +void lib2_function(); // Function declaration for lib2 diff --git a/libs/lib2/src/lib2.c b/libs/lib2/src/lib2.c new file mode 100644 index 0000000..94b2209 --- /dev/null +++ b/libs/lib2/src/lib2.c @@ -0,0 +1,7 @@ +#include "lib2.h" // Include lib2's own header +#include // 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"); +} diff --git a/libs/lib3/CMakeLists.txt b/libs/lib3/CMakeLists.txt new file mode 100644 index 0000000..1bb02f7 --- /dev/null +++ b/libs/lib3/CMakeLists.txt @@ -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 +) + diff --git a/libs/lib3/inc/lib3.h b/libs/lib3/inc/lib3.h new file mode 100644 index 0000000..8065939 --- /dev/null +++ b/libs/lib3/inc/lib3.h @@ -0,0 +1,11 @@ +#pragma once +#include // 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"); +} diff --git a/src/app.c b/src/app.c new file mode 100644 index 0000000..cf11f94 --- /dev/null +++ b/src/app.c @@ -0,0 +1,13 @@ +#include "lib1.h" // Header for Library 1 (static library) +#include "lib2.h" // Header for Library 2 (static library) +#include "lib3.h" // Header for Library 3 (header-only library) +#include // Standard I/O functions like printf + +int main() { + printf("Hello from main application!\n"); + // Call functions from the linked libraries + lib1_function(); + lib2_function(); + lib3_function(); + return 0; +}