English 中文(简体)
CMake ARGV 和宏观BUG?
原标题:Cmake ARGV and Macro BUG?
  • 时间:2012-05-23 14:35:08
  •  标签:
  • cmake

实在是太奇怪了!

macro(foo in1)
  message("FIRST" ${in1})
  message("OPtional1:" ${ARGV1})
  message("OPtional2:" ${ARGV2})
if( NOT ${ARGV1} AND ${ARGV2} )
   message("IF")  
else()
   message("ELSE")  
endif()
endmacro()

运行方式如下:

foo("gaga" false true)   

(SHOULD GIVE "IF") WORKS!

< 强 > BUT

foo("gaga" false)   

因为第二个备选论点是FALSE!

错误结果 :

cmake:126 (if):
given arguments:  "NOT" "false" "AND"
Unknown arguments specified

这是虫子吗?

下列作品:

if( NOT "${ARGV1}" AND "${ARGV2}" )
   message("IF")  
else()
   message("ELSE")  
endif()

为什么?

感谢您的帮助!

(有它工作的职能)

最佳回答

在宏中, ARGV1 等不是真实的 CMake 变量。 相反, ${ARGV1_%%/code> 是从宏调用字符串替换的字符串。 这意味着您只有一个参数的调用会扩展为

if( NOT false AND )

来解释错误消息。通过在引号中附加字符串扩展,您现在有了

if( NOT "false" AND "")

让 IF 命令一个空字符串来进行评价。 其他解决方案 : 在尝试扩展 ARGV2 之前, 使用 ${ ARGC} 字符串替换来决定参数是否存在, 或者, 如您提到的那样, 切换到函数而不是宏。 在一个函数中, ARGV1 是一个实际的 CMake 变量, 您可以更敏感地写入它, 即使没有 $ { { { { { $ { { { { } 扩展 : :

function(foo in1)
  message("Optional1: " ${ARGV1})
  message("Optional2: " ${ARGV2})
if( NOT ARGV1 AND ARGV2 )
   message("IF")  
else()
   message("ELSE")  
endif()
endfunction()

Reference: http://www.cmake.org/cmake/help/v2.8.8/cmake.html#command:macro

问题回答
if(NOT ARGV1 AND ARGV2)

if(ARGV2 AND NOT ARGV1)

逻辑否定(和大多数其他单一操作者)在交织之前相互约束。





相关问题
Why can t CLion see header files, WSL2 toolchain?

My setup: Windows 10 Pro 22H2 (19045.2965) CLion 2020.1.3 (JetBrains) WSL2 with Ubuntu 22.04.2 LTS gcc 11.3.0 cmake 3.22.1 gdb 12.1 gmake 4.3 CMakeLists.txt cmake_minimum_required(VERSION 3.22)...

CMake linking problem

I am trying to use CMake to compile a C++ application that uses the C library GStreamer. My main.cpp file looks like this: extern "C" { #include <gst/gst.h> #include <glib.h> } int main(...

How to add an extra plist property using CMake?

I m trying to add the item <key>UIStatusBarHidden</key><true/> to my plist that s auto-generated by CMake. For certain keys, it appears there are pre-defined ways to add an item; ...

(c)make - resursive compile

Let s assume I have directories like: dir1 main.cpp dir2 abc.cpp dir3 def.cpp dir4 ghi.cpp jkl.cpp And let s assume that main.cpp ...

Reading registry values with cmake

On a Windows 7 machine I cannot read any registry values that contain a semicolon. For example if you have 7-zip, running the following SET(MYPATH [HKEY_LOCAL_MACHINE\SOFTWARE\7-Zip;Path]) ...

Finding the correct Python framework with cmake

I am using the macports version of python on a Snow Leopard computer, and using cmake to build a cross-platform extension to it. I search for the python interpreter and libraries on the system using ...