链接共享库

时间:2015-10-06 01:09:10

标签: c linux eclipse gcc pthreads

我正在尝试编译和链接一些.c文件。我一直在为C / C ++开发人员使用Eclipse IDE,在我的本地机器上我可以毫无问题地编译。但是,当我尝试在RedHat OS中编译和链接相同的文件时(gcc版本在此操作系统中为4.9.2-6)我遇到了问题。我在编译时收到一些警告,但是这些都很好,我想,我只是被忽略了,应用程序仍然运行正常。以下是我执行的命令和相关的输出:

gcc -O0 -g3 -Wall -c -fmessage-length=0 -std=c99 -MMD -MP -MF"example.d" -MT"example.d" -o "example.o" "example.c"

warning: suggest parentheses around assignment used as truth value [-Wparentheses]
warning: implicit declaration of function ‘wait’ [-Wimplicit-function-declaration]

这会生成两个文件,example.d和example.o。然后,我尝试使用以下命令将它们连接起来:

gcc -Xlinker -L/usr/lib -lrt -static -pthread example.o -o example
/usr/bin/ld: cannot find -lrt
/usr/bin/ld: cannot find -lpthread
/usr/bin/ld: cannot find -lc
collect2: error: ld returned 1 exit status

这些命令直接来自Eclipse生成的命令,并且在我的本地计算机(Ubuntu OS)中工作得很好,但在RedHat环境中却没有。使用和不使用-L选项时,最后一个命令不起作用。我想-L中的目录很好,因为我运行,例如,

locate libpthread.so 

我得到的其中一个位置是/ usr / lib(也是/ usr / lib64,但都不起作用)。

任何帮助将不胜感激!! :)

1 个答案:

答案 0 :(得分:2)

如果您尝试链接静态可执行文件,它将查找库的*.a版本,而不是您通常想要的版本。删除-static标志。或者,如果您真的想要,可以安装静态库。也没有必要明确添加-L/usr/lib