1. 程式人生 > >折騰gcc/g++連結時.o檔案及庫的順序問題

折騰gcc/g++連結時.o檔案及庫的順序問題

先看看與動態庫連結相關的幾個選項的man說明:

--as-needed
--no-as-needed
    This option affects ELF DT_NEEDED tags for dynamic libraries mentioned on the command line after the --as-needed
    option.  Normally the linker will add a DT_NEEDED tag for each dynamic library mentioned on the command line,
    regardless of whether the library is actually needed or not.  --as-needed causes a DT_NEEDED tag to only be emitted for
    a library that satisfies an undefined symbol reference from a regular object file or, if the library is not found in
    the DT_NEEDED lists of other libraries linked up to that point, an undefined symbol reference from another dynamic
    library.  --no-as-needed restores the default behaviour.

--add-needed
--no-add-needed
    These two options have been deprecated because of the similarity of their names to the --as-needed and --no-as-needed
    options.  They have been replaced by --copy-dt-needed-entries and --no-copy-dt-needed-entries.

--copy-dt-needed-entries
--no-copy-dt-needed-entries
    This option affects the treatment of dynamic libraries referred to by DT_NEEDED tags inside ELF dynamic libraries
    mentioned on the command line.  Normally the linker won't add a DT_NEEDED tag to the output binary for each library
    mentioned in a DT_NEEDED tag in an input dynamic library.  With --copy-dt-needed-entries specified on the command line
    however any dynamic libraries that follow it will have their DT_NEEDED entries added.  The default behaviour can be
    restored with --no-copy-dt-needed-entries.

    This option also has an effect on the resolution of symbols in dynamic libraries.  With --copy-dt-needed-entries
    dynamic libraries mentioned on the command line will be recursively searched, following their DT_NEEDED tags to other
    libraries, in order to resolve symbols required by the output binary.  With the default setting however the searching
    of dynamic libraries that follow it will stop with the dynamic library itself.  No DT_NEEDED links will be traversed to
    resolve symbols.

再看看ld的幫助

--add-needed                Set DT_NEEDED tags for DT_NEEDED entries in   following dynamic libs
--no-add-needed             Do not set DT_NEEDED tags for DT_NEEDED entries   in following dynamic libs


--as-needed                 Only set DT_NEEDED for following dynamic libs if used
--no-as-needed              Always set DT_NEEDED for following dynamic libs

關鍵的看–as-needed,意思是說:只給用到的動態庫設定DT_NEEDED。比如:

g++ -shared  PyGalaxy.o -lGalaxyParser -lxxx  -lrt  -o PyGalaxy.so

像這樣連結一個PyGalaxy.so的時候,假設PyGalaxy.so裡面用到了libGalaxyParser.so但是沒 有用到libxxx.so。檢視依賴關係如下:

[email protected]:~$ readelf -d PyGalaxy.so 

Dynamic section at offset 0x7dd8 contains 26 entries:
  Tag        Type                         Name/Value
 0x0000000000000001 (NEEDED)             Shared library: [libGalaxyParser.so]
 0x0000000000000001 (NEEDED)             Shared library: [libxxx.so]
 ...

當開啟–as-needed的時候,像

g++ -shared  -Wl,--as-needed PyGalaxy.o -lGalaxyParser -lxxx  -lrt  -o PyGalaxy.so

這樣連結PyGalaxy.so的時候,檢視依賴關係如下:

[email protected]:~$ readelf -d PyGalaxy.so 

Dynamic section at offset 0x7dd8 contains 26 entries:
  Tag        Type                         Name/Value
 0x0000000000000001 (NEEDED)             Shared library: [libGalaxyParser.so]
 ...

–as-needed就是忽略連結時沒有用到的動態庫,只將用到的動態庫set NEEDED。

開啟–as-needed的一些常見的問題:

相關推薦

折騰gcc/g++連結.o檔案順序問題

先看看與動態庫連結相關的幾個選項的man說明: --as-needed --no-as-needed This option affects ELF DT_NEEDED tags for dynamic libraries mentioned on the command line after th

linux下gcc預設搜尋標頭檔案檔案的路徑

linux下gcc預設搜尋標頭檔案及庫檔案的路徑 一、標頭檔案gcc 在編譯時如何去尋找所需要的標頭檔案:※所以header file的搜尋會從-I開始※然後找gcc的環境變數 C_INCLUDE_PATH,CPLUS_INCLUDE_PATH,OBJC_INCLUDE_

gcc編譯連結標頭檔案檔案的搜尋順序

編譯:找符號定義 連結:找實現 執行:執行 靜態庫連結時直接寫程序序裡了 動態庫連結時只連結到了一些地址資訊,需要到執行時再進行動態載入 編譯時搜尋標頭檔案的順序: 1.  gcc先找-I設定的路徑 2.  再找gcc的環境變數C_INCLUDE_PATH, CPLU

gcc/g++ 連結的編譯與連結

 gcc/g++ 連結庫的編譯與連結 [email protected] http://blog.csdn.net/surgewong       程式編譯一般需要經預處理、編譯、彙編和連結幾個步驟。在實際應用中,有些公

android編譯拷貝檔案資料夾

拷貝檔案PRODUCT_COPY_FILES += device/qcom/msm8909/media/media_profiles_8909.xml:system/etc/media_profiles.xml拷貝資料夾PRODUCT_COPY_FILES += $(cal

(轉載)C語言中常用的幾個標頭檔案函式 (stdio.h ,string.h ,math.h ,stdlib.h)

不完全統計,C語言標準庫中的標頭檔案有15個之多,所以我主要介紹常用的這四個標頭檔案stdio.h ,string.h ,math.h ,stdlib.h ,以後用到其他的再做補充。下面上乾貨: 1.<stdio.h>:定義了輸入輸出函式、型別以及巨集,函式

Ubuntu下GCC引用mysql標頭檔案檔案

1.安裝mysql-server:    sudo apt-get install mysql-server-5.1 2.gcc連線mysql的庫安裝:    sudo apt-get install libmysqlclient-dev   安裝後,標頭檔案在/usr/include/mysql,庫檔案在

gcc連結相關so或者a檔案出現 undefined reference to "xxx"

gcc 在連結相關so或者a檔案時出現 undefined reference to "xxx", 首先確認是否有這個xxx介面的真身,然後查明其所在的庫檔案,之後在當前makefile裡新增就行。 可是今天遇到一種特殊情況,在如下的libs後面直接新增libapps_vram.a檔案時

linux下g++ 編譯動態和靜態連結和標頭檔案問題

原來編譯的時候都是用的很隨意,沒用系統的總結一下,這幾天在編譯的時候遇到一些下問題,於是就總結一下,省得過幾天又給忘了。 1.動態庫和靜態庫簡介 靜態庫在程式連結的時候會自動的連結到程式裡,所以一旦編譯完成,靜態庫就不需要了,靜態庫以.a結尾。  動態庫在編譯時不會被連線到目的碼中,而是在程式執行

博科SAN交換機學習筆記之二:配置檔案備份與韌體升級 作者 LiaoJL | 轉載請務必以超連結形式標明文章原文連結和作者資訊本版權宣告。 原文連結:http://www.liaojl.co

配置檔案恢復 當需要備份中恢復交換機配置時,可以通過configdownload命令將博科交換機的配置從遠端伺服器恢復到交換機。博科交換機支援將舊版本的配置檔案匯入新版本韌體的交換機,例如將v6.2.0的配置檔案匯入v6.3.0韌體版本的交換機,或者將v6.4.1 配置檔案匯入 v7.0.0 版本的交換機。

gcc/g++使用第三方新增標頭檔案路徑和檔案路徑的方法

本文總結了使用第三方庫函式時將其路徑告訴編譯器(gcc和g++同理)的2種常用方式,並舉例說明了每種方式的具體用法。方法一:在編譯自己的專案時新增-L和-I編譯選項1)新增標頭檔案路徑:-I     #指明標頭檔案的路徑2)新增庫檔案路徑:-L    #指定目錄。link的

gcc -g -o -c分別是什麽意思

gcc linux-g為了調試用的 加個-g 是為了gdb 用,不然gdb用不到-o output_filename,確定輸出文件的名稱為output_filename,同時這個名稱不能和源文件同名。如果不給出這個選項,gcc就給出預設的可執行文件a.out。-E:僅執行編譯預處理; -S:將C代碼轉換為匯編

0045-OpenCV2.4.9下載地址連結器配置檔案列表

因為OpenCV3.0的nonfree模組官方並沒有編譯,所以有時候我們使用OpenCV2.4.9進行影象處理的開發。 OpenCV2.4.9下載連結:https://pan.baidu.com/s/1o8FsD2i 密碼:n6ak 配置方法見帖子:https://blog.csdn.net

makefile生成 *.d 依賴檔案 gcc -M -MF -MP 等相關選項說明

1. 為什麼要使用字尾名為 .d 的依賴檔案? 在 Makefile 中, 我們的依賴關係可能需要包含一系列的標頭檔案。  比如  main.c 原始檔內容如下: #include "stdio.h" #include "defs.h" int main(int a

使用Python+opencv2檔案命名路徑問題

  最近在做一個數字影象的小專案,在最後的介面與程式結合階段總是出現單個程式可以執行,但是使用介面傳遞的引數就執行不了的情況。在網上查了很多相關錯誤,最終確定是檔案命名問題。 錯誤如下: cv2.error: OpenCV(3.4.1) C:\Miniconda3\conda-bld\opencv-sui

SQL Server2017還原資料庫指定mdf檔案日誌檔案的名稱

由於需要還原同一個資料庫的不同備份到不同資料庫中,可是在還原的時候,可是在指定目標資料庫時,填寫不同的資料庫名稱,在SQL Server Data資料夾中生成的.mdf檔案還是同一個,如圖,雖然是很簡單的一個操作,在這裡記錄一下,以防忘記。 折騰了半天。之前記得可以把資料庫分離,然後直接修改mdf檔案,

Linux下重要命令,許可權gcc/g++,gdb,vim的安裝

Linux重要命令: su 切換使用者 -c<指令>或–command=<指令>:執行完指定的指令後,即恢復原來的身份; -f或——fast:適用於csh與tsch,使shell不用去讀取啟動檔案; -l或——login:改變身份時,

Linux系統使用入門進階總結(6)——Ubuntu下gcc/g++編譯連結過程

文章轉自: https://blog.csdn.net/VennyJin/article/details/82794331 這裡講的是最簡單的c/c++檔案在linux下編譯連結的過程,後期還可以使用cmake來完成更復雜的工程構建過程。請關注博主的後續文章哈~~~ Ubuntu下gcc

gen_tcp接受連結enfile的問題分析解決

最近我們為了安全方面的原因,在RDS伺服器上做了個代理程式把普通的MYSQL TCP連線變成了SSL連結,在測試的時候,皓庭同學發現Tsung發起了幾千個TCP連結後Erlang做的SSL PROXY老是報告gen_tcp:accept返回{error, enfile}錯誤。針對這個問題,我展開了如下的

系統技術非業餘研究 » gen_tcp接受連結enfile的問題分析解決

最近我們為了安全方面的原因,在RDS伺服器上做了個代理程式把普通的MYSQL TCP連線變成了SSL連結,在測試的時候,皓庭同學發現Tsung發起了幾千個TCP連結後Erlang做的SSL PROXY老是報告gen_tcp:accept返回{error, enfile}錯誤。針對這個問題,我展開了