2012-12-27 42 views
0

我在Linux上構建了一個共享庫(.so),並且我正在編譯和鏈接某個對象,該對象不是在某些函數中動態鏈接的。這些功能是「外部'C''功能。它們出現在共享對象的普通符號表中,但不出現在外部函數列表或動態函數列表中。我正在編譯和使用g ++進行鏈接。我已經關閉了編譯器優化。是否需要設置特殊標誌才能使功能出現?共享庫不能在函數中鏈接

編輯: 我用的對象文件鏈接在一起的命令是這樣的:

g++ -m64 -rpath,/usr/local/Trolltech/Qt-4.8.2/lib -shared -Wl,-soname,libQtCommercialChart.so.1 -o libQtCommercialChart.so.1.0.0 ../build/release/lib/chartdataset.o ../build/release/lib/chartpresenter.o ../build/release/lib/charttheme.o ../build/release/lib/domain.o ../build/release/lib/qchart.o ../build/release/lib/qchartview.o ../build/release/lib/qabstractseries.o ../build/release/lib/chartbackground.o ../build/release/lib/chartelement.o ../build/release/lib/scroller.o ../build/release/lib/chartlayout.o ../build/release/lib/versiontracker.o ../build/release/lib/axisanimation.o ../build/release/lib/xyanimation.o ../build/release/lib/pieanimation.o ../build/release/lib/piesliceanimation.o ../build/release/lib/splineanimation.o ../build/release/lib/baranimation.o ../build/release/lib/stackedbaranimation.o ../build/release/lib/percentbaranimation.o ../build/release/lib/abstractbaranimation.o ../build/release/lib/horizontalbaranimation.o ../build/release/lib/horizontalstackedbaranimation.o ../build/release/lib/horizontalpercentbaranimation.o ../build/release/lib/areachartitem.o ../build/release/lib/qareaseries.o ../build/release/lib/chartaxis.o ../build/release/lib/qabstractaxis.o ../build/release/lib/chartvalueaxisx.o ../build/release/lib/chartvalueaxisy.o ../build/release/lib/qvalueaxis.o ../build/release/lib/chartbarcategoryaxisx.o ../build/release/lib/chartbarcategoryaxisy.o ../build/release/lib/qbarcategoryaxis.o ../build/release/lib/chartcategoryaxisx.o ../build/release/lib/chartcategoryaxisy.o ../build/release/lib/qcategoryaxis.o ../build/release/lib/chartdatetimeaxisx.o ../build/release/lib/chartdatetimeaxisy.o ../build/release/lib/qdatetimeaxis.o ../build/release/lib/bar.o ../build/release/lib/abstractbarchartitem.o ../build/release/lib/qabstractbarseries.o ../build/release/lib/qbarset.o ../build/release/lib/qbarmodelmapper.o ../build/release/lib/qvbarmodelmapper.o ../build/release/lib/qhbarmodelmapper.o ../build/release/lib/qbarseries.o ../build/release/lib/barchartitem.o ../build/release/lib/qstackedbarseries.o ../build/release/lib/stackedbarchartitem.o ../build/release/lib/qpercentbarseries.o ../build/release/lib/percentbarchartitem.o ../build/release/lib/qhorizontalbarseries.o ../build/release/lib/horizontalbarchartitem.o ../build/release/lib/qhorizontalstackedbarseries.o ../build/release/lib/horizontalstackedbarchartitem.o ../build/release/lib/qhorizontalpercentbarseries.o ../build/release/lib/horizontalpercentbarchartitem.o ../build/release/lib/qlegend.o ../build/release/lib/legendmarker.o ../build/release/lib/legendlayout.o ../build/release/lib/linechartitem.o ../build/release/lib/qlineseries.o ../build/release/lib/qpieseries.o ../build/release/lib/piesliceitem.o ../build/release/lib/piechartitem.o ../build/release/lib/qpieslice.o ../build/release/lib/qpiemodelmapper.o ../build/release/lib/qvpiemodelmapper.o ../build/release/lib/qhpiemodelmapper.o ../build/release/lib/qscatterseries.o ../build/release/lib/scatterchartitem.o ../build/release/lib/qsplineseries.o ../build/release/lib/splinechartitem.o ../build/release/lib/xychart.o ../build/release/lib/qxyseries.o ../build/release/lib/qxymodelmapper.o ../build/release/lib/qvxymodelmapper.o ../build/release/lib/qhxymodelmapper.o -L/usr/local/Trolltech/Qt-4.8.2/lib -L/tmp/qcharts/lib/release -Wl,-rpath,/tmp/qcharts/lib/release -lQtGui -L/usr/local/Trolltech/Qt-4.8.2/lib -L/usr/X11R6/lib64 -lQtCore -lpthread 

,並用於建立與丟失的功能對象的命令是這樣的:

g++ -c -m64 -pipe -fvisibility=hidden -fvisibility-inlines-hidden -O0 -Wall -W -D_REENTRANT -fPIC -DQTCOMMERCIALCHART_LIBRARY -DQT_NO_DEBUG -DQT_GUI_LIB -DQT_CORE_LIB -DQT_SHARED -I/usr/local/Trolltech/Qt-4.8.2/mkspecs/linux-g++-64 -I. -I/usr/local/Trolltech/Qt-4.8.2/include/QtCore -I/usr/local/Trolltech/Qt-4.8.2/include/QtGui -I/usr/local/Trolltech/Qt-4.8.2/include -I../include -I../include -I. -Ianimations -Iareachart -Iaxis -Iaxis/valueaxis -Iaxis/barcategoryaxis -Iaxis/categoryaxis -Iaxis/datetimeaxis -Ibarchart -Ibarchart/vertical/bar -Ibarchart/vertical/stacked -Ibarchart/vertical/percent -Ibarchart/horizontal/bar -Ibarchart/horizontal/stacked -Ibarchart/horizontal/percent -Ilegend -Ilinechart -Ipiechart -Iscatterchart -Isplinechart -Ithemes -Ixychart -I../build/release/lib -o ../build/release/lib/versiontracker.o versiontracker.cpp 
+1

讓我們看看你的'g ++'命令和你得到的錯誤。 – Foggzie

+0

http://stackoverflow.com/q/3588476/1741542是一個關於使用gcc構建共享庫的綜合性問題。 –

+0

@Olaf:那不是「全面的」 - 這是一個破碎的混亂! –

回答

4

From man gcc

-fvisibility =默認|內部|隱藏|保護
...
的extern聲明不受-fvisibility,所以很多代碼可以用-fvisibility重新編譯=隱藏沒有修改。然而,這意味着調用的extern功能有沒有明確的能見度將使用PLT,所以它是更有效的使用__attribute((能見度))和/或的#pragma GCC可見性告訴編譯器,extern聲明應視爲隱藏。

我沒有這個使用自己,但是從GCC Wiki - Visibility和一個小例子,我要說的是,加入

__attribute__ ((visibility ("default"))) 

extern "C"功能,應該讓他們在動態函數列表,例如可見nm -D libQtCommercialChart.so.1.0.0

如果您已經定義了很多功能,與

extern "C" { 
#pragma GCC visibility push(default) 
... 
#pragma GCC visibility pop 
}; 

支撐他們可能會更方便。

如果您不關心符號可見性,當然也可以從命令行中刪除-fvisibility=hidden