当一个共享库具有相同的名称时,我如何强制连接静态库?

时间:2022-09-18 11:47:56

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?

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

3 个解决方案

#1


21  

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

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

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

#2


9  

If your linker supports -l:<filename> you may use:

如果您的链接器支持-l: <文件名> ,您可以使用:

g++ -o main main.cpp -l:libmath.a

#3


5  

Use this function:

使用这个函数:

g++ -o main main.cpp /path_to/libmath.a

#1


21  

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

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

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

#2


9  

If your linker supports -l:<filename> you may use:

如果您的链接器支持-l: <文件名> ,您可以使用:

g++ -o main main.cpp -l:libmath.a

#3


5  

Use this function:

使用这个函数:

g++ -o main main.cpp /path_to/libmath.a