Static linking of occt libraries into executable.

Forums: 

Hi all.
I have built opencascade statically, and I'm trying to link opencascade static libraries into executable using cmake.
I develop library (let's name it mylib.a) that use opencascade functionality. And I want link mylib.a and opencascade libraries into executable (test app for example). When I try build my project I have many errors from opencascade libraries (compile output attached to this topic).
Here is a CMakeLists.txt from my executable:

project(test)

aux_source_directory(. SRC_LIST)
link_directories(/usr/lib /usr/lib/x86_64-linux-gnu /usr/local/lib)
include_directories(/usr/include /usr/local/include /usr/local/include/opencascade)

set(CMAKE_FIND_LIBRARY_SUFFIXES ".a")

if(POLICY CMP0037)
cmake_policy(PUSH)
cmake_policy(SET CMP0037 OLD)
endif()

if(POLICY CMP0037)
cmake_policy(POP)
endif()

add_executable(${PROJECT_NAME} ${SRC_LIST})
target_link_libraries(${PROJECT_NAME} mylib.a TKernel.a TKMath.a TKG2d.a TKG3d.a TKGeomBase.a
TKBRep.a TKGeomAlgo.a TKTopAlgo.a TKPrim.a TKShHealing.a TKBO.a TKBool.a TKHLR.a
TKFillet.a TKFeat.a TKOffset.a TKMesh.a TKXMesh.a TKService.a TKV3d.a TKOpenGl.a
TKMeshVS.a TKCDF.a TKLCAF.a TKCAF.a TKBinL.a TKXmlL.a TKBin.a TKXml.a TKStdL.a
TKStd.a TKXSBase.a TKSTEPBase.a TKSTEPAttr.a TKSTEP209.a TKSTEP.a TKIGES.a TKXCAF.a
TKXDEIGES.a TKXmlXCAF.a TKBinXCAF.a TKXDESTEP.a TKDraw.a TKTopTest.a TKViewerTest.a
TKXSDRAW.a TKDCAF.a TKXDEDRAW.a TKQADraw.a TKTObjDRAW.a FWOSPlugin.a)

I don't have any errors when I try link my static library (mylib.a) with shared opencascade libraries and my test application successfully link mylib.a and use dynamic opencascade libraries. But I want use my test application on machines without installed opencascade.
I think the problem is in the opencascade libraries dependencies, I guess I didn't specify all needed libraries which opencascade libraries use, and that's why I have so many errors. Could some body specify opencascade dependencies? Also I think it's a good idea to add this info into documentation. I think I didn't first who have the same problem with static linkage of opencascade libraries.

Regards, Volodymyr.

EDIT:
https://anotepad.com/notes/i57rfb -- free notes with compile output

Kirill Gavrilov's picture

Hello Volodymyr,

this forum is intended for questions related to development of OCCT itself, not for application-specific problems. The users forum is better place for your question (https://www.opencascade.com/forums).

target_link_libraries(${PROJECT_NAME} mylib.a TKernel.a TKMath.a TKG2d.a TKG3d.a TKGeomBase.a

The dependency libraries are placed in wrong order - TKernel.a should be a last library in the list of OCCT libraries (since all other libraries depend on it) and other libraries should be reordered as well. This issue is unrelated to OCCT itself - you should look for information about static linking.

You can refer to the graphs in the documentation:
https://dev.opencascade.org/doc/refman/html/index.html

Volodymyr vvv's picture

Hi.
Thanks for your reply. You right, when I changed order of libraries i had less errors then before.
But I think this is not inconveniently to make list of libraries in right order by diagrams (graphs).

UPD:

target_link_libraries(${PROJECT_NAME} mylib.a FWOSPlugin.a TKTObjDRAW.a TKQADraw.a TKXDEDRAW.a TKDCAF.a TKDCAF.a TKXSDRAW.a
TKViewerTest.a TKTopTest.a TKDraw.a TKXDESTEP.a TKBinXCAF.a TKXmlXCAF.a TKXDEIGES.a TKXCAF.a TKIGES.a TKSTEP.a TKSTEP209.a TKSTEPAttr.a
TKSTEPBase.a TKXSBase.a TKStd.a TKStdL.a TKXml.a TKBin.a TKXmlL.a TKBinL.a TKCAF.a TKXCAF.a TKLCAF.a TKCDF.a TKMeshVS.a TKOpenGl.a TKV3d.a TKService.a
TKXMesh.a TKMesh.a TKOffset.a TKFeat.a TKFillet.a TKHLR.a TKBool.a TKBO.a TKShHealing.a TKPrim.a TKTopAlgo.a TKGeomAlgo.a TKBRep.a
TKGeomBase.a TKG3d.a TKG2d.a TKMath.a TKernel.a)

the error list is:
/usr/local/lib/libTKXCAF.a(XCAFApp_Application.cxx.o): In function `XCAFApp_Application::XCAFApp_Application()':
XCAFApp_Application.cxx:(.text+0x1d3): undefined reference to `TPrsStd_DriverTable::Get()'
XCAFApp_Application.cxx:(.text+0x1e8): undefined reference to `TPrsStd_Driver::TPrsStd_Driver()'
XCAFApp_Application.cxx:(.text+0x219): undefined reference to `TPrsStd_DriverTable::AddDriver(Standard_GUID const&, opencascade::handle const&)'
/usr/local/lib/libTKXCAF.a(XCAFPrs_Driver.cxx.o): In function `XCAFPrs_Driver::get_type_descriptor()':
XCAFPrs_Driver.cxx:(.text+0x3c4): undefined reference to `typeinfo for TPrsStd_Driver'
/usr/local/lib/libTKXCAF.a(XCAFPrs_Driver.cxx.o):(.data.rel.ro._ZTI14XCAFPrs_Driver[_ZTI14XCAFPrs_Driver]+0x10): undefined reference to `typeinfo for TPrsStd_Driver'
/usr/local/lib/libTKernel.a(OSD_SharedLibrary.cxx.o): In function `OSD_SharedLibrary::DlOpen(OSD_LoadMode)':
OSD_SharedLibrary.cxx:(.text+0xe8): undefined reference to `dlopen'
OSD_SharedLibrary.cxx:(.text+0x10a): undefined reference to `dlopen'
/usr/local/lib/libTKernel.a(OSD_SharedLibrary.cxx.o): In function `OSD_SharedLibrary::DlSymb(char const*) const':
OSD_SharedLibrary.cxx:(.text+0x124): undefined reference to `dlsym'
/usr/local/lib/libTKernel.a(OSD_SharedLibrary.cxx.o): In function `OSD_SharedLibrary::DlClose() const':
OSD_SharedLibrary.cxx:(.text+0x134): undefined reference to `dlclose'
/usr/local/lib/libTKernel.a(OSD_SharedLibrary.cxx.o): In function `OSD_SharedLibrary::DlError() const':
OSD_SharedLibrary.cxx:(.text+0x141): undefined reference to `dlerror'
/usr/local/lib/libTKernel.a(OSD_Thread.cxx.o): In function `OSD_Thread::Run(void*, int)':
OSD_Thread.cxx:(.text+0xf5): undefined reference to `pthread_create'
/usr/local/lib/libTKernel.a(OSD_Thread.cxx.o): In function `OSD_Thread::Detach()':
OSD_Thread.cxx:(.text+0x13e): undefined reference to `pthread_detach'
/usr/local/lib/libTKernel.a(OSD_Thread.cxx.o): In function `OSD_Thread::Wait(void*&) const':
OSD_Thread.cxx:(.text+0x17d): undefined reference to `pthread_join'
/usr/local/lib/libTKernel.a(OSD_Thread.cxx.o): In function `OSD_Thread::Wait(int, void*&) const':
OSD_Thread.cxx:(.text+0x22c): undefined reference to `pthread_timedjoin_np'
/usr/local/lib/libTKernel.a(Standard_Mutex.cxx.o): In function `Standard_Mutex::Standard_Mutex()':
Standard_Mutex.cxx:(.text+0xa1): undefined reference to `pthread_mutexattr_init'
Standard_Mutex.cxx:(.text+0xae): undefined reference to `pthread_mutexattr_settype'
Standard_Mutex.cxx:(.text+0xc2): undefined reference to `pthread_mutexattr_destroy'
/usr/local/lib/libTKernel.a(Standard_Mutex.cxx.o): In function `Standard_Mutex::TryLock()':
Standard_Mutex.cxx:(.text+0x109): undefined reference to `pthread_mutex_trylock'
collect2: error: ld returned 1 exit status

Kirill Gavrilov's picture

OSD_SharedLibrary.cxx:(.text+0xe8): undefined reference to `dlopen'
OSD_Thread.cxx:(.text+0x17d): undefined reference to `pthread_join'

While using statically built OCCT, your application should be linked not only with OCCT itself, but also with all its dependencies (including standard threading / library loading libraries). You can find a hundreds of posts across internet how to fix these unresolved symbols.