如何使用ldconfig添加pthread库
目前,在编译使用 pthread 库函数的 C 程序时,我必须显式指定编译器选项 -lpthread 。请建议一种配置 ldconfig 的方法,以便无需在命令行上指定即可使用 pthread 库。
目前 lpthread -p 给出以下输出:=
[root@localhost lib]# ldconfig -p | grep pthread
libpthread.so.0 (libc6, OS ABI: Linux 2.6.9) => /lib/libpthread.so.0
libOpenIPMIpthread.so.0 (libc6) => /usr/lib/libOpenIPMIpthread.so.0
Currently, while compiling a C program which uses pthread library function I have to specify compiler option -lpthread explicitly. Please suggest a way to configure ldconfig so that the pthread library could be used without specifying it on the command line.
presently lpthread -p gives the following output :=
[root@localhost lib]# ldconfig -p | grep pthread
libpthread.so.0 (libc6, OS ABI: Linux 2.6.9) => /lib/libpthread.so.0
libOpenIPMIpthread.so.0 (libc6) => /usr/lib/libOpenIPMIpthread.so.0
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我不相信你能做到这一点。 LD_LIBRARY_PATH 和 ldconfig 程序用于设置共享库的路径,这些共享库用于在运行时解决可执行文件中的依赖关系。
编译时,您必须指定在链接阶段包含哪些库,即 -lpthread
I don't believe you can do that. The LD_LIBRARY_PATH and ldconfig program are used to set up the paths to shared libraries that are used to resolve dependencies in your executable at runtime.
When compiling you will have to specify which libraries to include in the linking phase, i.e. -lpthread
您可以设置 LD_LIBRARY_PATH 环境变量。
You can set the LD_LIBRARY_PATH env variable.
pthreads 库是一个动态库。这意味着,为了使用其功能,与二进制文件不同的文件必须在运行时可用。
您可以通过多种方式做到这一点。设置
-lpthread
将自动设置您想要的编译器标志。通过LD_PRELOAD
提供共享文件也能起到同样的作用。或者您的 pthreaded 程序显式地dlopen
库。但正确的方法是提供正确的编译器开关。
The pthreads library is a dynamic library. This means that, in order to use its functionality, a file distinct from your binary must be available at run time.
You can do this in several ways. Setting
-lpthread
will automatically set the compiler flags you want. Providing the shared file viaLD_PRELOAD
will do the same. Or you have your pthreaded programdlopen
the library explicitly.But the right way to do it is to provide the right compiler switches.