Linux C++ 链接器 /usr/bin/ld

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9875772/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 05:25:03  来源:igfitidea点击:

Linux C++ linker /usr/bin/ld

c++linuxlinker

提问by 2607

I wrote a small application on Redhat Linux 6 using g++ 4.4.6. After compilation, I received an error

我使用 g++ 4.4.6 在 Redhat Linux 6 上编写了一个小应用程序。编译后,我收到一个错误

/usr/bin/ld: cannot find -lcrypto

I did a search for the crypto library and find them here,

我搜索了加密库并在这里找到了它们,

[root@STL-DUNKEL01 bin]# find / -name libcrypto*
/usr/lib64/libcrypto.so.0.9.8e
/usr/lib64/libcrypto.so.10
/usr/lib64/libcrypto.so.6
/usr/lib64/libcrypto.so.1.0.0

My question is whether the compilation error is caused by /usr/bin/ldnot having /usr/lib64/in the search path? If yes, how can I add it?

我的问题是编译错误是否是由于/usr/bin/ld没有/usr/lib64/在搜索路径中引起的?如果是,我该如何添加它?

Thanks.

谢谢。

采纳答案by Gangadhar

You can provide the directories to search for the libraries in as a parameter to gcclike so -L<directory_to_search_in>. And note that there can be multiple parameters to -L. Also, are you trying to build a 32-bit application or a 64-bit one?

您可以提供的目录来搜索作为参数库gcc像现在这样-L<directory_to_search_in>。请注意,可以有多个参数-L。另外,您是在尝试构建 32 位应用程序还是 64 位应用程序?

回答by Attila

You have to add -L/usr/lib64when calling gcc or ld.

-L/usr/lib64调用 gcc 或 ld 时必须添加。

Note, you can specify LD_LIBRARY_PATH as well/instead, but it is considered harmfulto do so. (The link mentions Solaris specifically, but the issues apply to other OSs as well.)

请注意,您也可以/代替指定 LD_LIBRARY_PATH,但这样做被认为是有害的。(该链接特别提到了 Solaris,但这些问题也适用于其他操作系统。)

Quote:

引用:

  • LD_LIBRARY_PATH is used in preference to any run time or default system linker path. If (God forbid) you had it set to something like /dcs/spod/baduser/lib, if there was a hacked version of libc in that directory (for example) your account could be compromised. It is for this reason that set-uid programs completely ignore LD_LIBRARY_PATH.
  • When code is compiled and depends on this to work, it can cause confusion where different versions of a library are installed in different directories, for example there is a libtiff in /usr/openwin/lib and /usr/local/lib. In this case, the former library is an older one used by some programs that come with Solaris.
  • Sometimes when using precompiled binaries they may have been built with 3rd party libraries in specific locations; ideally code should either ship with the libraries and install into a certain location or link the code as a pre-installation step. Solaris 7 introduces $ORIGIN which allows for a relative library location to be specified at run time (see the Solaris Linker and Libraries Guide). The alternative is to set LD_LIBRARY_PATH on a per-program basis, either as a wrapper program to the real program or a shell alias. Note however, that LD_LIBRARY_PATH may be inherited by programs called by the wrapped one ...
  • LD_LIBRARY_PATH 优先于任何运行时或默认系统链接器路径使用。如果(上帝保佑)您将其设置为 /dcs/spod/baduser/lib 之类的内容,如果该目录中存在被黑客入侵的 libc 版本(例如),您的帐户可能会被盗用。正是因为这个原因,set-uid 程序完全忽略了 LD_LIBRARY_PATH。
  • 当代码被编译并依赖于此工作时,可能会在不同版本的库安装在不同目录中的情况下引起混淆,例如在 /usr/openwin/lib 和 /usr/local/lib 中有一个 libtiff。在这种情况下,前一个库是 Solaris 附带的某些程序使用的旧库。
  • 有时,当使用预编译的二进制文件时,它们可能是在特定位置使用 3rd 方库构建的;理想情况下,代码应该随库一起提供并安装到某个位置,或者将代码链接为预安装步骤。Solaris 7 引入了 $ORIGIN,它允许在运行时指定相对库位置(请参阅 Solaris 链接器和库指南)。另一种方法是在每个程序的基础上设置 LD_LIBRARY_PATH,作为真实程序的包装程序或 shell 别名。但是请注意,LD_LIBRARY_PATH 可能会被包装的程序调用的程序继承......

回答by Karoly Horvath

Add the directory to /etc/ld.so.conf

将目录添加到 /etc/ld.so.conf

then run "sudo ldconfig" to make the changes take effect.

然后运行“sudo ldconfig”使更改生效。

回答by ephemient

No, you have likely incorrectly diagnosed the cause.

不,您可能错误地诊断了原因。

You need a libcrypto.soto link against. This is usually a symlink to one of the actual libraries, whose soname (libcrypto.so.??) will be embedded into the binary. Only that library is needed at runtime, but the symlink is necessary to compile.

你需要一个libcrypto.so链接。这通常是指向实际库之一的符号链接,其 soname ( libcrypto.so.??) 将嵌入到二进制文件中。运行时只需要那个库,但编译时需要符号链接。

See Diego E. Pettenò: Linkers and namesfor more details.

有关更多详细信息,请参阅Diego E. Pettenò:链接器和名称