[英]LD: Linking a shared library to a static library
我正在嘗試將static library
與shared library
鏈接,這引發了一個錯誤recompile with -fPIC
這是我嘗試使用的示例:
savari@Ramana:~/Junk$ cat common.h
#include <stdio.h>
#include <stdlib.h>
void func1(int *p);
void func2();
共享庫代碼:
savari@Ramana:~/Junk$ cat shared.c
#include "common.h"
void func2()
{
int i=10;
func1(&i);
}
和靜態庫代碼:
#include "common.h"
int k;
void func1(int *p)
{
printf("%d\n", *p);
}
現在,看到共享庫使用靜態庫的API。
這是我的編譯方式:
gcc -c static.c
ar rcs libStatic.a static.o
gcc -c shared.c
gcc -shared -fPIC -o libShared.so shared.o -L. -lStatic
最后一條命令后,出現以下錯誤:
/usr/bin/ld: ./libStatic.a(static.o): relocation R_X86_64_32 against `.rodata' can not be used when making a shared object; recompile with -fPIC
./libStatic.a: error adding symbols: Bad value
collect2: error: ld returned 1 exit status
我實際上是從供應商那里獲得的static library
,我正在嘗試在其上構建一個shared library
。 我沒有靜態庫的來源。
我收到其他類型的錯誤信息:
relocation R_ARM_THM_MOVW_ABS_NC against `a local symbol' can not be used when making a shared object; recompile with -fPIC
error adding symbols: Bad value
collect2: error: ld returned 1 exit status
我讀了很多文章,但無法弄清楚。 請幫我解決這個問題。
參考文獻:
在上面的示例中,在為靜態庫編譯目標文件時需要使用-fPIC
。 如果省略此選項,則編譯后的代碼將無法包裝到共享庫中。 如果您只有靜態庫而沒有源代碼,那么您將無能為力。 請庫的創建者為您提供啟用-fPIC
的編譯器。
試試這個靜態鏈接libStatic.a:
gcc -shared -fPIC -o libShared.so shared.o -L. -Wl, -Bstatic -lStatic
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.