I have a CMake Project with a macro inside my CPP file to enable some debugging code, as shown below:
main.cpp
#ifdef MY_FLAG_DEBUG
// added for debugging only
cv::imwrite("segmentation.png", img);
#endif
I defined MY_FLAG_DEBUG
inside CMakeLists.txt. Please see below:
CMakeLists.txt
option(MY_FLAG "Use MY_FLAG" OFF) # OFF by default
if(MY_FLAG)
add_definitions(-DMY_FLAG_DEBUG)
endif()
# if(MY_FLAG)
# target_compile_definitions(${PROJECT_NAME}
# PRIVATE "${PROJECT_NAME}_BUILDING_DLL"
# PRIVATE MY_FLAG_DEBUG
# )
# else()
# target_compile_definitions(${PROJECT_NAME}
# PRIVATE "${PROJECT_NAME}_BUILDING_DLL"
# )
# endif(MY_FLAG)
target_compile_definitions(${PROJECT_NAME}
PRIVATE "${PROJECT_NAME}_BUILDING_DLL"
)
It works BUT shows the following error when no command-line argument is given (MY_FLAG is unset):
CMake Warning:
Manually-specified variables were not used by the project:
MY_FLAG
The above approach declares two variables inside CMakeLists.txt to enable a macro. Moreover, it turned out that the above is an old practice, and target_compile_definitions should be preferred. I tried, but it could not work. Please see the commented lines in the CMakeLists.txt file. In addition, I found the following posts 1 and 2. Unfortunately, they did not discuss unused variables.
My goal is to enable the macro only when specified as a command-line argument.
Questions
- How to use a newer command, i.e., target_compile_definitions in this case?
- How to modify the existing approach so that CMake does not display a warning?