C:未定义的楼层参考 [英] C: Undefined reference to floor

查看:249
本文介绍了C:未定义的楼层参考的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Ubuntu上使用Eclipse编写/编译/运行C代码。
我正在尝试构建我的项目。
以下是Eclipse控制台中的输出。

  22:18:31 ****配置的构建Debug对于Project1 **** 
使所有
构建文件:../project1.c
调用:GCC C编译器
gcc -I / lib / i386-linux-gnu -O0 -g3 -Wall -c -fmessage-length = 0 -pthread -lm -MMD -MP -MFproject1.d-MTproject1.d-oproject1.o../project1.c
../project1.c:在函数'main'中:
../project1.c:146:6:警告:未使用的变量'this_thread_id'[-Wunused-variable]
../project1.c:在函数'_pre_init'中:
../project1.c:126:1:warning:控制到达非void函数的结尾[-Wreturn-type]
完成的建筑物:../project1.c

构建文件:../scheduler.c
调用:GCC C编译器
gcc -I / lib / i386-linux-gnu -O0 -g3 -Wall -c -fmessage-length = 0 -pthread -lm -MMD -MP -MFscheduler.d-MTscheduler.d-oscheduler.o../scheduler.c
完成建筑:../scheduler.c

建筑目标:Project1
调用:GCC C链接器
gcc -L / lib / i386-linux-gnu -lm -pthread -oProject1./project1.o ./scheduler.o
./project1 .o:在函数`advance_global_time'中:
/home/akshay/Cworkspace/Project1/Debug/../project1.c:50:未定义对`floor'的引用
collect2:ld返回1退出状态
make:*** [Project1] Error 1

任何人都可以帮我弄清楚问题是什么以及如何解决它?

解决方案

您需要在对象文件之后链接库。 b
$ b

您有:

  gcc -L / lib / i386-linux-gnu -lm -pthread -oProject1./project1.o ./scheduler.o 

您需要:

  gcc -L / lib / i386-linux-gnu -pthread -oProject1./project1.o ./scheduler。 o -lm 

连接器的工作方式似乎有所改变 - 有可能指定共享li在目标文件之前进行数据处理(比如数学库),所有这些都可以工作。但是,现在,如果共享库在扫描时不满足任何符号,则在链接过程中将忽略它。确保目标文件在库修复之前列出。

-to-pthread-create>未定义的引用'pthread_create';同样的问题,相同的解我怀疑这是否是唯一的这样的问题。


I am using Eclipse on Ubuntu to write/compile/run C code. I am trying to build my project. Following is the output in the Eclipse console.

22:18:31 **** Build of configuration Debug for project Project1 ****
make all 
Building file: ../project1.c
Invoking: GCC C Compiler
gcc -I/lib/i386-linux-gnu -O0 -g3 -Wall -c -fmessage-length=0 -pthread -lm -MMD -MP -MF"project1.d" -MT"project1.d" -o "project1.o" "../project1.c"
../project1.c: In function ‘main’:
../project1.c:146:6: warning: unused variable ‘this_thread_id’ [-Wunused-variable]
../project1.c: In function ‘_pre_init’:
../project1.c:126:1: warning: control reaches end of non-void function [-Wreturn-type]
Finished building: ../project1.c

Building file: ../scheduler.c
Invoking: GCC C Compiler
gcc -I/lib/i386-linux-gnu -O0 -g3 -Wall -c -fmessage-length=0 -pthread -lm -MMD -MP -MF"scheduler.d" -MT"scheduler.d" -o "scheduler.o" "../scheduler.c"
Finished building: ../scheduler.c

Building target: Project1
Invoking: GCC C Linker
gcc -L/lib/i386-linux-gnu -lm -pthread -o "Project1"  ./project1.o ./scheduler.o   
./project1.o: In function `advance_global_time':
/home/akshay/Cworkspace/Project1/Debug/../project1.c:50: undefined reference to `floor'
collect2: ld returned 1 exit status
make: *** [Project1] Error 1

Can anyone please help me figure out what the problem is and how to solve it?

解决方案

You need to link libraries after the object files.

You have:

gcc -L/lib/i386-linux-gnu -lm -pthread -o "Project1"  ./project1.o ./scheduler.o   

You need:

gcc -L/lib/i386-linux-gnu -pthread -o "Project1"  ./project1.o ./scheduler.o -lm 

There seems to have been a change in the way the linkers work — at some time, it was possible to specify shared libraries (such as the maths library) before the object files, and all would work. However, nowadays, if the shared library doesn't satisfy any symbols when it is scanned, it is omitted from the link process. Ensuring that the object files are listed before the libraries fixes this.

See also Undefined reference to 'pthread_create'; same problem, same solution. And I doubt if that is the only such question in SO.

这篇关于C:未定义的楼层参考的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆