[Linux][C][gcc] Linux GCC 编译链接 报错ex: ./libxxx.so: undefined reference to `shm_open'
本人原创文章,文章是在此代码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'的更多相关文章
- LINUX下基于NVIDIA HPC SDK 的 VASP6.3.x编译安装报错整理
关于gcc 用旧版本安装NVIDIA HPC SDK再编译会报错: "/opt/rh/devtoolset-8/root/usr/include/c++/8/bits/move.h" ...
- Linux 下使用C语言 gets()函数报错
在Linux下,使用 gets(cmd) 函数报错:warning: the 'gets' function is dangerous and should not be used. 解决办法:采用 ...
- 在Linux上配置xampp后远程访问域名报错
在Linux上配置xampp后远程访问域名报错: New XAMPP security concept: Access to the requested object is only availabl ...
- linux上安装完torch后仍报错:ImportError: No module named torch
linux上安装完torch后仍报错: Traceback (most recent call last): File , in <module> import torch ImportE ...
- webpack编译sass报错找不到module /css-loader/index.js... || 安装node-sass报错
今天无论在家还是在公司,安装node-sass总是失败,或安装成功了,使用webpack编译sass报错,说找不到module,按照提示的错误我找了node_modules下的css-loader,发 ...
- 源码编译apache报错的解决方法
源码编译apache报错的解决方法 问题介绍 在源码编译安装httpd时,./configure执行无错误,到make时就报错,在网络上搜索了很多文章,很多方法如换apr-util的低版本并不能很 ...
- 编译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 ...
- idea中编译项目报错 java: javacTask: 源版本 1.8 需要目标版本 1.8
问题如上面所叙: > idea中编译项目报错 java: javacTask: 源版本 1.8 需要目标版本 1.8 解决方案: > Setting->Compiler->Ja ...
- zookeeper 链接报错 KeeperErrorCode = NodeExists for
zookeeper 链接报错 CONNECTING Receive watched event:WatchedEvent state:SyncConnected type:None path:nul ...
随机推荐
- 对这些工程师来说,Skype Translator是他们自己的事——Skype Translator项目研发团队的故事
Translator是他们自己的事--Skype Translator项目研发团队的故事" title="对这些工程师来说,Skype Translator是他们自己的事--Sky ...
- springboot oauth 鉴权之——password、authorization_code鉴权
参考一下两个案例:https://www.cnblogs.com/haoliyou/p/9606018.html https://www.cnblogs.com/haoliyou/p/9606036. ...
- Vacant Seat(Atcoder-C-交互式题目)
C - Vacant Seat Time limit : 2sec / Memory limit : 256MB Score : 500 points Problem Statement This i ...
- 纯js文件弹出窗写法
<dialog id="myDialog"> 01234567890 <a href="javascript:;" id="clos ...
- 机器学习算法的基本知识(使用Python和R代码)
本篇文章是原文的译文,然后自己对其中做了一些修改和添加内容(随机森林和降维算法).文章简洁地介绍了机器学习的主要算法和一些伪代码,对于初学者有很大帮助,是一篇不错的总结文章,后期可以通过文中提到的算法 ...
- 1078 Hashing (25 分)
1078 Hashing (25 分) The task of this problem is simple: insert a sequence of distinct positive integ ...
- 网络健身O2O,能火吗?
谈到中国想要020的那些项目,总给人一种土豪烧钱的怪异形象,而最终的成败因素也变得简单,也即谁能烧到最后,谁就能称霸市场,可问题在于,前期投入太多,谁也不甘心主动退出,最后,只落得个油尽灯枯.这 ...
- 安卓权威编程指南-笔记(第27章 broadcast intent)
本章需求:首先,让应用轮询新结果并在有所发现时及时通知用户,即使用户重启设备后还没有打开过应用.其次,保证用户在使用应用时不出现新结果通知. 1. 一般intent和broadcast intent ...
- Fragment argument
每个Fragment实例都可以附带一个Bundle对象,该bundle包含键值对,可以像附加extra到Activity的intent中那样使用他们.一个键值对即一个argument. 创建frage ...
- c++背包问题
又鸽了好久…… 前言 博主刚刚学会背包问题不久,然后有一段时间没练习了 今天就来重新温习一下,顺手就写了这一篇博客. 好了,下面进入正题! 算法简介 背包问题是动态规划的一个分支 主要是分成了01背包 ...