[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 ...
随机推荐
- 用css3实现图片的放大缩小
记录一个公用的css实现图片的放大缩小 @keyframes scaleDraw { /*定义关键帧.scaleDrew是需要绑定到选择器的关键帧名称*/ 0%{ transform: scale(1 ...
- 转:zabbix 2.0.6监控cisco交换机 2950 2960s 3560G
转自: http://blog.chinaunix.net/uid-24250828-id-3806551.html 想在zabbix 上监控交换机端口的流量,找了两天的模板,包括官方的和网友写的.在 ...
- JAVA 截图+tess4j识别
我们先来看看要识别的图片和效果图 效果图: 图片识别需要用到tess4j这个包,下面是下载地址: https://share.weiyun.com/5Hjv13T 我们拿到包以后解压出来,随便你放到哪 ...
- Luogu_1080_国王游戏
题目描述 恰逢H国国庆,国王邀请n位大臣来玩一个有奖游戏.首先,他让每个大臣在左.右手上面分别写下一个整数,国王自己也在左.右手上各写一个整数.然后,让这n位大臣排成一排,国王站在队伍的最前面.排好队 ...
- HBase完全分布式集群搭建
HBase完全分布式集群搭建 hbase和hadoop一样也分为单机版,伪分布式版和完全分布式集群版,此文介绍如何搭建完全分布式集群环境搭建.hbase依赖于hadoop环境,搭建habase之前首先 ...
- Eclipse无Server或者Tomcat8.5解决办法
原文链接:https://blog.csdn.net/fangzicheng/article/details/78333567 ...
- failed to load main-class manifest attribute(运行jar包出错)
原因描述:MANIFEST.MF文件中的Main-Class配置不正确或格式不正确 检查方式:以WinRarR的方式打开jar包,如图所示, 点击进入箭头所指的META-INF文件夹 将MAN ...
- webpack进阶之loader篇
webpack的loaders是一大特色,也是很重要的一部分.这遍博客我将分类讲解一些常用的laoder 一.loaders之 预处理 css-loader 处理css中路径引用等问题 style-l ...
- 北京VS上海:“活着为了工作”还是“工作为了生活”?
Costco开业你去现场了吗?人口普查似的排队场面对于上海人来说已经不稀奇,毕竟当新鲜的商品或是业态来到中国时,上海常常是第一站.但当Costco的新闻不断发酵的同时,在互联网的角落里也有一群人提出了 ...
- 推荐一款在UBUNTU下使用的编辑器
偶然的机会 ,发现了这款软件,以前一直是在用gedit编辑器,但在WINDOWS下写的文档,在ubuntu下用gedit打开后,复制有换行的问题,一直没解决,所以在网上找到了这款软件,安装使用了几天, ...