Ubuntu gcc错误:对'log'等函数未定义的引用
Ubuntu gcc错误:对'log'等函数未定义的引用
a.c
#include <stdio.h>
#include <math.h>
int main()
{
float a;
void print_logarithm(double);
printf("enter a num:");
scanf("%f",&a);
print_logarithm(a);
return 0;
}
void print_logarithm(double x)
{
if(x<=0.0)
{
printf("Positive numbers only,please.\n");
return;
}
printf("The log of x is %f",log(x));
}
Ubuntu gcc编译
直接命令gcc -Wall a.c
1 linuxidc.com@linuxidc:~$ gcc -Wall a.c
2 /tmp/ccmKjOLq.o:在函数‘print_logarithm’中:
3 a.c:(.text+0x97):对‘log’未定义的引用
4 collect2: error: ld returned 1 exit status
查了一下发现:主要是C/C++编译为obj文件的时候并不需要函数的具体实现,只要有函数的原型即可。但是在链接为可执行文件的时候就必须要具体的实现了。验证如下:
1 linuxidc.com@linuxidc:~$ gcc -Wall -c a.c
2 linuxidc.com@linuxidc:~$ gcc -Wall -o a a.o
3 a.o:在函数‘print_logarithm’中:
4 a.c:(.text+0x97):对‘log’未定义的引用
5 collect2: error: ld returned 1 exit status
6 linuxidc.com@linuxidc:~$
编译时没有问题,链接生成可执行文件时报错。接下来加上-lm。 链接成功,如下:
1 linuxidc.com@linuxidc:~$ gcc -Wall -o a a.o -lm
2 linuxidc.com@linuxidc:~$
注意1:-lm要加在编译文件后面。
这个主要的原因是gcc编译的时候,各个文件依赖顺序的问题。在gcc编译的时候,如果文件a依赖于文件b,那么编译的时候必须把a放前面,b放后面。
注意2:sqrt()函数也是<math.h>头文件中的函数,但sqrt函数的使用没有以上限制,即编译时不加-lm也可以。
Linux升级GCC 4.8.1清晰简明教程(Ubuntu 12.04 64位版为例) http://www.linuxidc.com/Linux/2014-04/99583.htm
Ubuntu 14.04 LST安装GCC 4.1.2 http://www.linuxidc.com/Linux/2016-06/132040.htm
Ubuntu 16.04 下将GCC-5.4降级到 4.8 http://www.linuxidc.com/Linux/2017-07/145502.htm
CentOS6.5升级手动安装GCC4.8.2 http://www.linuxidc.com/Linux/2015-01/112595.htm
Ubuntu升级GCC版本 http://www.linuxidc.com/Linux/2016-11/136840.htm
CentOS7环境下在/离线安装GCC与GCC-C++ http://www.linuxidc.com/Linux/2017-03/142319.htm
Ubuntu 16.04 LTS 降级安装GCC 4.8 http://www.linuxidc.com/Linux/2017-03/142299.htm
本文永久更新链接地址:http://www.linuxidc.com/Linux/2017-09/147168.htm
Ubuntu gcc错误:对'log'等函数未定义的引用的更多相关文章
- gcc/g++ 编译时出现:“对’xxxx’未定义的引用,collect2: error: ld returned 1 exit status” 的错误
出现的问题: 在使用 make 编译实现一个程序时,出现了下面的错误.查看程序源文件所在的目录时发现程序已经完成了编译,并生成了 list_repo.o 的文件,说明是在程序链接生成可执行文件时发生了 ...
- gcc编译时对'xxxx'未定义的引用问题
gcc编译时对’xxxx’未定义的引用问题 gcc编译时对’xxxx’未定义的引用问题 原因 解决办法 gcc 依赖顺序问题 在使用gcc编译的时候有时候会碰到这样的问题,编译为.o(obj) 文件没 ...
- gcc编译时对’xxxx’未定义的引用问题
gcc编译时对’xxxx’未定义的引用问题 原因 解决办法 gcc 依赖顺序问题 在使用gcc编译的时候有时候会碰到这样的问题,编译为.o(obj) 文件没有问题,但是编译(这一步应该是链接)为可执行 ...
- C语言中未定义的引用错误
1.错误描述: /tmp/ccAu32Cb.o:在函数‘main’中:static.c:(.text+0x2d):对‘print_value’未定义的引用 2.通过对错误内容分析,我在编写程序时,ma ...
- GCC 高版本7.4 编译链接 boost 报错 boost::thread::XXX’未定义的引用 解决方法
背景:开发中的项目之前一直用GCC4.8,boost库1.48版本的开发环境.现在因业务需求,需要更换GCC7.4,boost库1.70. 问题:可以正常编译BOOST的链接库文件,但是链接时候报错. ...
- 【转】 g++编译时对'xxxx'未定义的引用问题(undefined reference to)
转自:https://blog.csdn.net/killwho/article/details/53785910 引用:http://www.linuxdiyf.com/linux/16754.ht ...
- 多线程编程-pthread 未定义的引用
多线程编程时用到函数库 pthread.h ,但是该函数库不是linux默认的函数库,所以编译c文件时,需要在目标代码文件后加上 -lpthread参数. 1.未加上 -lpthread 编译时,报错 ...
- 编译lua5.3.2报错提示libreadline.so存在未定义的引用解决方法
从官网上下载5.3.2的源码后,make linux进行编译,提示报错: gcc -std=gnu99 -o lua lua.o liblua.a -lm -Wl,-E -ldl -lreadline ...
- (原)编译caffe时提示未定义的引用(undefined reference to)
转载请注明出处: http://www.cnblogs.com/darkknightzh/p/5864715.html 参考网址: https://github.com/BVLC/caffe/issu ...
随机推荐
- 根据百度地图API得到坐标和地址并在地图上显示
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout ...
- IOS控件:分歧解决其(UILabel 和 IBAction)
#import <UIKit/UIKit.h> @interface demo7_dayViewController : UIViewController { // 用来显示程序结果 IB ...
- python 之 赋值和拷贝(你真的了解吗)
现象:先上一段代码. >>> import copy >>> a = [1,2,3,4,['a','b']] >>> b = a >> ...
- python技巧之下划线(二)
Python 用下划线作为变量前缀和后缀指定特殊变量 _xxx 不能用’from module import *’导入 __xxx__ 系统定义名字 __xxx 类中的私有变量名 核心风格:避免用下划 ...
- ural1297. Palindrome
1297. Palindrome Time limit: 1.0 secondMemory limit: 64 MB The “U.S. Robots” HQ has just received a ...
- iOS设置导航栏透明度
As I support Colin's answer, I want to give you an additional hint to customize the appearance of an ...
- LeetCode-Integer Breaks
Given a positive integer n, break it into the sum of at least two positive integers and maximize the ...
- POJ 2773 Happy 2006(容斥原理+二分)
Happy 2006 Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 10827 Accepted: 3764 Descr ...
- query_string查询支持全部的Apache Lucene查询语法 低频词划分依据 模糊查询 Disjunction Max
3.3 基本查询3.3.1词条查询 词条查询是未经分析的,要跟索引文档中的词条完全匹配注意:在输入数据中,title字段含有Crime and Punishment,但我们使用小写开头的crime来搜 ...
- caffe自定义layer
caffe自带layers: http://caffe.berkeleyvision.org/tutorial/layers.html Layers: Image Data - read raw im ...