c++ - GNU C/C++ 编译器更改包括搜索顺序

标签 c++ c gcc compilation g++

默认情况下,GNU C/C++ 编译器首先在使用 -I 指定的目录中查找包含,然后在标准系统目录中查找。

如何强制编译器反转搜索顺序?

目前我调用编译器时设置了选项-nostdinc,它表示编译器不在标准系统目录中搜索头文件。然后我使用 -I 所有其他包含之前传递标准系统目录。

有没有更好的方法来实现它?也许是编译器选项?


更新:根据用户的要求,我在这里添加一些额外的信息以使我的问题更清楚。

compilation setup

我希望该图可以帮助理解设置以及我必须反转编译器的搜索路径顺序的原因。

当我尝试编译我的单元测试源文件时

CPPFLAGS          = -c -std=gnu++11 -v -g -O0 -pipe -Wall -MMD -MP -D__STDC_LIMIT_MACROS
TEST_INCLUDE_PATH = -I$(GMOCK_DIR)/include -I$(GMOCK_DIR)/gtest/include -I$(ECOS_INSTALL_DIR)/include

最后:

gcc $(CPPFLAGS) $(TEST_INCLUDE_PATH)

我遇到了很多错误。通过检查编译器的包含顺序,我注意到编译器在错误的目录中找到了一些系统头文件的头文件(在本例中是在 eCos 中)。

#include "..." search starts here:
#include <...> search starts here:
 ../../../../Common/Implementation/TestLibraries/gmock-1.7.0/include
 ../../../../Common/Implementation/TestLibraries/gmock-1.7.0/gtest/include
 ../../Implementation/Build/eCos_install/include
 /usr/lib/gcc/i686-pc-cygwin/5.4.0/include/c++
 /usr/lib/gcc/i686-pc-cygwin/5.4.0/include/c++/i686-pc-cygwin
 /usr/lib/gcc/i686-pc-cygwin/5.4.0/include/c++/backward
 /usr/lib/gcc/i686-pc-cygwin/5.4.0/include
 /usr/include
 /usr/lib/gcc/i686-pc-cygwin/5.4.0/../../../../include/w32api
End of search list.

有关信息,我的单元测试看起来是这样的:

#include "gmock/gmock.h"

class TestDummy : public ::testing::Test
{
  public:
    TestDummy() {}
};

TEST_F(TestDummy, ShouldFail)
{
    ASSERT_TRUE(false);
}


通过反转顺序,一切都可以正常编译:

CPPFLAGS            = -c -std=gnu++11 -v -g -O0 -pipe -Wall -MMD -MP -D__STDC_LIMIT_MACROS -nostdinc
SYSTEM_INCLUDE_PATH = -I$(GCC_CYGWIN_PATH)/include/c++ ....
TEST_INCLUDE_PATH = -I$(GMOCK_DIR)/include -I$(GMOCK_DIR)/gtest/include -I$(ECOS_INSTALL_DIR)/include

最后:

gcc $(CPPFLAGS) $(SYSTEM_INCLUDE_PATH) $(TEST_INCLUDE_PATH)

#include "..." search starts here:
#include <...> search starts here:
 /usr/lib/gcc/i686-pc-cygwin/5.4.0/include/c++
 /usr/lib/gcc/i686-pc-cygwin/5.4.0/include/c++/i686-pc-cygwin
 /usr/lib/gcc/i686-pc-cygwin/5.4.0/include/c++/backward
 /usr/lib/gcc/i686-pc-cygwin/5.4.0/include
 /usr/include
 /usr/lib/gcc/i686-pc-cygwin/5.4.0/../../../../include/w32api
 ../../../../Common/Implementation/TestLibraries/gmock-1.7.0/include
 ../../../../Common/Implementation/TestLibraries/gmock-1.7.0/gtest/include
 ../../Implementation/Build/HostMCU_icb_jtag_install/include
End of search list.

最佳答案

如果您希望在主机系统包含目录之后搜索 eCos 系统包含目录,请使用 -idirafter:

TEST_INCLUDE_PATH = -I$(GMOCK_DIR)/include -I$(GMOCK_DIR)/gtest/include -idirafter $(ECOS_INSTALL_DIR)/include

来自 the GCC manual, chapter 12 “Invocation :

The lookup order is as follows:

  1. For the quote form of the include directive, the directory of the current file is searched first.
  2. For the quote form of the include directive, the directories specified by -iquote options are searched in left-to-right order, as they appear on the command line.
  3. Directories specified with -I options are scanned in left-to-right order.
  4. Directories specified with -isystem options are scanned in left-to-right order.
  5. Standard system directories are scanned.
  6. Directories specified with -idirafter options are scanned in left-to-right order.

关于c++ - GNU C/C++ 编译器更改包括搜索顺序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43753537/

相关文章:

c++ - 事务模式下的SQLite字符串管理

c++ - 简化模板类的静态常量成员定义

c - 如何在大量递归时允许更多内存并避免堆栈溢出?

c++ - 为什么找不到这些符号?

c++ - 使用 boost::program_options 解析配置文件

C++将整数 vector 转换为整数

c - 创建自定义 gsource 时如何使用串行引脚?

c - 更好地理解 C 中的信号捕获(即 SIGINT/SIGQUIT)

c - 如何解决涉及重叠对的问题以及使用哪种数据结构

c - 为什么 ld 没有从共享库中找到一些符号