Hi, I have a C++ audio project, with some headers (.hpp) and some source (.cpp). I have a include folder and a src folder. Moreover, I think it is a good idea to have a build folder (maybe created automatically), a doc folder (for Doxygen generated pdf) and tests, for Catch2 UT. Please provide a feedback if it is a good structure and in case provide a CMakelists to build it as a library.
Assistant
Your folder structure seems well-organized for a C++ project, separating concerns and keeping things tidy. Here's an example of what your directory structure might look like:
To build your project as a library and include the other components, you can use the following CMakeLists.txt as a starting point:
Root CMakeLists.txt
cmake
cmake_minimum_required(VERSION 3.10)
project(your_project VERSION 1.0.0 LANGUAGES CXX)
# Set the C++ Standard
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
# Specify where to find the headers
include_directories(${CMAKE_SOURCE_DIR}/include)
# Add the source files
file(GLOB_RECURSE SOURCES ${CMAKE_SOURCE_DIR}/src/*.cpp)
# Add the library
add_library(your_project STATIC ${SOURCES})
# Add directories with headers
target_include_directories(your_project PUBLIC ${PROJECT_SOURCE_DIR}/include)
# Optionally, generate documentation using Doxygen
find_package(Doxygen)
if(DOXYGEN_FOUND)
add_custom_target(doc
COMMAND ${DOXYGEN_EXECUTABLE} ${PROJECT_SOURCE_DIR}/doc/Doxyfile
WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}/doc
COMMENT "Generating API documentation with Doxygen"
VERBATIM)
endif()
# Enable testing and add tests
enable_testing()
add_subdirectory(tests)
tests/CMakeLists.txt
cmake
file(GLOB TEST_SOURCES ${CMAKE_SOURCE_DIR}/tests/*.cpp)
# Add Catch2
include(FetchContent)
FetchContent_Declare(
catch
GIT_REPOSITORY https://github.com/catchorg/Catch2.git
GIT_TAG v2.x # Or any specific version of Catch2
)
FetchContent_MakeAvailable(catch)
foreach(test_source ${TEST_SOURCES})
get_filename_component(test_name ${test_source} NAME_WE)
add_executable(${test_name} ${test_source})
target_link_libraries(${test_name} PRIVATE your_project Catch2::Catch2)
add_test(NAME ${test_name} COMMAND ${test_name})
endforeach()
Using CMake:
Create the build directory:
sh
mkdir build
cd build
Run the cmake command:
sh
cmake ..
Build the project:
sh
cmake --build .
Run tests:
sh
ctest
With this setup, you can easily compile your project, generate documentation, and run unit tests using Catch2. This structure scales well and keeps your project organized. If your project grows larger, you can always further modularize it by adding more subdirectories and CMake files.