# Sample Project Structure Let's construct a sample project with the specified folder structure and provide detailed instructions on how to build it using CMake with both GCC (MinGW) and MSVC compilers. We'll also cover how to add new files to the project. ## Project Structure ``` YourProject/ ├── src/ │ └── main.c ├── inc/ │ └── functions.h ├── api/ │ └── api.h ├── build/ │ └── ...auto-generated build files... (it's empty in the beginning) └── CMakeLists.txt ``` - **src/**: Contains `.c` source files. - **inc/**: Contains `.h` header files (assuming `.f` was a typo). - **api/**: Contains API header files (`.h` files). <img src ='https://hackmd.io/_uploads/SyrrER-fyg.png' width=70% height=70%> ## Sample Files ### src/main.c ```cpp #include <stdio.h> #include "functions.h" #include "api.h" int main() { print_message(); api_function(); return 0; } ``` ### inc/functions.h ```cpp #ifndef FUNCTIONS_H #define FUNCTIONS_H void print_message(); #endif // FUNCTIONS_H ``` ### src/functions.c ```cpp #include <stdio.h> #include "functions.h" void print_message() { printf("Hello from functions.c!\n"); } ``` ### api/api.h ```cpp #ifndef API_H #define API_H void api_function(); #endif // API_H ``` ### src/api_function.c ```cpp #include <stdio.h> #include "api.h" void api_function() { printf("Hello from api_function.c!\n"); } ``` ## CMake Configuration ### CMakeLists.txt ```cmake cmake_minimum_required(VERSION 3.0) project(YourProject) # Include directories include_directories(${PROJECT_SOURCE_DIR}/inc) include_directories(${PROJECT_SOURCE_DIR}/api) # Source files file(GLOB SRC_FILES "${PROJECT_SOURCE_DIR}/src/*.c") # Create executable add_executable(YourProject ${SRC_FILES}) ``` ## Building the Project ### Prerequisites - **CMake**: Download and install from [https://cmake.org/download/](https://cmake.org/download/) - **GCC (MinGW)** or **MSVC**: Depending on your preference - **MinWG Installation Manager**: Download and install from [https://sourceforge.net/projects/mingw/](https://sourceforge.net/projects/mingw/) ### Build Steps 1. **Open a terminal or command prompt.** 2. **Navigate to the project directory:** ```sh cd path/to/YourProject ``` 3. **Create a build directory:** ```sh mkdir build cd build ``` 4. **Generate build files with CMake:** - **For GCC (MinGW):** ```sh cmake -G "MinGW Makefiles" .. ``` - **For MSVC:** ```sh cmake -G "Visual Studio 17 2022" .. ``` - Replace `"Visual Studio 17 2022"` with your version of Visual Studio. 5. **Build the project:** - **For GCC (MinGW):** ```sh mingw32-make ``` <img src = 'https://hackmd.io/_uploads/HyIgBCWMkx.png' width=80% height=80%> - **For MSVC (from Developer Command Prompt):** ```sh msbuild YourProject.sln ``` 6. **Run the executable:** - The executable will be located in the `build` directory. ```sh ./YourProject ``` **Output:** ``` Hello from functions.c! Hello from api_function.c! ``` ## Adding New Files to the Project ### Adding a New Source File 1. **Create a new `.c` file in the `src/` directory:** - Example: `src/new_feature.c` ```cpp #include <stdio.h> #include "new_feature.h" void new_feature() { printf("This is a new feature!\n"); } ``` 2. **Create a corresponding header file in `inc/` or `api/`:** - Example: `inc/new_feature.h` ```cpp #ifndef NEW_FEATURE_H #define NEW_FEATURE_H void new_feature(); #endif // NEW_FEATURE_H ``` 3. **Include the header in your `main.c` or any other source file where you want to use it:** ```cpp #include "new_feature.h" // ... int main() { // ... new_feature(); return 0; } ``` 4. **No need to modify `CMakeLists.txt`** if you're adding files within the `src/` directory, as it automatically includes all `.c` files in `src/` due to the `file(GLOB ...)` command. ### Rebuilding the Project 1. **Navigate to the build directory:** ```sh cd path/to/YourProject/build ``` 2. **Re-run the build command:** - **For GCC (MinGW):** ```sh mingw32-make ``` - **For MSVC:** ```sh msbuild YourProject.sln ``` 3. **Run the updated executable:** ```sh ./YourProject ``` **Updated Output:** ```shell Hello from functions.c! Hello from api_function.c! This is a new feature! ``` ## Explanation of CMakeLists.txt - **cmake_minimum_required(VERSION 3.0)**: Specifies the minimum version of CMake required. - **project(YourProject)**: Defines the project name. - **include_directories(...)**: Adds directories to the compiler's include path. - **file(GLOB ...)**: Collects all `.c` files in `src/` into the `SRC_FILES` variable. - **add_executable(...)**: Creates an executable from the specified source files. ## Tips - **Updating CMakeLists.txt**: If you add source files outside the `src/` directory or use different extensions, update `CMakeLists.txt` accordingly. - **Cleaning the Build**: To clean the build, delete the `build/` directory and start fresh. - **Using IDEs**: You can open the generated solution file (`YourProject.sln`) in Visual Studio when using MSVC. ## Conclusion You now have a sample project structure with `src`, `inc`, and `api` directories, along with a `CMakeLists.txt` file to build the project using GCC (MinGW) or MSVC compilers. By following the steps above, you can build the project and add new files as needed.