如何防止g ++从/ usr / include中获取头文件?

时间:2010-04-27 03:29:28

标签: g++ c-preprocessor header-files zlib

我正在使用zlib.h构建,我有一个本地副本到v1.2.5,但在/usr/include/zlib.h中有v1.2.1.2。

如果我省略在我的make中添加-I / my / path /到/ zlib,那么使用没有Z_FIXED的旧版本会出错:

g++ -g -Werror -Wredundant-decls -D_FILE_OFFSET_BITS=64 -c -o ARCH.linux_26_i86/debug/sysParam.o sysParam.cpp
sysParam.cpp: In member function `std::string CSysParamAccess::getCompressionStrategyName() const':
sysParam.cpp:1816: error: `Z_FIXED' was not declared in this scope
sysParam.cpp: In member function `bool CSysParamAccess::setCompressionStrategy(const std::string&, paramSource)':
sysParam.cpp:1849: error: `Z_FIXED' was not declared in this scope

或者,如果我将包含路径添加到我正在使用的zlib z1.2.5,我得到双重定义,好像zlib.h包含两次,带有两组不同的-D值,但我不知道看看情况如何发生:

g++ -g -Werror -Wredundant-decls -I../../src/zlib-1.2.5 -D_FILE_OFFSET_BITS=64 -c -o ARCH.linux_26_i86/debug/sysParam.o sysParam.cpp
In file included from sysParam.cpp:24:
../../src/zlib-1.2.5/zlib.h:1582: warning: redundant redeclaration of `void* gzopen64(const char*, const char*)' in same scope
../../src/zlib-1.2.5/zlib.h:1566: warning: previous declaration of `void* gzopen64(const char*, const char*)'
../../src/zlib-1.2.5/zlib.h:1583: warning: redundant redeclaration of `long long int gzseek64(void*, long long int, int)' in same scope
../../src/zlib-1.2.5/zlib.h:1567: warning: previous declaration of `off64_t gzseek64(void*, off64_t, int)'
../../src/zlib-1.2.5/zlib.h:1584: warning: redundant redeclaration of `long long int gztell64(void*)' in same scope
../../src/zlib-1.2.5/zlib.h:1568: warning: previous declaration of `off64_t gztell64(void*)'
../../src/zlib-1.2.5/zlib.h:1585: warning: redundant redeclaration of `long long int gzoffset64(void*)' in same scope
../../src/zlib-1.2.5/zlib.h:1569: warning: previous declaration of `off64_t gzoffset64(void*)'
../../src/zlib-1.2.5/zlib.h:1586: warning: redundant redeclaration of `uLong adler32_combine64(uLong, uLong, long long int)' in same scope
../../src/zlib-1.2.5/zlib.h:1570: warning: previous declaration of `uLong adler32_combine64(uLong, uLong, off64_t)'
../../src/zlib-1.2.5/zlib.h:1587: warning: redundant redeclaration of `uLong crc32_combine64(uLong, uLong, long long int)' in same scope
../../src/zlib-1.2.5/zlib.h:1571: warning: previous declaration of `uLong crc32_combine64(uLong, uLong, off64_t)'

这里有一些来自zlib.h的相关行,如上所述:

// This would be line 1558 of zlib.h
/* provide 64-bit offset functions if _LARGEFILE64_SOURCE defined, and/or
 * change the regular functions to 64 bits if _FILE_OFFSET_BITS is 64 (if
 * both are true, the application gets the *64 functions, and the regular
 * functions are changed to 64 bits) -- in case these are set on systems
 * without large file support, _LFS64_LARGEFILE must also be true
 */
#if defined(_LARGEFILE64_SOURCE) && _LFS64_LARGEFILE-0
   ZEXTERN gzFile ZEXPORT gzopen64 OF((const char *, const char *));
   ZEXTERN z_off64_t ZEXPORT gzseek64 OF((gzFile, z_off64_t, int));
   ZEXTERN z_off64_t ZEXPORT gztell64 OF((gzFile));
   ZEXTERN z_off64_t ZEXPORT gzoffset64 OF((gzFile));
   ZEXTERN uLong ZEXPORT adler32_combine64 OF((uLong, uLong, z_off64_t));
   ZEXTERN uLong ZEXPORT crc32_combine64 OF((uLong, uLong, z_off64_t));
#endif

#if !defined(ZLIB_INTERNAL) && _FILE_OFFSET_BITS-0 == 64 && _LFS64_LARGEFILE-0
#  define gzopen gzopen64
#  define gzseek gzseek64
#  define gztell gztell64
#  define gzoffset gzoffset64
#  define adler32_combine adler32_combine64
#  define crc32_combine crc32_combine64
#  ifdef _LARGEFILE64_SOURCE
     ZEXTERN gzFile ZEXPORT gzopen64 OF((const char *, const char *));
     ZEXTERN z_off_t ZEXPORT gzseek64 OF((gzFile, z_off_t, int));
     ZEXTERN z_off_t ZEXPORT gztell64 OF((gzFile));
     ZEXTERN z_off_t ZEXPORT gzoffset64 OF((gzFile));
     ZEXTERN uLong ZEXPORT adler32_combine64 OF((uLong, uLong, z_off_t));
     ZEXTERN uLong ZEXPORT crc32_combine64 OF((uLong, uLong, z_off_t));
#  endif
#else
   ZEXTERN gzFile ZEXPORT gzopen OF((const char *, const char *));
   ZEXTERN z_off_t ZEXPORT gzseek OF((gzFile, z_off_t, int));
   ZEXTERN z_off_t ZEXPORT gztell OF((gzFile));
   ZEXTERN z_off_t ZEXPORT gzoffset OF((gzFile));
   ZEXTERN uLong ZEXPORT adler32_combine OF((uLong, uLong, z_off_t));
   ZEXTERN uLong ZEXPORT crc32_combine OF((uLong, uLong, z_off_t));
#endif
// This would be line 1597 of zlib.h

我不确定如何进一步追踪这一点。我尝试将zlib.h的include包含到cpp文件包含列表的顶部和底部,但没有区别。

将-E传递给g ++的摘录部分显示:

extern int inflateInit2_ (z_streamp strm, int windowBits, const char *version, int stream_size);

extern int inflateBackInit_ (z_streamp strm, int windowBits, unsigned char *window, const char *version, int stream_size);
# 1566 "../../src/zlib-1.2.5/zlib.h"
   extern gzFile gzopen64 (const char *, const char *);
   extern off64_t gzseek64 (gzFile, off64_t, int);
   extern off64_t gztell64 (gzFile);
   extern off64_t gzoffset64 (gzFile);
   extern uLong adler32_combine64 (uLong, uLong, off64_t);
   extern uLong crc32_combine64 (uLong, uLong, off64_t);
# 1582 "../../src/zlib-1.2.5/zlib.h"
     extern gzFile gzopen64 (const char *, const char *);
     extern long long gzseek64 (gzFile, long long, int);
     extern long long gztell64 (gzFile);
     extern long long gzoffset64 (gzFile);
     extern uLong adler32_combine64 (uLong, uLong, long long);
     extern uLong crc32_combine64 (uLong, uLong, long long);
# 1600 "../../src/zlib-1.2.5/zlib.h"
    struct internal_state {int dummy;};

不确定为什么第1566行和第1582行在CPP输出中一起出现,但因此警告重复声明。

3 个答案:

答案 0 :(得分:2)

-nostdinc回答了Q标题中的问题 - 引用this manpage,意思是:

  

不要搜索标准系统   头文件的目录。只有   使用-I指定的目录   选项(和目录)   当前文件,如果合适的话)   搜索。

但是,我认为它不会解决您的实际问题,这似乎是由于同一个非系统头文件的两个互不兼容的部分被包含在内 - 感觉更可能是由于某些需要{ {1}}遗失了,但由于我不熟悉那个特定的头文件,所以我无法确切地说出来。

答案 1 :(得分:0)

我的猜测是,在某些情况下,你有:

#include <zlib.h>

而在其他人中你有

#include "zlib.h"

您将在第一种情况下找到旧的(系统)zlib.h,在第二种情况下找到新的(用户)zlib.h。

对此的修复是使用-isystem而不是-I用于新的zlib包含,即-isystem /my/path/to/zlib/includes而不是-I /my/path/to/zlib/includes

答案 2 :(得分:0)

在Solaris 5.10(64位SPARC)上编译leptonica时出现了类似的错误,我同意Alex的观点:缺少一些必要的定义或者其他东西。一时兴起,我添加了_FILE_OFFSET_BITS = 64(./configure CPPFLAGS =' - D_FILE_OFFSET_BITS = 64'),“工作”(编译)。这当然是一种货物崇拜编程,因为我不知道为什么我应该这样做。我还没有尝试过使用leptonica,所以它可能是核心或者其他什么因为我在构建时添加了这个定义。