本人原创文章,文章是在此代码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. SWUST OJ Gold Nuggets Distribution(0490)

    Gold Nuggets Distribution(0490) Time limit(ms): 1000 Memory limit(kb): 65535 Submission: 421 Accepte ...

  2. python自动化测试技术-Allure

    文末有源码 大部分人可能做的是爬虫和web,数据分析方面的工作,今天分享个在自动化测试领域python能做什么样的事情,比如下方,是用python+pytest+allure生成的精美自动化测试报告, ...

  3. [JS奇怪的世界]No.55 危險小叮嚀:陣列與for in

    前言 前面已經瞭解了使用內建函數建構子的某些危險地方,但其實陣列與for in,也是有一些危險的地方. 陣列與for in 在前面幾個章節有講過陣列就是物件,所以我們一樣可以使用 for in來做處理 ...

  4. css之制作三角形

    箭头向上三角形 #triangle-up { width: 0; height: 0; border-left: 50px solid transparent; border-right: 50px ...

  5. Java线程知识

    概念 线程生命周期 Java线程模型 线程方法 线程优先级 线程同步 线程在多任务处理应用程序中有着至关重要的作用 概念 基本概念 进程:在操作系统中每个独立运行的程序就是一个进程 线程:程序执行的一 ...

  6. Android入门(创建、编译、运行、打包、安装)

    一.创建Android项目 1.选择Emtpy Activity=>Next 2.配置项目 输入Name(名称) Package.name(包名) Save location(保存位置) Lan ...

  7. 一条SQL在内存结构与后台进程工作机制

    oracle服务器由数据库以及实例组成,数据库由数据文件,控制文件等物理文件组成,实例是由内存结构+后台进程组成,实例又可以看做连接数据库的方式,在我看来就好比一家公司,实例就是一个决策的办公室,大大 ...

  8. C++ 走向远洋——44(项目一、点—圆—圆柱类族的设计、派生类)

    */ * Copyright (c) 2016,烟台大学计算机与控制工程学院 * All rights reserved. * 文件名:text.cpp * 作者:常轩 * 微信公众号:Worldhe ...

  9. scrapy爬虫-scrapy-redis分布式

    1.如何将一个scrapy爬虫项目修改成为一个简单的分布式爬虫项目 官方文档:https://scrapy-redis.readthedocs.io/en/stable/ 只用修改scrapy项目的两 ...

  10. 第六周学习笔记,vc各类控件的输入输出

    6w学习笔记 vc控件的输入输出 单选按钮 当单击 RadioButton 控件时,其 Checked 属性设置为 true,并且调用 Click 事件处理程序.当 Checked 属性的值更改时,将 ...