Skip to content

Commit b53aec0

Browse files
committed
fixed executable building
1 parent 1974980 commit b53aec0

File tree

5 files changed

+51
-13
lines changed

5 files changed

+51
-13
lines changed

CMakeLists.txt

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ project(
1616

1717
include(cmake/StandardSettings.cmake)
1818
include(cmake/Utils.cmake)
19-
message("\nStarted CMake for ${PROJECT_NAME} v${PROJECT_VERSION}...\n")
19+
message(STATUS "Started CMake for ${PROJECT_NAME} v${PROJECT_VERSION}...\n")
2020

2121
#
2222
# Setup alternative names
@@ -53,7 +53,8 @@ include(cmake/Vcpkg.cmake)
5353
include(cmake/SourcesAndHeaders.cmake)
5454

5555
if(${PROJECT_NAME}_BUILD_EXECUTABLE)
56-
add_executable(${PROJECT_NAME} ${sources})
56+
add_executable(${PROJECT_NAME} ${exe_sources})
57+
add_library(${PROJECT_NAME}_LIB ${headers} ${sources})
5758
elseif(${PROJECT_NAME}_BUILD_HEADERS_ONLY)
5859
add_library(${PROJECT_NAME} INTERFACE)
5960
else()
@@ -64,9 +65,15 @@ else()
6465
)
6566
endif()
6667

67-
verbose_message("Found the following header files:\n${headers}\n")
68-
verbose_message("Found the following source files:\n${sources}\n")
69-
message("Added all header and implementation files.\n")
68+
verbose_message("Found the following header files:")
69+
verbose_message(${headers})
70+
verbose_message("Found the following source files:")
71+
if(${PROJECT_NAME}_BUILD_EXECUTABLE)
72+
verbose_message(${exe_sources})
73+
else()
74+
verbose_message(${sources})
75+
endif()
76+
message(STATUS "Added all header and implementation files.\n")
7077

7178
#
7279
# Set the project standard and warnings
@@ -109,9 +116,19 @@ else()
109116
PRIVATE
110117
${CMAKE_CURRENT_SOURCE_DIR}/src
111118
)
119+
if(${PROJECT_NAME}_BUILD_EXECUTABLE)
120+
target_include_directories(
121+
${PROJECT_NAME}_LIB
122+
PUBLIC
123+
$<INSTALL_INTERFACE:include>
124+
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
125+
PRIVATE
126+
${CMAKE_CURRENT_SOURCE_DIR}/src
127+
)
128+
endif()
112129
endif()
113130

114-
message("Finished setting up include directories.")
131+
message(STATUS "Finished setting up include directories.")
115132

116133
#
117134
# Model project dependencies
@@ -254,17 +271,17 @@ if(${PROJECT_NAME}_GENERATE_EXPORT_HEADER)
254271
include
255272
)
256273

257-
message("Generated the export header `${PROJECT_NAME_LOWERCASE}_export.h` and installed it.")
274+
message(STATUS "Generated the export header `${PROJECT_NAME_LOWERCASE}_export.h` and installed it.")
258275
endif()
259276

260-
message("Finished building requirements for installing the package.\n")
277+
message(STATUS "Finished building requirements for installing the package.\n")
261278

262279
#
263280
# Unit testing setup
264281
#
265282

266283
if(${PROJECT_NAME}_ENABLE_UNIT_TESTING)
267284
enable_testing()
268-
message("Build unit tests for the project. Tests should always be found in the test folder\n")
285+
message(STATUS "Build unit tests for the project. Tests should always be found in the test folder\n")
269286
add_subdirectory(test)
270287
endif()

cmake/SourcesAndHeaders.cmake

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@ set(sources
22
src/tmp.cpp
33
)
44

5+
set(exe_sources
6+
src/main.cpp
7+
${sources}
8+
)
9+
510
set(headers
611
include/project/tmp.hpp
712
)

cmake/Utils.cmake

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
function(verbose_message content)
66
if(${PROJECT_NAME}_VERBOSE_OUTPUT)
7-
message(${MESSAGE_TYPE} ${content})
7+
message(VERBOSE ${content})
88
endif()
99
endfunction()
1010

@@ -20,6 +20,6 @@ function(add_clang_format_target)
2020
add_custom_target(clang-format
2121
COMMAND ${${PROJECT_NAME}_CLANG_FORMAT_BINARY}
2222
-i ${CMAKE_CURRENT_LIST_DIR}/${sources} ${CMAKE_CURRENT_LIST_DIR}/${headers})
23-
message("Format the project using the `clang-format` target (i.e: cmake --build build --target clang-format).\n")
23+
message(STATUS "Format the project using the `clang-format` target (i.e: cmake --build build --target clang-format).\n")
2424
endif()
2525
endfunction()

src/main.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#include <iostream>
2+
3+
#include <project/tmp.hpp>
4+
5+
int main()
6+
{
7+
std::cout << "Hello World!\n";
8+
9+
return 0;
10+
}

test/CMakeLists.txt

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,12 @@ endif()
4141
#
4242
# Currently supported: GoogleTest (and GoogleMock), Catch2.
4343

44+
if(${CMAKE_PROJECT_NAME}_BUILD_EXECUTABLE)
45+
set(${CMAKE_PROJECT_NAME}_TEST_LIB ${CMAKE_PROJECT_NAME}_LIB)
46+
else()
47+
set(${CMAKE_PROJECT_NAME}_TEST_LIB ${CMAKE_PROJECT_NAME})
48+
endif()
49+
4450
if(${CMAKE_PROJECT_NAME}_USE_GTEST)
4551
find_package(GTest REQUIRED)
4652

@@ -54,15 +60,15 @@ if(${CMAKE_PROJECT_NAME}_USE_GTEST)
5460
GTest::GTest
5561
GTest::Main
5662
${GOOGLE_MOCK_LIBRARIES}
57-
${CMAKE_PROJECT_NAME}
63+
${${CMAKE_PROJECT_NAME}_TEST_LIB}
5864
)
5965
elseif(${CMAKE_PROJECT_NAME}_USE_CATCH2)
6066
find_package(Catch2 REQUIRED)
6167
target_link_libraries(
6268
${PROJECT_NAME}
6369
PUBLIC
6470
Catch2::Catch2
65-
${CMAKE_PROJECT_NAME}
71+
${${CMAKE_PROJECT_NAME}_TEST_LIB}
6672
)
6773
else()
6874
message(FATAL_ERROR "Unknown testing library. Please setup your desired unit testing library by using `target_link_libraries`.")

0 commit comments

Comments
 (0)