GCC编译一些常见报错解决

一. C语法错误

错误资讯∶文件source.c中第n行有语法错误(syntex errror)。这种类型的错误,一般都是C语言的语法错误,应该仔细检查源代码文件中第n行及该行之前的程序,有时也需要对该文件所包含的头文件进行检查。有些情况下,一个很简单的语法错误,gcc会给出一大堆错误,我们最主要的是要保持清醒的头脑,不要被其吓倒,必要的时候再参考一下C语言的基本教材。

二. 头文件错误

错误资讯∶找不到头文件head.h(Can not find include file head.h)。这类错误是源代码文件中的包含头文件有问题,可能的原因有头文件名错误、指定的头文件所在目录名错误等,也可能是错误地使用了双引号和尖括号。

三. 档案库错误

错误资讯∶连接程序找不到所需的函数库,例如∶
ld: -lm: No such file or directory
这类错误是与目标文件相连接的函数库有错误,可能的原因是函数库名错误、指定的函数库所在目录名称错误等,检查的方法是使用find命令在可能的目录中寻找相应的函数库名,确定档案库及目录的名称并修改程序中及编译选项中的名称。

四. 未定义符号

错误资讯∶有未定义的符号(Undefined symbol)。这类错误是在连接过程中出现的,可能有两种原因∶一是使用者自己定义的函数或者全局变量所在源代码文件,没有被编译、连接,或者干脆还没有定义,这需要使用者根据实际情况修改源程序,给出全局变量或者函数的定义体;二是未定义的符号是一个标准的库函数,在源程序中使用了该库函数,而连接过程中还没有给定相应的函数库的名称,或者是该档案库的目录名称有问题,这时需要使用档案库维护命令ar检查我们需要的库函数到底位于哪一个函数库中,确定之后,修改gcc连接选项中的-l和-L项。

五. 常见报错案例

1.编译报错:
make[1]: *** [out/target/product/cubieaio_miniPC/obj/APPS/tvui_tv_intermediates/arm/package.odex] Aborted (core dumped)

在相应APK的Android.mk文件里加上 LOCAL_DEX_PREOPT:=false 即可编译出不带 .odex 的文件。

2.编译报错ld.lld: error: unable to find library

ld.lld: error: unable to find library -lcrypto
clang-14: error: linker command failed with exit code 1 (use -v to see invocation)

链接libcrypto库时,找不到这个库。

解决方案:下载libcrypto库
$ apt-cache search libcrypto

得到如下搜索结果:
libssl-dev - Secure Sockets Layer toolkit - development files
libssl-doc - Secure Sockets Layer toolkit - development documentation
libssl1.0.0 - Secure Sockets Layer toolkit - shared libraries
libssl1.0.0-dbg - Secure Sockets Layer toolkit - debug information
libcrypto++-dev - General purpose cryptographic library - C++ development
libcrypto++-doc - General purpose cryptographic library - documentation
libcrypto++-utils - General purpose cryptographic library - utilities and data files
libcrypto++9v5 - General purpose cryptographic library - shared library
libcrypto++9v5-dbg - General purpose cryptographic library - debug symbols
libcryptokit-ocaml - cryptographic algorithm library for OCaml - runtime
libcryptokit-ocaml-dev - cryptographic algorithm library for OCaml - development


选择一个库下载,这里我选择的是libssl-dev
$ sudo apt install libssl-dev


看一下安装后的效果,在/usr路径下执行:
du -a | grep libcrypto
得到如下的搜索结果,库安装完成。
0    ./local/ssl/libressl/lib/libcrypto.so.45
16664    ./local/ssl/libressl/lib/libcrypto.a
4    ./local/ssl/libressl/lib/pkgconfig/libcrypto.pc
4    ./local/ssl/libressl/lib/libcrypto.la
0    ./local/ssl/libressl/lib/libcrypto.so
0    ./local/libressl/lib/libcrypto.so
8416    ./local/libressl/lib/libcrypto.so.45.0.5
4592    ./lib/x86_64-linux-gnu/libcrypto.a
4    ./lib/x86_64-linux-gnu/pkgconfig/libcrypto.pc
0    ./lib/x86_64-linux-gnu/libcrypto.so

猜你喜欢

转载自blog.csdn.net/Chris_1994/article/details/134752449