使用 Autoconf 检测另一个库中的预处理器宏

Detect preprocessor macro in another library with Autoconf

我有一个已安装的库,我们称之为 "custom_lib",我正在链接它。

我想检查该库是否使用特定选项安装。

该库中的 options.h 头文件有一个预处理器宏列表,如下所示:

#undef THIS
#define THIS

#undef THAT
#define THAT

#undef WE_WANT_THIS_ONE
#define WE_WANT_THIS_ONE

在我的 configure.ac 文件中的另一个项目中,此测试:

 OPTION_FOUND="no"
 AC_CHECK_HEADERS(custom_lib/options.h)                                             
 AC_MSG_CHECKING([is libary configured with --enable-we_want_this_one])

 #AC_EGREP_HEADER([ string to match ],                                            
 #                [ header file ],                                               
 #                [ action if found ],                                           
 #                [ action if not found ])

 AC_EGREP_HEADER([[WE_WANT_THIS_ONE]],                                                      
                 [custom_lib/options.h],                                          
                 [OPTION_FOUND="yes"],                                    
                 [OPTION_FOUND="no"])                                     

 if test "$OPTION_FOUND" = "yes"                                            
 then                                                                            
     # If we have WE_WANT_THIS_ONE check to see which compiler is being used                
     if test "$GCC" = "yes"                                                      
     then                                                                        
         if test "$CC" != "icc"                                                  
         then                                                                    
             #if compiler is not icc then add these flags                        
             CFLAGS="$CFLAGS -maes -msse4"                                       
         fi                                                                      
     fi                                                                          
     AC_MSG_RESULT([yes])                                                        
else                                                                            
     AC_MSG_RESULT([no])                                                         
fi

然后我 autreconf 和 运行 ./configure 总是 returns 这个消息:

checking custom_lib/options.h usability... yes                                     
checking custom_lib/options.h presence... yes                                      
checking for custom_lib/options.h... yes
checking is library configured with --enable-we_want_this_one... no

我是不是做错了什么。我在 configure.ac 中进行的测试需要更改哪些内容,以便 autoconf 可以检测 options.h 中的预处理器宏?

AC_EGREP_HEADER 宏不会对已测试 header 的文本执行 grep,而是对该文件的预处理器 运行 的输出执行 grep。

来自autoconf manual

— Macro: AC_EGREP_HEADER (pattern, header-file, action-if-found, [action-if-not-found])

If the output of running the preprocessor on the system header file header-file matches the extended regular expression pattern, execute shell commands action-if-found, otherwise execute action-if-not-found.

您可以改用 AC_COMPILE_IFELSE 宏。例如(未测试):

AC_COMPILE_IFELSE([AC_LANG_SOURCE([[
#include "custom_lib/options.h"
#ifndef WE_WANT_THIS_ONE
# error macro not defined
#endif
]])], [OPTION_FOUND="yes"], [OPTION_FOUND="no"])