使用自定义标志配置Cmake
我在CPP文件中有一个带有宏的CMAKE项目,以启用一些调试代码,如下所示:
main.cpp
#ifdef MY_FLAG_DEBUG
// added for debugging only
cv::imwrite("segmentation.png", img);
#endif
我定义了my_flag_debug
cmakelists.txt。请参阅下面:
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"
)
它有效,但在没有给出命令行参数时显示以下错误(my_flag是未设置的):
CMake Warning:
Manually-specified variables were not used by the project:
MY_FLAG
上面的方法在cmakelists.txt中声明两个变量 启用宏。而且,事实证明,上述是一种旧习俗, 和 target_compile_definitions 应该是首选。我尝试过,但它无法正常工作。请参阅cmakelists.txt文件中的评论行。此外,我找到了以下帖子 1 和 2 。不幸的是,他们没有讨论未使用的变量。
我的目标是仅当指定为命令行参数时才能启用宏。
问题
- 如何在这种情况下使用较新命令,即target_compile_definitions?
- 如何修改现有方法以使CMAKE不会显示警告?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
可能的问题可能是
target_compile_definitions
命令中的错误目标名称;您使用的是$ {project_name}
而不是命名的目标。cmake Manual : em>命名目标必须由命令创建,例如
add_executable()
或add_library()
/strong>
main.cpp
输出
Probably the issue is the wrong target name in the
target_compile_definitions
command; you are using${PROJECT_NAME}
instead of the named target.The quote from cmake manual:
"The named target must have been created by a command such as
add_executable()
oradd_library()
"CMakeList.txt
main.cpp
Output
如果要使用自定义cmake标志,请致电CMake这样:
确保您以-D为前缀并给它一个值,否则CMAKE不会解析该选项
If you want to use custom CMake flags, call cmake as such:
Make sure you prefix it with -D and give it a value, otherwise CMake won't parse the option