初学Linux C编程遇到编译出错

运行下面编译命令,出错

gcc -g -W -Wall -pthread  -lcrypto -o httpd httpd.c

错误信息:

/tmp/ccYw8W8E.o: In function `md5test':
/home/rivulet/work/Tinyhttpd-master/httpd.c:666: undefined reference to `MD5_Init'
/home/rivulet/work/Tinyhttpd-master/httpd.c:667: undefined reference to `MD5_Update'
/home/rivulet/work/Tinyhttpd-master/httpd.c:668: undefined reference to `MD5_Final'
collect2: error: ld returned 1 exit status
make: *** [httpd] Error 1

一开始以为是库没找到,但是在/user/local/lib下面明明存在libcrypto.so库,后来发现下面命令能编译成功

gcc -g -W -Wall -o httpd httpd.c -lssl -lcrypto

编译时候,-l所知识的库需要跟在.c文件的后面,表明依赖关系,库与库的之间的依赖关系需要通过顺序定义好,写在前面的库依赖于 写在后边的库

具体解释如下:

The traditional behavior of linkers is to search for external functions from left to right in the libraries specified on the command line. This means that a library containing the definition of a function should appear after any source files or object files which use it. This includes libraries specified with the short-cut -l option, as shown in the following command:

$ gcc -Wall calc.c -lm -o calc   (correct order)

With some linkers the opposite ordering (placing the -lm option before the file which uses it) would result in an error,

$ cc -Wall -lm calc.c -o calc    (incorrect order)
main.o: In function `main':
main.o(.text+0xf): undefined reference to `sqrt'

because there is no library or object file containing sqrt after 'calc.c'. The option -lm should appear after the file 'calc.c'.

When several libraries are being used, the same convention should be followed for the libraries themselves. A library which calls an external function defined in another library should appear before the library containing the function.

For example, a program 'data.c' using the GNU Linear Programming library 'libglpk.a', which in turn uses the math library 'libm.a', should be compiled as,

$ gcc -Wall data.c -lglpk -lm

since the object files in 'libglpk.a' use functions defined in 'libm.a'.

Most current linkers will search all libraries, regardless of order, but since some do not do this it is best to follow the convention of ordering libraries from left to right.

This is worth keeping in mind if you ever encounter unexpected problems with undefined references, and all the necessary libraries appear to be present on the command line.

google翻译如下:

链接器的传统行为是在命令行中指定的库中从左到右搜索外部函数。这意味着包含函数定义的库应该出现在使用它的任何源文件或目标文件之后。这包括使用short-cut -l选项指定的库,如以下命令所示:

$ gcc -Wall calc.c -lm -o calc(正确的顺序)
对于一些链接器,相反的顺序(在使用它的文件之前放置-lm选项)会导致错误,

$ cc -Wall -lm calc.c -o calc(错误的顺序)
main.o:在函数`main'中:
main.o(.text + 0xf):对'sqrt'的未定义引用
因为'calc.c'之后没有包含sqrt的库或目标文件。选项-lm应出现在文件'calc.c'之后。

当使用多个库时,库本身应遵循相同的约定。调用另一个库中定义的外部函数的库应该出现在包含该函数的库之前。

例如,使用GNU线性编程库'libglpk.a'的程序'data.c',它反过来使用数学库'libm.a',应编译为,

$ gcc -Wall data.c -lglpk -lm
因为'libglpk.a'中的目标文件使用'libm.a'中定义的函数。

大多数当前链接器将搜索所有库,无论顺序如何,但由于一些不执行此操作,因此最好遵循从左到右排序库的约定。

如果您遇到未定义引用的意外问题,并且所有必需的库似乎都出现在命令行中,则值得记住。

linux gcc编译参数顺序问题相关推荐

  1. Linux系统 gcc工作流程和gcc编译参数

    一.gcc工作流程 二.gcc编译参数 gcc参数 说明 -E 预处理指定的源文件,不进行编译 -S 编译指定源文件,但是不进行汇编 -c 编译.汇编指定的源文件,但是不进行链接 -o [file1] ...

  2. Linux GCC 常用命令

    Linux GCC 常用命令与汇编 文章目录 Linux GCC 常用命令与汇编 1 .简介 2 .简单编译 2.1 预处理 2.2 编译为汇编代码 2.3 汇编(Assembly) 2.4 连接(L ...

  3. Linux GCC常用命令和ELF文件格式

    Linux GCC常用命令和ELF文件格式 一.各种工具 (一)GCC编译工具 (二)Binutils (三)C 运行库 一. C 语言程序 Hello.c示例 (一)准备工作 (二)编译过程 1.预 ...

  4. Linux GCC lib库相互引用,互相依赖(交叉引用)链接解决办法

    Linux GCC中,如果lib a依赖b,b又依赖a,链接的时候无论a放在前,还是b放在前,都会提示unrefrence. 解决办法就是: 链接的时候a链接两次,即: -la -lb -la

  5. Linux命令执行顺序— ||和和; 比较

    Linux命令执行顺序- ||和&&和; command1 && command2: &&左边的command1执行成功(返回0表示成功)后,& ...

  6. linux gcc中sin30度,学习gcc.pdf

    学习gcc 如何在 Ubuntu 下安装和使用 gcc GCC 是在Linux 系统中用的比较多的编译器,一般做Linux 开发的朋友都会用 GCC 编译器,但今天 在 Ubuntu 上安装 GCC ...

  7. linux gcc 示例_最好的Linux示例

    linux gcc 示例 Linux is a powerful operating system that powers most servers and most mobile devices. ...

  8. 在wsl下运行c语言,在Windows10通过WSL架设linux/gcc c语言学习环境

    在Windows10通过WSL架设linux/gcc c语言学习环境 零:简单介绍与先决条件 中国一般的大学C课程中都使用很过时的编译器来进行教授--比如说什么visual studio 6.0啊,d ...

  9. 深入剖析 linux GCC 4.4 的 STL string

    转自: 深入剖析 linux GCC 4.4 的 STL string 本文通过研究STL源码来剖析C++中标准模板块库std::string运行机理,重点研究了其中的引用计数和Copy-On-Wri ...

最新文章

  1. get post put delete 区别_GET 和 POST 的区别?
  2. 《学习之道》第六章关注过程,亦培养习惯
  3. C#实现RSA加密和解密详解
  4. git或gitlab修改密码之后,报错remote: HTTP Basic: Access denied,fatal: Authentication failed for ‘git或gitlab地址’
  5. css制作按钮按下去效果
  6. 阿里云 超级码力在线编程大赛初赛 第2场 题目4. 小栖的金字塔(超级卡特兰数+除法求模/乘法逆元)
  7. mysql排重_mysql 排重查询
  8. Mediator(中介者)
  9. java int integer_浅谈java中int和Integer的区别
  10. 第四章需求分析与设计工具
  11. Linux线程优先级设置
  12. Work around for SQL Configuration Manager Consol
  13. 3款移动应用数据统计分析平台对比
  14. 计算机常用英语词汇表
  15. 基于Php门禁系统设计与实现
  16. 微信小程序动态点赞php,微信小程序小组件 基于Canvas实现直播点赞气泡效果
  17. SiamFC:用于目标跟踪的全卷积孪生网络 fully-convolutional siamese networks for object tracking
  18. 为什么要用python处理excel-以Excel处理为目的学习python还是VBA?
  19. RabbitMQ和fegin补充
  20. 华为ensp Cloud连接使用及创建环回适配器网卡

热门文章

  1. Python_序列对象内置方法详解_String
  2. Sublime-text
  3. 22. 一个题来探查对 字符串,指针,数组三方面的关联使用方面的概念是否清晰,分析下面三个printf打印什么?...
  4. el表达式判断字符串相等
  5. Debian和CentOS主流系统按包含文件名称搜索软件包
  6. CEF(包含均衡负载)
  7. SpringBoot集成全局异常处理
  8. Ubuntu14搭建配置青岛大学OJ系统
  9. x window的奥秘
  10. [Java] Web开发环境搭建 - MyEclipse 篇