默认情况下,GNU C/C++ 编译器首先在使用 -I
指定的目录中查找包含,然后在标准系统目录中查找。
如何强制编译器反转搜索顺序?
目前我调用编译器时设置了选项-nostdinc
,它表示编译器不在标准系统目录中搜索头文件。然后我使用 -I
在所有其他包含之前传递标准系统目录。
有没有更好的方法来实现它?也许是编译器选项?
更新:根据用户的要求,我在这里添加一些额外的信息以使我的问题更清楚。
我希望该图可以帮助理解设置以及我必须反转编译器的搜索路径顺序的原因。
当我尝试编译我的单元测试源文件时
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:
- For the quote form of the include directive, the directory of the current file is searched first.
- 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.- Directories specified with
-I
options are scanned in left-to-right order.- Directories specified with
-isystem
options are scanned in left-to-right order.- Standard system directories are scanned.
- Directories specified with
-idirafter
options are scanned in left-to-right order.
关于c++ - GNU C/C++ 编译器更改包括搜索顺序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43753537/