本人原创文章,文章是在此代码github/note的基础上进行补充,转载请注明出处:https://github.com/dramalife/note。

以librt丶用户自定义动态库libxxx 和 用户应用程序app为例,讨论编译链接过程中出现的错误,

其中app依赖libxxx,libxxx依赖librt。

关键词:“ undefined reference to”。

1 源文件

1.1 app.c

/*
* [note](github.com/dramalife/note.git)
* Dramalife@live.com
* Init : 2020.03.04
*/
#include <stdio.h> extern void func_in_libxxx(void); int main(void)
{
printf("File:%12s,Func:%14s,Line:%4d. \n",__FILE__,__func__,__LINE__);
func_in_libxxx();
return 0;
}

1.2 libxxx.c

/*
* [note](github.com/dramalife/note.git)
* Dramalife@live.com
* Init : 2020.03.04
*/ /* shm related */
#include <sys/mman.h>
#include <sys/stat.h> /* For mode constants */
#include <fcntl.h> /* For O_* constants */ #include <stdio.h> void func_in_libxxx(void)
{
printf("File:%12s,Func:%14s,Line:%4d. \n",__FILE__,__func__,__LINE__);
shm_open("abcd",O_RDWR, 0);
}

1.3 Makefile

#
# [note](github.com/dramalife/note.git)
# Dramalife@live.com
# Init : 2020.03.04
# all : dynlib app_out app_out:
gcc -o app.out app.c -Wl,-rpath=. -L. -lxxx -lrt dynlib:
gcc -o libxxx.so libxxx.c -fPIC -shared clean:
rm -rvf ./*.out ./*.so ######### Targets for testing !!! #########
# Target - no "-lrt"
app_err_lrt:
gcc -o app.out app.c -Wl,-rpath=. -L. -lxxx
# Target - add "-Wl,--as-needed"
app_err_asneeded:
gcc -o app.out app.c -L. -Wl,-rpath=. -Wl,--as-needed -lrt -lxxx

2 编译

2.1 编译动态库

# make dynlib
gcc -o libxxx.so libxxx.c -fPIC -shared

2.2 编译程序文件错误1 - 缺少链接库

缺少链接动态库的情况-lrt

# make app_err_lrt
gcc -o app.out app.c -Wl,-rpath=. -L. -lxxx
./libxxx.so: undefined reference to `shm_open'
collect2: error: ld returned 1 exit status
makefile:22: recipe for target 'app_err_lrt' failed
make: *** [app_err_lrt] Error 1

2.3 编译程序文件错误2 - 错误使用链接选项

由于链接选项导致的编译错误

# make app_err_asneeded
gcc -o app.out app.c -L. -Wl,-rpath=. -Wl,--as-needed -lrt -lxxx
./libxxx.so: undefined reference to `shm_open'
collect2: error: ld returned 1 exit status
makefile:25: recipe for target 'app_err_asneeded' failed
make: *** [app_err_asneeded] Error 1

3 结论

错误1出现的原因是链接时缺少对必要库的指定。

错误2出现的原因是“-Wl,--as-needed”向ld传递的参数,手册中对这个选项的说明如下(LD(1)):

 --as-needed
--no-as-needed
This option affects ELF DT_NEEDED tags for dynamic libraries mentioned on the command line after the --as-needed option. Normally the linker will add a
DT_NEEDED tag for each dynamic library mentioned on the command line, regardless of whether the library is actually needed or not. --as-needed causes a
DT_NEEDED tag to only be emitted for a library that at that point in the link satisfies a non-weak undefined symbol reference from a regular object file or, if
the library is not found in the DT_NEEDED lists of other needed libraries, a non-weak undefined symbol reference from another needed dynamic library. Object
files or libraries appearing on the command line after the library in question do not affect whether the library is seen as needed. This is similar to the
rules for extraction of object files from archives. --no-as-needed restores the default behaviour.

[Linux][C][gcc] Linux GCC 编译链接 报错ex: ./libxxx.so: undefined reference to `shm_open'的更多相关文章

  1. LINUX下基于NVIDIA HPC SDK 的 VASP6.3.x编译安装报错整理

    关于gcc 用旧版本安装NVIDIA HPC SDK再编译会报错: "/opt/rh/devtoolset-8/root/usr/include/c++/8/bits/move.h" ...

  2. Linux 下使用C语言 gets()函数报错

    在Linux下,使用 gets(cmd) 函数报错:warning: the 'gets' function is dangerous and should not be used. 解决办法:采用 ...

  3. 在Linux上配置xampp后远程访问域名报错

    在Linux上配置xampp后远程访问域名报错: New XAMPP security concept: Access to the requested object is only availabl ...

  4. linux上安装完torch后仍报错:ImportError: No module named torch

    linux上安装完torch后仍报错: Traceback (most recent call last): File , in <module> import torch ImportE ...

  5. webpack编译sass报错找不到module /css-loader/index.js... || 安装node-sass报错

    今天无论在家还是在公司,安装node-sass总是失败,或安装成功了,使用webpack编译sass报错,说找不到module,按照提示的错误我找了node_modules下的css-loader,发 ...

  6. 源码编译apache报错的解决方法

    源码编译apache报错的解决方法   问题介绍 在源码编译安装httpd时,./configure执行无错误,到make时就报错,在网络上搜索了很多文章,很多方法如换apr-util的低版本并不能很 ...

  7. 编译PHP 报错:node.c: In function dom_canonicalization

    编译PHP 报错:node.c: In function dom_canonicalization  /opt/php-5.2.17/ext/dom/node.c:1953: error: deref ...

  8. idea中编译项目报错 java: javacTask: 源版本 1.8 需要目标版本 1.8

    问题如上面所叙: > idea中编译项目报错 java: javacTask: 源版本 1.8 需要目标版本 1.8 解决方案: > Setting->Compiler->Ja ...

  9. zookeeper 链接报错 KeeperErrorCode = NodeExists for

    zookeeper  链接报错 CONNECTING Receive watched event:WatchedEvent state:SyncConnected type:None path:nul ...

随机推荐

  1. 千万不要在module里扩展较多逻辑,很容易引起项目异常。

    NOP项目 为保持紧跟NOP更新,项目组坚持不改NOP源码. 以触发器,插件化开发为拓展模式 NOP自定义好的接口或完全独立的新拓展功能很容易插件化. 但部分功能要在NOP原项目上扩展修改在不改源码的 ...

  2. java操作telnet远程登录

    import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import jav ...

  3. WordPress调用page页面内容方法

    WordPress调用page页面内容方法,有时候在特殊条件下,原有的wordpress页面获取内容代码不能正常使用,这个时候不能通过wordpress自带的模板标签输出,就需要改变下方式,通过PHP ...

  4. Python---13面向对象编程

    一.类和实例 面向对象最重要的概念就是类(Class)和实例(Instance),必须牢记类是抽象的模板,比如Student类,而实例是根据类创建出来的一个个具体的“对象”,每个对象都拥有相同的方法, ...

  5. React中key的讲解

    通过阅读React的文档我们知道React这个框架的核心思想是,将页面分割成一个个组件,一个组件还可能嵌套更小的组件,每个组件有自己的数据(属性/状态);当某个组件的数据发生变化时,更新该组件部分的视 ...

  6. LaunchImage的设置及对应图片尺寸

    2017-10-12 设置APP的LaunchImage 按照如下步骤设置app的LaunchImage: In Assets.xcassets click + button -> App Ic ...

  7. uboot--tftp

    一.      概述 U-boot中的TFTP用于发送较小的文件.下层使用UDP协议,发送使用UDP 69端口,每次发送的最大分组为512 Bytes.发送双方采用超时重传机制.数据传输模式为octe ...

  8. spring——AOP原理及源码(一)

    教程共分为五篇,从AOP实例的构建及其重要组件.基本运行流程.容器创建流程.关键方法调用.原理总结归纳等几个方面一步步走进AOP的世界. 本篇主要为读者演示构建AOP实例及AOP核心组件分析. 一.项 ...

  9. 【5min+】保持程序健康的秘诀!AspNetCore的HealthCheck

    系列介绍 [五分钟的dotnet]是一个利用您的碎片化时间来学习和丰富.net知识的博文系列.它所包含了.net体系中可能会涉及到的方方面面,比如C#的小细节,AspnetCore,微服务中的.net ...

  10. WebAPI-处理架构

    带着问题去思考,大家好! 问题1:HTTP请求和返回相应的HTTP响应信息之间发生了什么? 1:首先是最底层,托管层,位于WebAPI和底层HTTP栈之间 2:其次是 消息处理程序管道层,这里比如日志 ...