当存在同名的共享库时,如何强制与静态库链接 [英] How can I force linking with a static library when a shared library of same name is present

查看:344
本文介绍了当存在同名的共享库时,如何强制与静态库链接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一个使用 sin()函数的文件 main.cpp ,它在 libmath 。另外假设我们在同一个目录中都有libmath.a和libmath.so。现在,如果我发出命令 g ++ -o main main.cpp -lmath ,Linux的默认行为是链接到共享库 libmath.so 。我想知道是否有办法强制程序与静态库 libmath.a 链接,而不删除或移动共享库?

解决方案

您需要将-static传递给链接器,但仅限于您需要的特定库。例如:

  g ++ -o main main.cpp -Wl,-Bstatic -lmath -Wl,-Bdynamic 


Suppose I have a file main.cpp which uses sin() function which is defined in libmath. Also suppose that we have both libmath.a and libmath.so available in the same directory. Now if I issue the command g++ -o main main.cpp -lmath the default behaviour of Linux is to link to the shared library libmath.so. I want to know is there a way to force the program to link with the static library libmath.a without deleting or moving the shared library?

解决方案

You'll need to pass the -static to the linker, but only for particular libraries you want. e.g.:

g++ -o main main.cpp -Wl,-Bstatic -lmath -Wl,-Bdynamic

这篇关于当存在同名的共享库时,如何强制与静态库链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆