I have a dynamic header Version.hpp
generated by configure_file
command of cmake-3.25.
I can normally use it at the top of the source tree as is, but can NOT use it in a subdirectory of the tree.
Like this:
The top level CMakeLists.txt:
cmake_minimum_required( VERSION 3.16.0 )
set( PROJECT_VERSION_MAJOR 1 )
set( PROJECT_VERSION_MINOR 2 )
set( PROJECT_VERSION_PATCH 3 )
set( PROJECT_VERSION ${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR}.${PROJECT_VERSION_PATCH} )
set( PROJECT_DESCRIPTION "try-cmake" )
project( try-cmake VERSION ${PROJECT_VERSION} DESCRIPTION ${PROJECT_DESCRIPTION} LANGUAGES CXX )
configure_file( Version.hpp.in Version.hpp )
# include_directories( "." )
set( CMAKE_INCLUDE_CURRENT_DIR ON )
add_executable( try-cfg Main.cpp )
install( TARGETS try-cfg RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR} )
add_subdirectory( unit-test )
And the CMakeList.txt in subdirectory unit-test
:
cmake_minimum_required( VERSION 3.16.0 )
# This line does not work
include_directories( ".." )
# and this neither
set( CMAKE_INCLUDE_CURRENT_DIR ON )
add_executable( ut ut-main.cpp )
install( TARGETS ut RUNTIME DESTINATION test )
And the source Main.cpp
in the top( can be compiled):
#include <iostream>
#include <iomanip>
#include "Version.hpp"
using namespace std;
int main() {
cout << "Product Version:" << PROJECT_VERSION << endl;
};
And the source ut-main.cpp
at a subdir(can NOT be compiled):
#include <iostream>
#include <iomanip>
#include "Version.hpp"
using namespace std;
int main() {
cout << "Unit Test Version:" << PROJECT_VERSION << endl;
};
The error text:
fatal error: Version.hpp: No such file or directory
3 | #include “Version.hpp”
| ^~~~~~~~~~~~~
compilation terminated.
Thanks!