C:对地板的未定义参考

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

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 

谁能帮我解决问题是什么以及如何解决?

您需要在目标文件之后链接库。

你有:

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

你需要:

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

链接器的工作方式似乎发生了变化 – 在某些时候,可以在目标文件之前指定共享库(例如数学库),并且一切都可以工作。 但是,如今,如果共享库在扫描时不满足任何符号,则在链接过程中将其省略。 在库修复此问题之前确保列出目标文件。

另请参见未定义的’pthread_create’参考 ; 同样的问题,相同的解决方 我怀疑这是否是SO中唯一的问题。

您需要链接数学库,即在链接行的末尾添加-lm 。 不知道怎么在Eclipse中这样做,对不起。

请注意,输出中的链接标志看起来不正常。 也许您试图通过Eclipse中的Linker Flags添加-lm 。 这会导致Eclipse出现问题。 我建议你试试……

右键单击您的项目 – >属性 – > C / C ++构建 – >设置 – > GCC链接器 – >图书馆 – >添加“m” – >应用 – >构建

或者,在列表中,确保-L和-l参数位于链接过程中的.o文件之后。

我今天刚受到这种打击,一段时间没有让我失望。

如果只需要floor函数,则使用int或long cast,它会返回相同的结果。

 float x = 3.5; int y = (int) x;