宏在GCC工作,但在Solaris编译器失败

Macro in GCC works but in Solaris Compiler fails?

本文关键字:Solaris 编译器 失败 但在 GCC 工作 宏在      更新时间:2023-10-16

source: svn checkout svn://dev.exiv2.org/svn/trunk(最新版本:3020)

我的平台:Fedora 17 64位

下面的命令可以工作:

cmake -DCMAKE_CXX_FLAGS=-library=stlport4 -    
DCMAKE_CXX_COMPILER=/opt/oracle/solarisstudio12.3/bin/CC -        
DCMAKE_C_COMPILER=/opt/oracle/solarisstudio12.3/bin/cc .

但是之后当我做的时候,我得到了错误:

Scanning dependencies of target exiv2lib
[ 17%] Building CXX object src/CMakeFiles/exiv2lib.dir/asfvideo.cpp.o
cd /home/Wani/GSoC/exiv2-trunk/trunk/src && /opt/oracle/solarisstudio12.3/bin/CC   -    
DEXV_BUILDING_LIB -DEXV_HAVE_DLL -DEXV_LOCALEDIR="/usr/local/share/locale" -
DEXV_HAVE_STDINT_H -library=stlport4 -KPIC -I/home/Wani/GSoC/exiv2-trunk/trunk -
I/home/Wani/GSoC/exiv2-trunk/trunk/xmpsdk/include    -o 
CMakeFiles/exiv2lib.dir/asfvideo.cpp.o -c /home/Wani/GSoC/exiv2-
trunk/trunk/src/asfvideo.cpp
"/home/Wani/GSoC/exiv2-trunk/trunk/src/error.cpp", line 29: Error: Multiple declaration 
for rcsId.
1 Error(s) detected.

error.cpp的内容:

28 #include "rcsid_int.hpp"
29 EXIV2_RCSID("@(#) $Id: error.cpp 2681 2012-03-22 15:19:35Z ahuggel $")

rcsid_int.hpp:

内容
#ifndef RCSID_INT_HPP_
#define RCSID_INT_HPP_
#if !defined (EXIV2_RCSID)
#if defined(__clang__)
#define EXIV2_RCSID(id)
#elif defined(OS_SOLARIS)
#define EXIV2_RCSID(id) 
{ 
inline const char* getRcsId(const char*) { return id ; } 
const char* rcsId = getRcsId(rcsId); 
}
#else
#define EXIV2_RCSID(id) 
namespace { 
inline const char* getRcsId(const char*) { return id ; } 
const char* rcsId = getRcsId(rcsId); 
}
#endif
#endif 
#endif

如果我使用GCC编译相同的程序,它可以正常工作。

查看差异,因为Rev 3019在GCC和Solaris编译器中工作:http://dev.exiv2.org/projects/exiv2/repository/revisions/3020/diff?rev=3020&type=sbs

如何忽略Solaris编译器中的多个声明错误?

我已经计算了r3018和r3019中.cpp文件的预处理输出的差异:

 2a3,6
 > #30 "/home/Wani/exiv2-trunk/trunk/src/asfvideo.cpp"
 > namespace { inline const char * getRcsId ( const char * ) { return "@(#) $Id$" ; }           
 const char * rcsId = getRcsId ( rcsId 
 > #30
 > ) ; } 

我在exiv2.org的wiki上发表了关于这个问题的评论,并提供了补丁解决了这个问题。

关键问题是Solaris Studio编译器不允许多行c++中的宏。我提供的补丁省略了这一点,因为它注释掉了完整的rcsId声明。

据我所知,rcsid_int.hpp中的正确声明如下:

#if defined(__clang__) || (defined(OS_SOLARIS) && defined(__SUNPRO_CC))
#define EXIV2_RCSID(id)
#else
#define EXIV2_RCSID(id) 
    namespace { 
        inline const char* getRcsId(const char*) { return id ; } 
        const char* rcsId = getRcsId(rcsId); 
    }
#endif

这将导致预处理的源而不是包含rcsId字符串。