Gcc在错误的目录下搜索头文件
Gcc searches for header files under the wrong directory
我安装了两个版本的gcc/g++:gcc-4.5(从软件包管理器安装,二进制文件在/usr/bin下,头文件在/usr/include下)和gcc-4.4.3(我自己编译,放在/opt/gcc-4.4.3下)。
当我将gcc/g++4.4.3设置为默认版本(使用"更新选项"使/usr/bin/gcc和/usr/bin/g++指向目录"/opt/gcc-4.4.3/bin"下的相应版本)并编译文件时,它总是报告以下错误:
/usr/include/c++/4.5/bits/basic_string.h:1659:对的未定义引用`std::basic_string,std::分配器::_S_construct_aux_2(unsigned long,char,std::分配器常量&)'
编译器似乎试图在/usr/include/c++/4.5下查找c++的头文件,这导致了链接错误。当我将gcc的默认版本更改为gcc-4.5时,错误消失了。
那么,我如何让编译器在正确的目录"/opt/gcc-4.4.3/include"下搜索头文件呢?我曾尝试导出CPLUS_INCLUDE_PATH,但似乎不起作用。
PS:gcc-v
Using build-in specs
Target: x86_64-suse-linux
Configured with: ./configure --prefix=/opt/gcc-4.4.3
Thread model: posix
gcc version 4.4.3 (GCC)
再次尝试编译gcc 4.4.3,但在配置步骤中使用--with-gxx-include-dir=/opt/gcc-4.4.3/include
选项。
这可能是update-alternatives
做了什么或没有做什么的问题。
当我构建备用编译器时,我倾向于使用--prefix
和--program-suffix=-XY
,这样我就可以发现问题。检查哪个cpp
正在运行:
/opt/gcc-4.4.3/bin/g++ --print-prog-name=cpp
cpp -v </dev/null
/opt/gcc-4.4.3/bin/cpp -v < /dev/null
/opt/gcc-4.4.3/bin/g++ -print-search-dirs | grep '^programs:'
(您也可以用--print-prog-name
检查ld
和as
)
在构建过程中设置CPPFLAGS="-v -H
也可能有助于追踪情况
一个丑陋的解决方法可能是CPPFLAGS="-nostdinc -nostdinc++ -I/opt/gcc-4.4.3/include/"
,但最好修复编译环境,因为这可能会导致与解决的问题一样多的问题。在某些情况下,还有选项-isystem
和-sysroot
可提供帮助,请参阅http://gcc.gnu.org/onlinedocs/cpp/Invocation.html。
- 按单词搜索文件,然后在C++中打印特定行数
- 在C++目录中搜索文件的函数输出中出错
- 按扩展名过滤搜索文件会返回太多结果
- 如何向 g++ 编译器解释如何读取用于从'list'文件中搜索 .h 文件的'include'目录列表
- C++,使用自己的条件搜索文件中的字符串
- C/C++ 从根位置搜索文件夹并返回绝对路径
- 在目录中搜索文件夹
- 搜索文件的特定位置
- 按文件名搜索文件
- 使用扩展功能在整个系统中搜索文件
- 在目录中搜索文件
- 在C++中搜索文件夹
- 生成文件应该在另一个目录中搜索 .o 文件
- 有没有更好的方法来搜索文件中的字符串
- 在 c++ 中搜索文件中的字符串以查找非常大的输入的有效方法
- 如何在子目录中搜索文件
- 如何指定在整个系统中搜索文件的目录
- 如何在visualstudio中快速搜索文件内容以查找大型代码库
- C++显示include搜索文件的位置
- 在计算机中递归搜索文件