常见gcc编译警告整理以及解决方法【收藏】

合集下载

gcc错误解决方法

gcc错误解决方法

原因:因为安装其他软件,升级gcc相关的软件,造成gcc编译错误,说没有gcc,解决方法,用系统盘重新安装gcc。

具体方法如下:[root@mail /]# rpm -e --nodeps cpp[root@mail /]# rpm -qa | grep gcclibgcc-4.1.2-48.el5gcc-java-4.1.2-48.el5libgcc-4.1.2-48.el5gcc-c++-4.1.2-48.el5gcc-gfortran-4.1.2-48.el5[root@mail /]# rpm -ivh /root/rpm/cpp-4.1.2-46.el5.x86_64.rpmPreparing... ########################################### [100%] 1:cpp ########################################### [100%][root@mail /]# rpm -ivh /root/rpm/gcc-4.1.2-46.el5.x86_64.rpmPreparing... ########################################### [100%] 1:gcc ########################################### [100%][root@mail /]# rpm -qa | grep kernel-headerskernel-headers-2.6.18-194.26.1.el5[root@mail /]# rpm -e --nodeps kernel-headers[root@mail /]# rpm -qa | grep kernel-headers[root@mail /]# rpm -ivh /root/rpm/kernel-headers-2.6.18-164.el5.x86_64.rpmPreparing... ########################################### [100%] 1:kernel-headers ########################################### [100%][root@mail /]# rpm -qa | grep glibc-headersglibc-headers-2.5-42[root@mail /]# rpm -qa | grep glibc-headersglibc-headers-2.5-42[root@mail /]# rpm -e --nodeps glibc-headers[root@mail /]# rpm -ivh /root/rpm/glibc-headers-2.5-42.x86_64.rpmPreparing... ########################################### [100%] 1:glibc-headers ########################################### [100%][root@mail /]# rpm -qa | grep glibc-develglibc-devel-2.5-42glibc-devel-2.5-42[root@mail /]# rpm -e --nodeps glibc-develerror: "glibc-devel" specifies multiple packages[root@mail /]# rpm -q --queryformat "%{NAME}-%{VERSION}-%{RELEASE} (%{ARCH})\n" glibc-devel glibc-devel-2.5-42 (i386)glibc-devel-2.5-42 (x86_64)[root@mail /]# rpm -e --nodeps glibc-devel*error: package glibc-devel* is not installed[root@mail /]# rpm -e --nodeps glibc-devel-2.5-42 (i386)-bash: syntax error near unexpected token `('[root@mail /]# rpm -e --nodeps glibc-devel-2.5-42 \(i386\)error: "glibc-devel-2.5-42" specifies multiple packageserror: package (i386) is not installed[root@mail /]# rpm -e --allmatches glibc-develerror: Failed dependencies:glibc-devel >= 2.2.90-12 is needed by (installed) gcc-4.1.2-46.el5.x86_64[root@mail /]# rpm -qa | grep gcclibgcc-4.1.2-48.el5gcc-java-4.1.2-48.el5libgcc-4.1.2-48.el5gcc-c++-4.1.2-48.el5gcc-gfortran-4.1.2-48.el5gcc-4.1.2-46.el5[root@mail /]# rpm -qa | grep glibc-develglibc-devel-2.5-42glibc-devel-2.5-42[root@mail /]# rpm -e --allmatches --nodeps glibc-devel[root@mail /]# rpm -qa | grep glibc-devel[root@mail /]# rpm -ivh /root/rpm/glibc-devel-2.5-42.i386.rpmerror: Failed dependencies:binutils < 2.15.94.0.2-1 conflicts with glibc-devel-2.5-42.i386[root@mail /]# rpm -ivh /root/rpm/glibc-devel-2.5-42.x86_64.rpmerror: Failed dependencies:binutils < 2.15.94.0.2-1 conflicts with glibc-devel-2.5-42.x86_64[root@mail /]# rpm -qa | grep binutilsbinutils-2.15.92.0.2-13.0.0.0.2binutils-2.17.50.0.6-14.el5[root@mail /]# rpm -q --queryformat "%{NAME}-%{VERSION}-%{RELEASE} (%{ARCH})\n" binutils binutils-2.17.50.0.6-14.el5 (x86_64)binutils-2.15.92.0.2-13.0.0.0.2 (x86_64)[root@mail /]# rpm -e --allmatches --nodeps binutils/sbin/ldconfig: libraries libdb.so.2 and libgdbm.so.2.0.0 in directory /usr/lib have same soname but different type. /sbin/ldconfig: libraries libdb.so.2 and libgdbm.so.2.0.0 in directory /usr/lib have same soname but different type. [root@mail /]# rpm -q --queryformat "%{NAME}-%{VERSION}-%{RELEASE} (%{ARCH})\n" binutils package binutils is not installed[root@mail /]# rpm -ivh /root/rpm/binutils-2.17.50.0.6-12.el5.x86_64.rpmPreparing... ########################################### [100%] 1:binutils ########################################### [100%]/sbin/ldconfig: libraries libdb.so.2 and libgdbm.so.2.0.0 in directory /usr/lib have same soname but different type. [root@mail /]# rpm -ivh /root/rpm/glibc-devel-2.5-42.*Preparing... ########################################### [100%] 1:glibc-devel ########################################### [ 50%]2:glibc-devel ########################################### [100%][root@mail /]# rpm -qa | grep libgomplibgomp-4.4.0-6.el5[root@mail /]# rpm -e --nodeps libgomp/sbin/ldconfig: libraries libdb.so.2 and libgdbm.so.2.0.0 in directory /usr/lib have same soname but different type. [root@mail /]# rpm -qa | grep libgomp[root@mail /]# rpm -qa | grep libgomp*[root@mail /]# rpm -qa | grep libstdc++-devellibstdc++-devel-4.1.2-48.el5[root@mail /]# rpm -e --nodeps libstdc++-devel[root@mail /]# rpm -ivh /root/rpm/libstdc++-devel*error: Failed dependencies:libstdc++ = 4.1.2-46.el5 is needed by libstdc++-devel-4.1.2-46.el5.i386libstdc++ = 4.1.2-46.el5 is needed by libstdc++-devel-4.1.2-46.el5.x86_64[root@mail /]# rpm -qa | grep libstdc++libstdc++-4.1.2-48.el5compat-libstdc++-33-3.2.3-61compat-libstdc++-296-2.96-138libstdc++-4.1.2-48.el5compat-libstdc++-33-3.2.3-61[root@mail /]# rpm -ivh /root/rpm/libstdc++-devel-4.1.2-46.el5.x86_64.rpmerror: Failed dependencies:libstdc++ = 4.1.2-46.el5 is needed by libstdc++-devel-4.1.2-46.el5.x86_64[root@mail /]# rpm -e --nodeps libstdc++error: "libstdc++" specifies multiple packages[root@mail /]# rpm -e --nodeps --allmatches libstdc++/sbin/ldconfig: libraries libdb.so.2 and libgdbm.so.2.0.0 in directory /usr/lib have same soname but different type. /sbin/ldconfig: libraries libdb.so.2 and libgdbm.so.2.0.0 in directory /usr/lib have same soname but different type. [root@mail /]# rpm -ivh /root/rpm/libstdc++*Preparing... ########################################### [100%] 1:libstdc++ ########################################### [ 25%]/sbin/ldconfig: libraries libdb.so.2 and libgdbm.so.2.0.0 in directory /usr/lib have same soname but different type.2:libstdc++ ########################################### [ 50%]/sbin/ldconfig: libraries libdb.so.2 and libgdbm.so.2.0.0 in directory /usr/lib have same soname but different type.3:libstdc++-devel ########################################### [ 75%]4:libstdc++-devel ########################################### [100%][root@mail /]# rpm -ivh /root/rpm/compat-libstdc++-compat-libstdc++-296-2.96-138.i386.rpm compat-libstdc++-33-3.2.3-61.i386.rpm compat-libstdc++-33-3.2.3-61.x86_64.rpm[root@mail /]# rpm -ivh /root/rpm/compat-libstdc++-compat-libstdc++-296-2.96-138.i386.rpm compat-libstdc++-33-3.2.3-61.i386.rpm compat-libstdc++-33-3.2.3-61.x86_64.rpm[root@mail /]# rpm -ivh /root/rpm/compat-libstdc++-*Preparing... ########################################### [100%]package compat-libstdc++-33-3.2.3-61.x86_64 is already installedpackage compat-libstdc++-296-2.96-138.i386 is already installedpackage compat-libstdc++-33-3.2.3-61.i386 is already installed[root@mail /]# rpm -ivh /root/rpm/gcc-c++-4.1.2-46.el5.x86_64.rpmPreparing... ########################################### [100%]package gcc-c++-4.1.2-48.el5.x86_64 (which is newer than gcc-c++-4.1.2-46.el5.x86_64) is already installedfile /usr/bin/c++ from install of gcc-c++-4.1.2-46.el5.x86_64 conflicts with file from package gcc-c++-4.1.2-48.el5.x86_64file /usr/bin/g++ from install of gcc-c++-4.1.2-46.el5.x86_64 conflicts with file from package gcc-c++-4.1.2-48.el5.x86_64file /usr/bin/x86_64-redhat-linux-c++ from install of gcc-c++-4.1.2-46.el5.x86_64 conflicts with file from package gcc-c++-4.1.2-48.el5.x86_64file /usr/bin/x86_64-redhat-linux-g++ from install of gcc-c++-4.1.2-46.el5.x86_64 conflicts with file from package gcc-c++-4.1.2-48.el5.x86_64file /usr/lib/gcc/x86_64-redhat-linux/4.1.1/32/libsupc++.a from install of gcc-c++-4.1.2-46.el5.x86_64 conflicts with file from package gcc-c++-4.1.2-48.el5.x86_64file /usr/lib/gcc/x86_64-redhat-linux/4.1.1/libsupc++.a from install of gcc-c++-4.1.2-46.el5.x86_64 conflicts with file from package gcc-c++-4.1.2-48.el5.x86_64file /usr/libexec/gcc/x86_64-redhat-linux/4.1.1/cc1plus from install of gcc-c++-4.1.2-46.el5.x86_64 conflicts with file from package gcc-c++-4.1.2-48.el5.x86_64file /usr/share/man/man1/g++.1.gz from install of gcc-c++-4.1.2-46.el5.x86_64 conflicts with file from package gcc-c++-4.1.2-48.el5.x86_64。

16种C语言编译警告(Warning)类型的解决方法

16种C语言编译警告(Warning)类型的解决方法

16种C语言编译警告(Warning)类型的解决方法当编译程序发现程序中某个地方有疑问,可能有问题时就会给出一个警告信息。

警告信息可能意味着程序中隐含的大错误,也可能确实没有问题。

对于警告的正确处理方式应该是:尽可能地消除之。

对于编译程序给出的每个警告都应该仔细分析,看看是否真的有问题。

只有那些确实无问题的警告才能放下不管。

说明:由于编译的警告各种各样,根本不可以一一罗列出来,下面只是列举出比较典型的一些警告,还有一些警告,大家只要根据字面意思,就可以很快的查找出来,并解决之。

类型1:显示:warning: implicit declaration of function 'Example()'。

警告原因:在你的.c文件中调用了函数Example(),可是你并没有把声明这个函数的相应的.h文件包含进来。

有可能你在一个.c文件中定义了这个函数体,但并没有在.h中进行声明。

解决方法:你可以在调用这种函数的.c文件的一开始处加上:extern Example();你可以在调用这种函数的.c文件中包含进声明了函数Example()的头文件。

如果你在一个.c文件中定义了这个函数体,但并没有在.h中进行声明,不嫌麻烦的话,你也可以去生成一个.h文件,加上你的函数声明。

类似的警告:warning: type mismatch with previous implicit declarationwarning: type mismatch with previous implicit declarationwarning: previous implicit declaration of 'Example()'类型2:显示:warning: unused variable 'param'。

警告原因:很明显,是您定义了变量‘param',却根本没有使用它。

解决方法:不需要用的话,就删了它吧。

gcc警告手册

gcc警告手册

gcc警告手册GCC(GNU Compiler Collection)是一款开源的编译器,用于将源代码编译成可执行文件。

在使用GCC编译代码时,可能会遇到各种警告信息,这些警告信息可以帮助我们发现代码中的潜在问题。

下面是一个关于GCC警告信息的800字左右的说明手册:一、概述GCC在编译过程中会输出警告信息,这些信息通常用于指出代码中可能存在的问题,如未使用的变量、未初始化的变量、潜在的类型不匹配等。

正确理解和处理这些警告信息可以帮助我们提高代码质量,减少潜在的错误和缺陷。

二、常见警告类型1.未使用的变量:当一个变量在定义后从未被使用时,GCC会发出这种警告。

这可能是由于变量名拼写错误、逻辑错误或者变量定义多余。

2.未初始化的变量:当一个变量在使用前未被初始化时,GCC会发出这种警告。

未初始化的变量可能导致不确定的行为,因此应该避免使用未初始化的变量。

3.潜在的类型不匹配:当一个变量的类型与其被赋予的值不匹配时,GCC会发出这种警告。

这可能是由于类型转换错误、函数返回值类型不匹配等原因引起的。

4.函数未声明:当一个函数在调用前未被声明时,GCC 会发出这种警告。

应该确保所有调用的函数都已在调用前声明或定义。

5.数组越界:当数组下标越界时,GCC会发出这种警告。

这可能是由于数组下标计算错误或者逻辑错误引起的。

6.除以零:当一个整数被零除时,GCC会发出这种警告。

应该避免整数被零除的情况。

7.丢失符号:当链接器找不到某个符号(如函数或变量)的定义时,GCC会发出这种警告。

这可能是由于编译选项错误、遗漏了某个源文件等原因引起的。

三、处理警告的方法1.检查代码逻辑:对于未使用的变量和未初始化的变量等警告,需要检查代码逻辑是否正确,确保每个变量都有正确的用途和初始化。

2.类型转换:对于潜在的类型不匹配等警告,可以使用类型转换来确保数据类型匹配。

3.函数声明:对于函数未声明等警告,需要确保所有调用的函数都已在调用前声明或定义。

GCC(警告.优化以及调试选项)

GCC(警告.优化以及调试选项)

GCC(警告.优化以及调试选项)[介绍]gcc and g 分别是gnu的c & c 编译器gcc/g 在执行编译工作的时候,总共需要4步1.预处理,生成.i的文件预处理器cpp2.将预处理后的文件不转换成汇编语言,生成文件.s编译器egcs3.有汇编变为目标代码(机器代码)生成.o的文件汇编器as4.连接目标代码,生成可执行程序连接器ld1.总体选项-E只激活预处理,这个不生成文件,你需要把它重定向到一个输出文件里面.例子用法:gcc -E hello.c > pianoapan.txtgcc -E hello.c | more慢慢看吧,一个hello word 也要与处理成800行的代码-S只激活预处理和编译,就是指把文件编译成为汇编代码。

例子用法gcc -S hello.c他将生成.s的汇编代码,你可以用文本编辑器察看-c只激活预处理,编译,和汇编,也就是他只把程序做成obj文件例子用法:gcc -c hello.c他将生成.o的obj文件2.目录选项-Idir在你是用#include'file'的时候,gcc/g 会先在当前目录查找你所制定的头文件,如果没有找到,他回到缺省的头文件目录找,如果使用-I制定了目录,他回先在你所制定的目录查找,然后再按常规的顺序去找.对于#include,gcc/g 会到-I制定的目录查找,查找不到,然后将到系统的缺省的头文件目录查找-include file-i相当于“#include”包含某个代码,简单来说,就是便以某个文件,需要另一个文件的时候,就可以用它设定,功能就相当于在代码中使用#include例子用法:gcc hello.c -include /root/pianopan.h-I-就是取消前一个参数的功能,所以一般在-Idir之后使用-idirafter dir在-I的目录里面查找失败,讲到这个目录里面查找.-iprefix prefix-iwithprefix dir一般一起使用,当-I的目录查找失败,会到prefix dir下查找-Ldir制定编译的时候,搜索库的路径。

gcc编译报错解决方案

gcc编译报错解决方案

Gcc最基本的用法是∶gcc [options] [filenames] 其中options就是编译器所需要的参数,filenames给出相关的文件名称。

-c,只编译,不连接成为可执行文件,编译器只是由输入的.c等源代码文件生成.o为后缀的目标文件,通常用于编译不包含主程序的子程序文件。

-o output_filename,确定输出文件的名称为output_filename,同时这个名称不能和源文件同名。

如果不给出这个选项,gcc就给出预设的可执行文件a.out。

1.linker input file unused because linking not done我在Windows下使用SQLite编了个C++程序,在编写Makefile并使用cygwin 中的g++进行编译链接时遇到如下的错误:linker input file unused because linking not done我的语句是g++ -c a.cpp -lsqlite3到网上查了一下,问题出在链接数据库的语句不应出现在编译语句里面。

上面的语句里面有-c 即编译compile,所以命令会忽略掉所有的-l 链接库的命令。

而要链接数据库,应该在生成可执行文件时才使用。

正确的使用链接的语句应该是g++ a.o -L. -lsqlite3 -o exename2.Linux系统的头文件和库文件搜索路径**#include**的头文件,连结数据库,系统定义,总共有下列来源指定gcc去那找。

∙当初在编译时指定的(在~gcc/gcc/collect2.c:locatelib()∙写在specs内的(针对rpm包)∙后来用-D -I -L指定的∙gcc环境变量设定(编译的时候)∙ld.so的环境变量(这是run time的时候)头文件gcc 在编译时如何去寻找所需要的头文件:∙header file的搜寻会从-I开始∙然后找gcc的环境变量C_INCLUDE_PA TH,CPLUS_INCLUDE_PATH,OBJC_INCLUDE_PA TH∙再找内定目录:o/usr/includeo/usr/local/includeo/usr/lib/gcc-lib/i386-linux/2.95.2/includeo/usr/lib/gcc-lib/i386-linux/2.95.2/../../../../include/g++-3o/usr/lib/gcc-lib/i386-linux/2.95.2/../../../../i386-linux/include∙库文件但是如果装gcc的时候,是有给定的prefix的话,那么就是o/usr/includeo prefix/includeo prefix/xxx-xxx-xxx-gnulibc/includeo prefix/lib/gcc-lib/xxxx-xxx-xxx-gnulibc/2.8.1/include库文件cos()等函式库的选项要多加-lm∙编译的时候:gcc会去找-L∙再找gcc的环境变量LIBRARY_PATH∙再找内定目录/lib /usr/lib /usr/local/lib 这是当初compile gcc时写在程序内的运行时动态库的搜索路径1 在配置文件/etc/ld.so.conf中指定动态库搜索路径2 通过环境变量LD_LIBRARY_PA TH指定动态库搜索路径(当通过该环境变量指定多个动态库搜索路径时,路径之间用冒号":"分隔)3 在编译目标代码时指定该程序的动态库搜索路径(还可以在编译目标代码时指定程序的动态库搜索路径。

gcc常见的编译警告与错误(按字母顺序排列)

gcc常见的编译警告与错误(按字母顺序排列)

gcc 常见的编译警告与错误〔按字母顺序排列〕C语言初学者遇到的最大问题往往是看不懂编译错误,进而不知如何修改程序。

有鉴于此,本附录罗列了用gcc编译程序时经常出现的编译警告与错误。

需要提醒读者的是,出现警告〔warning〕并不影响目标程序的生成,但出现错误〔error〕则无法生成目标程序。

为便于读者查阅,下面列出了经常遇到的警告与错误,给出了中英文对照〔英文按字典顺序排列〕,并对局部错误与警告做了必要的解释。

*%s e*pects \FILENAME\ or …*%s 需要\FILENAME\ 或…*%s is a deprecated GCC e*tension*%s 是一个已过时的GCC 扩展*%s is a GCC e*tension*%s 是一个GCC 扩展*~ error:*~ 错误:*~ In file included from %s:%u*~ 在包含自%s:%u 的文件中*~ internal error:*~ 内部错误:*~ no newline at end of file*~ 文件未以空白行完毕*~ warning:*~ 警告:*elif after *else*elif 出现在*else 后*elif without *if*elif 没有匹配的*if*else after *else*else 出现在*else 后*else without *if*else 没有匹配的*if*endif without *if*endif 没有匹配的*if*include nested too deeply*include 嵌套过深*include_ne*t in primary source file*include_ne*t 出现在主源文件中*pragma %s %s is already registered*pragma %s %s 已经被注册*pragma %s is already registered*pragma %s 已经被注册*pragma once in main file*pragma once 出现在主文件中*pragma system_header ignored outside include file*pragma system_heade 在包含文件外被忽略%.*s is not a valid universal character%.*s 不是一个有效的Unicode 字符%s in preprocessing directive预处理指示中出现%s%s is a block device%s 是一个块设备%s is shorter than e*pected%s 短于预期%s is too large%s 过大%s with no e*pression%s 后没有表达式%s: not used because `%.*s’ defined as `%s’ not `%.*s’%s:未使用因为‘%.*s’被定义为‘%s’而非‘%*.s’%s: not used because `%.*s’ is poisoned%s:未使用因为‘%.*s’已被投毒%s: not used because `%.*s’ not defined%s:未使用因为‘%.*s’未定义%s: not used because `%s’ is defined%s:未使用因为‘%s’已定义%s: not used because `__COUNTER__’ is invalid%s:未使用因为‘__COUNTER__’无效(\%s\ is an alternative token for \%s\ in C++)(在C++ 中"%s〞会是"%s〞的替代标识符)(this will be reported only once per input file)(此警告为每个输入文件只报告一次)\%s\ after * is not a positive integer* 后的"%s〞不是一个正整数\%s\ after *line is not a positive integer*line 后的"%s〞不是一个正整数\%s\ cannot be used as a macro name as it is an operator in C++ "%s〞不能被用作宏名,因为它是C++ 中的一个操作符\%s\ is not a valid filename"%s〞不是一个有效的文件名\%s\ is not defined"%s〞未定义\%s\ may not appear in macro parameter list"%s不能出现在宏参数列表中\%s\ re-asserted重断言"%s〞\%s\ redefined"%s重定义\/*\ within ment"/*出现在注释中\\* used with no following he* digits\\* 后没有16 进制数字\defined\ cannot be used as a macro name"defined不能被用作宏名__COUNTER__ e*panded inside directive with -fdirectives-only带-fdirectives-only 时__COUNTER__ 在指示中扩展__VA_ARGS__ can only appear in the e*pansion of a C99 variadic macro __VA_ARGS__ 只能出现在C99 可变参数宏的展开中_Pragma takes a parenthesized string literal_Pragma 需要一个括起的字符串字面常量‘%.*s’ is not in NFC‘%.*s’不在NFC 中‘%.*s’ is not in NFKC‘%.*s’不在NFKC 中‘**’ cannot appear at either end of a macro e*pansion‘**’不能出现在宏展开的两端‘*’ is not followed by a macro parameter‘*’后没有宏参数‘$’ in identifier or number‘$’出现在标识符或数字中‘:’ without preceding ‘"’‘:’前没有‘"’‘"’ without following ‘:’‘"’后没有‘:’'return' with a value, in function returning void在void返回类型的函数中,return返回值。

如何解决C语言中的编译错误与警告

如何解决C语言中的编译错误与警告

如何解决C语言中的编译错误与警告C语言是一种广泛应用于系统编程和嵌入式开发的编程语言,它的编译过程中常常会出现各种错误和警告。

这些错误和警告可能会导致程序无法正确编译或者在运行时出现问题。

因此,解决C语言中的编译错误与警告是每个程序员都需要掌握的重要技能。

本文将介绍一些常见的编译错误和警告,并提供一些解决方法。

一、理解编译错误和警告的含义在解决编译错误和警告之前,我们首先需要理解它们的含义。

编译错误是指在编译过程中发现的语法错误或者逻辑错误,这些错误会导致程序无法正确编译。

编译警告是指在编译过程中发现的一些潜在的问题,虽然不会导致程序无法编译,但可能会导致程序在运行时出现问题。

二、常见的编译错误和警告1. 语法错误:语法错误是最常见的编译错误之一,它通常是由于编程人员在编写代码时遗漏了分号、括号不匹配等导致的。

解决语法错误的方法是仔细检查代码,确保语法正确。

2. 未声明的标识符:当使用一个未声明的标识符时,编译器会报错。

解决这个问题的方法是在使用标识符之前先进行声明或者引入相应的头文件。

3. 类型不匹配:类型不匹配是指在赋值或者函数调用过程中,变量的类型与期望的类型不一致。

解决这个问题的方法是检查变量的类型,并进行相应的类型转换。

4. 未使用的变量:编译器会对未使用的变量发出警告,这可能是由于代码中存在一些无用的变量。

解决这个问题的方法是删除未使用的变量或者对其进行使用。

5. 未初始化的变量:未初始化的变量可能导致程序在运行时出现未定义的行为。

解决这个问题的方法是在使用变量之前先进行初始化。

6. 函数返回值不匹配:函数的返回值类型与函数定义时声明的返回值类型不一致会导致编译错误。

解决这个问题的方法是检查函数的返回值类型,并进行相应的修改。

7. 潜在的内存泄漏:编译器可能会对未释放的内存发出警告,这可能会导致内存泄漏。

解决这个问题的方法是在使用完内存后及时释放。

三、解决编译错误和警告的方法1. 仔细阅读编译错误和警告信息:编译器通常会提供详细的错误和警告信息,我们应该仔细阅读这些信息,找出问题所在。

常见gcc编译问题解决方法集

常见gcc编译问题解决方法集

常见gcc编译问题解决⽅法集除⾮明确说明,本⽂内容仅针对x86/x86_64的Linux开发环境,有朋友说baidu不到,开个贴记录⼀下(加粗字体是关键词):⽤“-Wl,-Bstatic”指定链接静态库,使⽤“-Wl,-Bdynamic”指定链接共享库,使⽤⽰例:-Wl,-Bstatic -lmysqlclient_r -lssl -lcrypto -Wl,-Bdynamic -lrt -Wl,-Bdynamic -pthread -Wl,-Bstatic -lgtest ("-Wl"表⽰是传递给链接器ld的参数,⽽不是编译器gcc/g++的参数。

)1) 下⾯是因为没有指定链接参数-lz(/usr/lib/libz.so,/usr/lib/libz.a )/usr/local/mysql/lib/mysql/libmysqlclient.a(my_compress.c.o): In function `my_uncompress':/home/software/mysql-5.5.24/mysys/my_compress.c:122: undefined reference to `uncompress'/usr/local/mysql/lib/mysql/libmysqlclient.a(my_compress.c.o): In function `my_compress_alloc':/home/software/mysql-5.5.24/mysys/my_compress.c:71: undefined reference to `compress'2) 下⾯是因为没有指定编译链接参数-pthread(注意不仅仅是-lpthraed)/usr/local/mysql/lib/mysql/libmysqlclient.a(charset.c.o): In function `get_charset_name':/home/zhangsan/mysql-5.5.24/mysys/charset.c:533: undefined reference to `pthread_once'3) 下⾯这个是因为没有指定链接参数-lrt/usr/local/thirdparty/curl/lib/libcurl.a(libcurl_la-timeval.o): In function `curlx_tvnow':timeval.c:(.text+0xe9): undefined reference to `clock_gettime'4) 下⾯这个是因为没有指定链接参数-ldl/usr/local/thirdparty/openssl/lib/libcrypto.a(dso_dlfcn.o): In function `dlfcn_globallookup':dso_dlfcn.c:(.text+0x4c): undefined reference to `dlopen'dso_dlfcn.c:(.text+0x62): undefined reference to `dlsym'dso_dlfcn.c:(.text+0x6c): undefined reference to `dlclose'5) 下⾯这个是因为指定了链接参数-static,它的存在,要求链接的必须是静态库,⽽不能是共享库ld: attempted static link of dynamic object如果是以-L加-l⽅式指定,则⽬录下必须有.a⽂件存在,否则会报-l的库⽂件找不到:ld: cannot find -lACE6) GCC编译遇到如下的错误,可能是因为在编译时没有指定-fPIC,记住:-fPIC即是编译参数,也是链接参数relocation R_x86_64_32S against `vtable for CMyClass` can not be used when making a shared object7) 下⾯的错误表⽰gcc编译时需要定义宏__STDC_FORMAT_MACROS,并且必须包含头⽂件inttypes.htest.cpp:35: error: expected `)' before 'PRIu64'8) 下⾯是因为在x86机器(32位)上编译没有指定编译参数-march=pentium4../../src/common/libmooon.a(logger.o): In function `atomic_dec_and_test':../../include/mooon/sys/atomic_gcc.h:103: undefined reference to `__sync_sub_and_fetch_4'9) 下列错误可能是因为多了个“}”error: expected declaration before '}' token10) 下列错误可能是因为少了个“}”error: expected `}' at end of input11) 下⾯这个错误是编译⼀个共享库时,该共享库依赖的⼀静态库编译时没有加“-fPIC”参数,解决⽅法为带“-fPIC”重新编译被依赖的静态库relocation R_X86_64_32 against `a local symbol' can not be used when making a shared object; recompile with -fPIC12) 下⾯这个错误,是因为头⽂件中使⽤“_syscall0(pid_t, gettid)”不当引起的./test.o: In function `gettid()':./test.h:17: multiple definition of `gettid()'正确的⽤法是使⽤"inline"或"static inline"修饰⼀下:inline _syscall0(pid_t, gettid)或static inline _syscall0(pid_t, gettid)当然也可以这样:在.h头⽂件中:extern "C" pid_t gettid(void);在.cpp⽂件中:_syscall0(pid_t, gettid)_syscall0是⼀个宏,定义⼀个函数的实现。

  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

常见gcc编译警告整理以及解决方法【收藏】
1、warning: no newline at end of file
 在文件最后一行加上回车键
 解释:在《Rationale for the C99 standard》一文中,有C99的相关信息:
 A backslash immediately before a newline has long been used to continue string literals, as well as preprocessing command lines. In the interest of easing machine generation of C, and of transporting code to machines with restrictive physical line lengths, the C89 Committee generalized this mechanism to permit any token to be continued by interposing a backslash/newline sequence.
 c/c++代码的每一行后面有一个结束符,也就是newline。

避免当被include
的文件展开后,前一个文件的最后一行与后一个文件的第一行直接被连接成一行从而造成错误。

 2、warning: comparison between pointer and integer
 解释:integer与pointer比较
 3、warning: assignment discards qualifiers from pointer target type。

相关文档
最新文档