简体   繁体   English

静态库中的链接失败,但共享库中的链接成功

[英]Linking in a static library failes, but linking a shared library succeeds

I can build my application against the shared library but I'm getting the unresolved symbol errors when linking it against the static version of the same library: 我可以针对共享库构建应用程序,但将其与同一库的静态版本链接时出现未解决的符号错误:

I can build my application this way: 我可以这样构建我的应用程序:

g++ -lutils application.cpp -o application.exe

The above command links in the shared version of an utils library. 上面的命令链接了utils库的共享版本。

I'm trying link against the static version of the library like this: 我正在尝试像这样的静态版本的库链接:

g++ -l:utils.a application.cpp -o application.exe

Both times I'm using 两次我都在使用

export LD_LIBRARY_PATH=path/to/utils:$LD_LIBRARY_PATH

to inform g++ where utils.a is placed. 通知g ++ utils.a的放置位置。

The unresolved symbol reported by ld is present in the output of the nm: ld报告的未解析符号出现在nm的输出中:

nm --defined-only path/to/utils.a

and is marked with the "T" (meaning that it is from the code section). 并标有“ T”(表示它来自代码部分)。

I'm trying to figure out what can be the reason of the problem. 我正在尝试找出问题的原因。

Is it correct to use LD_LIBRARY_PATH to specify where to search for utils.a? 使用LD_LIBRARY_PATH指定在哪里搜索utils.a是否正确?

What is the exact command to verify that a static library defines (resolves) the symbol? 验证静态库定义(解析)符号的确切命令是什么? Is the command 是命令

nm --defined-only path/to/utils.a

enough or should I use any additional options like 足够还是我应该使用其他任何选项,例如

nm --defined-only --demangle path/to/utils.a

eg? 例如?

Just option -static should be enough for compiler. 对于编译器来说,仅选项-static应该足够了。 In case only one library has to be static, then -static- and lib name is short name not file name. 如果只有一个库必须是静态的,则-static-和lib name是短名称而不是文件名。

Is it correct to use LD_LIBRARY_PATH to specify where to search for utils.a? 使用LD_LIBRARY_PATH指定在哪里搜索utils.a是否正确?

  1. As mentioned by @user10605163, LD_LIBRARY_PATH is not to find path to static library at compile and link time. 如@ user10605163所述,LD_LIBRARY_PATH不会在编译和链接时找到静态库的路径。 It is an environment variable used in some Linux distribution to search shared libraries during run time. 它是一个环境变量,在某些 Linux发行版中用于在运行时搜索共享库。 Please find more documentation here It is useful for build and test environment but not a recommended way of linking in production systems. 在此处找到更多文档它对于构建和测试环境很有用,但不是在生产系统中推荐的链接方式。

What is the exact command to verify that a static library defines (resolves) the symbol? 验证静态库定义(解析)符号的确切命令是什么? Is the command nm --defined-only path/to/utils.a 是命令nm --defined-only path / to / utils.a

  1. Yes, that is correct. 对,那是正确的。 However based on the information provided this error is not likely an error with symbols not present in utils(as it worked with shared library), but with the linking. 但是,基于提供的信息,该错误不太可能是utils中不存在符号的错误(因为它与共享库一起使用),但具有链接。

Refer GNU documentation GCC link options Excerpt: 请参阅GNU文档GCC链接选项摘录:

-l library : Search the library named library when linking. -l library:链接时搜索名为library的库。 The linker searches a standard list of directories for the library. 链接器在标准目录列表中搜索该库。 The directories searched include several standard system directories plus any that you specify with -L. 搜索的目录包括几个标准系统目录以及您使用-L指定的目录。

Also, with -l link option you need to provide the library name (without 'lib' and extension) or full file name. 另外,使用-l链接选项,您需要提供库名(不带“ lib”和扩展名)或完整文件名。 -lutils or -llibutils.a You can also provide direct full path here only, if required. -lutils或-llibutils.a如果需要,也可以仅在此处提供直接的完整路径。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM