Skip to content

Commit fd21431

Browse files
committed
Remove include_directories and replace with target_include_directories
also added automatic directory traversal. no cmake file needs to be modified when adding a new directory in src/
1 parent be64d81 commit fd21431

4 files changed

Lines changed: 36 additions & 15 deletions

File tree

ProjectFolder/CMakeLists.txt

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,13 @@ project(
1111

1212
include(cmake/StandardSettings.cmake)
1313

14-
set (CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR})
14+
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR})
1515

1616
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/bin")
1717
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/bin/int")
1818
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/bin/lib")
1919

20-
include_directories (${CMAKE_SOURCE_DIR}/src)
21-
22-
add_executable (${PROJECT_NAME} src/main.cpp)
20+
add_executable(${PROJECT_NAME} src/main.cpp)
2321
add_subdirectory(src)
2422
target_link_libraries(
2523
${PROJECT_NAME}

ProjectFolder/cmake/StandardSettings.cmake

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,5 @@
22
# Default settings
33
#
44
set(CMAKE_CXX_VISIBILITY_PRESET hidden)
5-
set(CMAKE_POSITION_INDEPENDENT_CODE ON)
5+
# set(CMAKE_POSITION_INDEPENDENT_CODE ON)
66
set(CMAKE_EXPORT_COMPILE_COMMANDS 1)

ProjectFolder/src/CMakeLists.txt

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,27 @@
1-
# Add any new directory that you add in ProjectFolder/src here
2-
file(GLOB_RECURSE sources utility/*.cpp)
3-
add_library(${PROJECT_NAME}_LIB ${sources})
1+
set(sources_list
2+
# Add here any file you want to add manually which is not in a
3+
# src/directory.
4+
)
5+
6+
# Getting all the .cpp files in any src/directory.
7+
file(GLOB directories */)
8+
foreach(dir ${directories})
9+
if(IS_DIRECTORY ${dir})
10+
string(FIND ${dir} "/" last_slash_pos REVERSE)
11+
math(EXPR string_start "${last_slash_pos}+1")
12+
string(SUBSTRING ${dir} ${string_start} -1 dir_stripped)
13+
file(GLOB_RECURSE sources ${dir_stripped}/*.cpp)
14+
list(APPEND sources_list ${sources})
15+
endif()
16+
endforeach()
17+
18+
add_library(${PROJECT_NAME}_LIB ${sources_list})
19+
20+
target_include_directories(
21+
${PROJECT_NAME}_LIB
22+
PUBLIC
23+
${CMAKE_SOURCE_DIR}/src
24+
)
425

526
target_compile_definitions(
627
${PROJECT_NAME}_LIB
@@ -33,7 +54,7 @@ target_link_libraries(
3354
# Add libraries to link to the binary here
3455
)
3556

36-
set_target_properties(
37-
${PROJECT_NAME}_LIB
38-
PROPERTIES ENABLE_EXPORTS ON
39-
)
57+
# set_target_properties(
58+
# ${PROJECT_NAME}_LIB
59+
# # Add any target properties here
60+
# )

README.md

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,9 @@ To run tests in a Docker container:<br>
3737

3838
<h2>Notes:</h2>
3939

40-
- When creating a new folder in ProjectFolder/src<br>
41-
also add its name in ProjectFolder/src/CMakeLists.txt
40+
- To add new files create a new folder in ProjectFolder/src,<br>
41+
no CMake files need to be modified.<br>
42+
You need to re-run "build_cmake".
4243
- To add a new test just add a new test_***.cpp file<br>
43-
in ProjectFolder/test.
44+
in ProjectFolder/test.<br>
45+
You need to re-run "build_cmake".

0 commit comments

Comments
 (0)