最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

How to 'link-whole' external static library in meson? - Stack Overflow

programmeradmin0浏览0评论

I'm using GCC/Ubuntu. I need to link my app against external (pre-installed) static library.

It needs to be wrapped with '-Wl,--whole-archive' ... '-Wl,--no-whole-archive'

My naive approach is:

lib = cc.find_library('lib.a', dirs : '/path/to/lib', required : true, static: true)
lib_dep = declare_dependency(link_whole: lib)

Unfortunately, it produces the error below: ERROR: declare_dependency keyword argument "link_whole" can only be self-built targets, external dependencies (including libraries) must go in "dependencies".

Obviously, if I replace 'link_whole' with 'dependencies', it kind of 'works'. Though, I don't get what I want.

I'm not considering messing up with 'link_args', since meson was found reordering args on its own.

Any help appreciated. Thanks.

I'm using GCC/Ubuntu. I need to link my app against external (pre-installed) static library.

It needs to be wrapped with '-Wl,--whole-archive' ... '-Wl,--no-whole-archive'

My naive approach is:

lib = cc.find_library('lib.a', dirs : '/path/to/lib', required : true, static: true)
lib_dep = declare_dependency(link_whole: lib)

Unfortunately, it produces the error below: ERROR: declare_dependency keyword argument "link_whole" can only be self-built targets, external dependencies (including libraries) must go in "dependencies".

Obviously, if I replace 'link_whole' with 'dependencies', it kind of 'works'. Though, I don't get what I want.

I'm not considering messing up with 'link_args', since meson was found reordering args on its own.

Any help appreciated. Thanks.

Share Improve this question asked Nov 21, 2024 at 8:31 Evgenii AstafevEvgenii Astafev 1 4
  • Welcome to SO. If you're linking an application with this static library, what is the need for --whole-archive? - since the linker will pull in any archive members that are actually referenced. – Mike Kinghan Commented Nov 24, 2024 at 15:48
  • The static library has a constructor, which is not being called by default. – Evgenii Astafev Commented Nov 25, 2024 at 16:15
  • A shared library can have a constructor function. A static library cannot: it is just an archive of object files and has no runtime existence. It seems likely you have an X/Y problem. If you edit your question to show us a Minimal Reproducible Example of the problem linkage you are likely to get useful answers. – Mike Kinghan Commented Nov 25, 2024 at 16:34
  • Each object file in the archive may contain multiple constructors. They are not being called in my case, because I'm not referencing anything from the object files. This happens due to linker optimizations. In order to force the linker to include the object file is to provide link-whole argument. – Evgenii Astafev Commented Nov 26, 2024 at 17:24
Add a comment  | 

1 Answer 1

Reset to default 0

The main issue you have here is that compiler.find_library returns a dep object, i.e. it is already a dependency. As such, it cannot occur in the link_whole argument for declare_dependency, which accepts only lib values, i.e. static/shared libraries buildable by meson.

发布评论

评论列表(0)

  1. 暂无评论