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

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

问题描述

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

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?

推荐答案

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

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天全站免登陆