Tutorial: Improve Step 9

* Move USE_MYMATH from configured header to target_compile_definitions
This commit is contained in:
Betsy McPhail 2019-07-18 15:40:25 -04:00 committed by Brad King
parent 6a35d630dc
commit 6f6a32a0f6
11 changed files with 22 additions and 20 deletions

View File

@ -13,6 +13,8 @@ target_include_directories(MathFunctions
option(USE_MYMATH "Use tutorial provided math implementation" ON)
if(USE_MYMATH)
target_compile_definitions(MathFunctions PRIVATE "USE_MYMATH")
# first we add the executable that generates the table
add_executable(MakeTable MakeTable.cxx)
target_link_libraries(MakeTable tutorial_compiler_flags)
@ -35,14 +37,12 @@ if(USE_MYMATH)
${CMAKE_CURRENT_BINARY_DIR}
)
# state that SqrtLibrary need PIC when the default is shared libraries
set_target_properties(SqrtLibrary PROPERTIES
POSITION_INDEPENDENT_CODE ${BUILD_SHARED_LIBS}
)
target_link_libraries(SqrtLibrary PUBLIC tutorial_compiler_flags)
target_compile_definitions(MathFunctions PRIVATE "USE_MYMATH")
target_link_libraries(MathFunctions PRIVATE SqrtLibrary)
endif()

View File

@ -13,6 +13,8 @@ target_include_directories(MathFunctions
option(USE_MYMATH "Use tutorial provided math implementation" ON)
if(USE_MYMATH)
target_compile_definitions(MathFunctions PRIVATE "USE_MYMATH")
# first we add the executable that generates the table
add_executable(MakeTable MakeTable.cxx)
@ -34,6 +36,7 @@ if(USE_MYMATH)
${CMAKE_CURRENT_BINARY_DIR}
)
# state that SqrtLibrary need PIC when the default is shared libraries
set_target_properties(SqrtLibrary PROPERTIES
POSITION_INDEPENDENT_CODE ${BUILD_SHARED_LIBS}
)
@ -41,8 +44,6 @@ if(USE_MYMATH)
target_link_libraries(MathFunctions PRIVATE SqrtLibrary)
endif()
target_compile_definitions(MathFunctions PRIVATE "$<$<BOOL:${USE_MYMATH}>:USE_MYMATH>")
# define the symbol stating we are using the declspec(dllexport) when
# building on windows
target_compile_definitions(MathFunctions PRIVATE "EXPORTING_MYMATH")

View File

@ -1,6 +1,5 @@
// A simple program that computes the square root of a number
#include <iostream>
#include <sstream>
#include <string>
#include "MathFunctions.h"

View File

@ -11,6 +11,8 @@ target_include_directories(MathFunctions
option(USE_MYMATH "Use tutorial provided math implementation" ON)
if(USE_MYMATH)
target_compile_definitions(MathFunctions PRIVATE "USE_MYMATH")
# first we add the executable that generates the table
add_executable(MakeTable MakeTable.cxx)
@ -37,8 +39,6 @@ if(USE_MYMATH)
POSITION_INDEPENDENT_CODE ${BUILD_SHARED_LIBS}
)
target_compile_definitions(MathFunctions PRIVATE "USE_MYMATH")
target_link_libraries(MathFunctions PRIVATE SqrtLibrary)
endif()

View File

@ -11,6 +11,8 @@ target_include_directories(MathFunctions
option(USE_MYMATH "Use tutorial provided math implementation" ON)
if(USE_MYMATH)
target_compile_definitions(MathFunctions PRIVATE "USE_MYMATH")
# first we add the executable that generates the table
add_executable(MakeTable MakeTable.cxx)
target_link_libraries(MakeTable tutorial_compiler_flags)
@ -33,13 +35,12 @@ if(USE_MYMATH)
${CMAKE_CURRENT_BINARY_DIR}
)
# state that SqrtLibrary need PIC when the default is shared libraries
set_target_properties(SqrtLibrary PROPERTIES
POSITION_INDEPENDENT_CODE ${BUILD_SHARED_LIBS}
)
target_link_libraries(SqrtLibrary PUBLIC tutorial_compiler_flags)
target_compile_definitions(MathFunctions PRIVATE "USE_MYMATH")
target_link_libraries(MathFunctions PRIVATE SqrtLibrary)
endif()

View File

@ -1,5 +1,4 @@
// A simple program that computes the square root of a number
#include <cmath>
#include <iostream>
#include <string>

View File

@ -10,7 +10,8 @@ set(CMAKE_CXX_STANDARD_REQUIRED True)
# should we use our own math functions
option(USE_MYMATH "Use tutorial provided math implementation" ON)
# configure a header file to pass the version number only
# configure a header file to pass some of the CMake settings
# to the source code
configure_file(TutorialConfig.h.in TutorialConfig.h)
# add the MathFunctions library

View File

@ -22,10 +22,6 @@ target_include_directories(MathFunctions
PRIVATE ${CMAKE_CURRENT_BINARY_DIR}
)
# use compile definitions to state if we have enabled USE_MYMATH
# and that anything that links to use will get this define
target_compile_definitions(MathFunctions INTERFACE "USE_MYMATH")
# install rules
install(TARGETS MathFunctions DESTINATION lib)
install(FILES MathFunctions.h DESTINATION include)

View File

@ -1,3 +1,4 @@
// the configured version number
// the configured options and settings for Tutorial
#define Tutorial_VERSION_MAJOR @Tutorial_VERSION_MAJOR@
#define Tutorial_VERSION_MINOR @Tutorial_VERSION_MINOR@
#cmakedefine USE_MYMATH

View File

@ -1,7 +1,6 @@
// A simple program that computes the square root of a number
#include <cmath>
#include <iostream>
#include <sstream>
#include <string>
#include "TutorialConfig.h"

View File

@ -645,7 +645,6 @@ The first step is to update the starting section of the top-level
.. literalinclude:: Step10/CMakeLists.txt
:language: cmake
:start-after: set(CMAKE_CXX_STANDARD 14)
:end-before: # add the binary tree
Now that we have made MathFunctions always be used, we will need to update
@ -658,7 +657,7 @@ The end result is that ``MathFunctions/CMakeLists.txt`` should look like:
.. literalinclude:: Step10/MathFunctions/CMakeLists.txt
:language: cmake
:lines: 1-40,46-
:lines: 1-36,42-
Next, update ``MathFunctions/mysqrt.cxx`` to use the ``mathfunctions`` and
``detail`` namespaces:
@ -671,6 +670,7 @@ uses ``USE_MYMATH``:
#. Always include ``MathFunctions.h``
#. Always use ``mathfunctions::sqrt``
#. Don't include cmath
Finally, update ``MathFunctions/MathFunctions.h`` to use dll export defines:
@ -683,9 +683,14 @@ library that has position enabled code. The solution to this is to explicitly
set the ``POSITION_INDEPENDENT_CODE`` target property of SqrtLibrary to be
True no matter the build type.
.. literalinclude:: Step10/MathFunctions/CMakeLists.txt
:language: cmake
:lines: 37-42
**Exercise**: We modified ``MathFunctions.h`` to use dll export defines.
Using CMake documentation can you find a helper module to simplify this?
Adding Generator Expressions (Step 10)
======================================