I'm trying to read the Git commit hash into a C++ application. Using the following CMakeList file I get the correct commit hash.
CMakelist
FIND_PACKAGE(Git)
IF(GIT_FOUND)
EXECUTE_PROCESS(
COMMAND ${GIT_EXECUTABLE} rev-parse --short HEAD
WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}"
OUTPUT_VARIABLE COMMIT
OUTPUT_STRIP_TRAILING_WHITESPACE)
MESSAGE( STATUS "Commit : ${COMMIT}" )
ELSE(GIT_FOUND)
SET(COMMIT 0)
ENDIF(GIT_FOUND)
set(yada "${COMMIT}")
MESSAGE("lkpoopkpoef".${yada})
add_definitions("-D_GIT_COMMIT_HASH=${yada}")
However, when I try to read the value in C++ in the following function, I get an error.
main.cpp
std::string getGitCommit()
{
#ifdef _GIT_COMMIT_HASH
return _GIT_COMMIT_HASH;
#endif
return "unavailable";
}
In function ‘std::__cxx11::string getGitCommit()’: :0:18: error: ‘d2cdfd2’ was not declared in this scope
d2cdfd2 is the commit hash.
I'm referring to
- http://xit0.org/2013/04/cmake-use-git-branch-and-commit-details-in-project/
- How to read a CMake Variable in C++ source code
I'm trying to avoid creating another file to store the commit hash and would like to directly read this from the CMakeList.
I'm using catkin_make to build the application on an Ubuntu 16.04
What am I doing wrong here?