openpnp / openpnp-capture

A cross platform video capture library with a focus on machine vision.
MIT License
129 stars 56 forks source link

Static linking on Windows #75

Closed raphaelmenges closed 4 months ago

raphaelmenges commented 4 months ago

Hello 👋,

thank you for this library! According to my research, this is the only library that can reliable list connected cameras on Windows and macOS and provides an uniform frame format to be processed across platforms. Great!

I want to use openpnp-capture within another library. For this, I prefer to link openpnp-capture statically. I have forked the project and I have changed the CMakeLists.txt accordingly. It works fine for me under macOS. However, using compiling the library and accompanying test using Visual Studio 2022, I am confronted with the following linker errors:

Severity    Code    Description Project File    Line    Suppression State   Details
Error   LNK2019 unresolved external symbol __imp_Cap_createContext referenced in function main  openpnp-capture-test    C:\Users\Raphael\Git\static-openpnp-capture\build\win\tests\main.obj    1       
Warning C4267   'argument': conversion from 'size_t' to 'uint32_t', possible loss of data   openpnp-capture-test    C:\Users\Raphael\Git\static-openpnp-capture\win\tests\main.cpp  438     
Error   LNK2019 unresolved external symbol __imp_Cap_releaseContext referenced in function main openpnp-capture-test    C:\Users\Raphael\Git\static-openpnp-capture\build\win\tests\main.obj    1       
Error   LNK2019 unresolved external symbol __imp_Cap_getDeviceCount referenced in function main openpnp-capture-test    C:\Users\Raphael\Git\static-openpnp-capture\build\win\tests\main.obj    1       
Error   LNK2019 unresolved external symbol __imp_Cap_getDeviceName referenced in function main  openpnp-capture-test    C:\Users\Raphael\Git\static-openpnp-capture\build\win\tests\main.obj    1       
Error   LNK2019 unresolved external symbol __imp_Cap_getDeviceUniqueID referenced in function main  openpnp-capture-test    C:\Users\Raphael\Git\static-openpnp-capture\build\win\tests\main.obj    1       
Error   LNK2019 unresolved external symbol __imp_Cap_getNumFormats referenced in function main  openpnp-capture-test    C:\Users\Raphael\Git\static-openpnp-capture\build\win\tests\main.obj    1       
Error   LNK2019 unresolved external symbol __imp_Cap_getFormatInfo referenced in function main  openpnp-capture-test    C:\Users\Raphael\Git\static-openpnp-capture\build\win\tests\main.obj    1       
Error   LNK2019 unresolved external symbol __imp_Cap_openStream referenced in function main openpnp-capture-test    C:\Users\Raphael\Git\static-openpnp-capture\build\win\tests\main.obj    1       
Error   LNK2019 unresolved external symbol __imp_Cap_closeStream referenced in function main    openpnp-capture-test    C:\Users\Raphael\Git\static-openpnp-capture\build\win\tests\main.obj    1       
Error   LNK2019 unresolved external symbol __imp_Cap_isOpenStream referenced in function main   openpnp-capture-test    C:\Users\Raphael\Git\static-openpnp-capture\build\win\tests\main.obj    1       
Error   LNK2019 unresolved external symbol __imp_Cap_captureFrame referenced in function main   openpnp-capture-test    C:\Users\Raphael\Git\static-openpnp-capture\build\win\tests\main.obj    1       
Error   LNK2019 unresolved external symbol __imp_Cap_getStreamFrameCount referenced in function "void __cdecl estimateFrameRate(void *,int)" (?estimateFrameRate@@YAXPEAXH@Z)   openpnp-capture-test    C:\Users\Raphael\Git\static-openpnp-capture\build\win\tests\main.obj    1       
Error   LNK2019 unresolved external symbol __imp_Cap_getPropertyLimits referenced in function main  openpnp-capture-test    C:\Users\Raphael\Git\static-openpnp-capture\build\win\tests\main.obj    1       
Error   LNK2019 unresolved external symbol __imp_Cap_setProperty referenced in function main    openpnp-capture-test    C:\Users\Raphael\Git\static-openpnp-capture\build\win\tests\main.obj    1       
Error   LNK2019 unresolved external symbol __imp_Cap_setAutoProperty referenced in function main    openpnp-capture-test    C:\Users\Raphael\Git\static-openpnp-capture\build\win\tests\main.obj    1       
Error   LNK2019 unresolved external symbol __imp_Cap_getProperty referenced in function "void __cdecl showProperty(void *,int,unsigned int)" (?showProperty@@YAXPEAXHI@Z)   openpnp-capture-test    C:\Users\Raphael\Git\static-openpnp-capture\build\win\tests\main.obj    1       
Error   LNK2019 unresolved external symbol __imp_Cap_getAutoProperty referenced in function "void __cdecl showAutoProperty(void *,int,unsigned int)" (?showAutoProperty@@YAXPEAXHI@Z)   openpnp-capture-test    C:\Users\Raphael\Git\static-openpnp-capture\build\win\tests\main.obj    1       
Error   LNK2019 unresolved external symbol __imp_Cap_setLogLevel referenced in function main    openpnp-capture-test    C:\Users\Raphael\Git\static-openpnp-capture\build\win\tests\main.obj    1       
Error   LNK2019 unresolved external symbol __imp_Cap_installCustomLogFunction referenced in function main   openpnp-capture-test    C:\Users\Raphael\Git\static-openpnp-capture\build\win\tests\main.obj    1       
Error   LNK2019 unresolved external symbol __imp_Cap_getLibraryVersion referenced in function main  openpnp-capture-test    C:\Users\Raphael\Git\static-openpnp-capture\build\win\tests\main.obj    1       
Error   LNK1120 20 unresolved externals openpnp-capture-test    C:\Users\Raphael\Git\static-openpnp-capture\build\win\tests\Debug\openpnp-capture-test.exe  1       

I had suspected target_link_libraries(openpnp-capture strmiids) (https://github.com/openpnp/openpnp-capture/blob/8badbbae826a578c2d66deecbffb6a2a079f1817/CMakeLists.txt#L89C5-L90C1) as cause of the issue and moved the linking of strmiids into the CMakeLists.txt of the test under win/tests. But the linker error persist. Any idea what I am doing wrongly?

raphaelmenges commented 4 months ago

I can reproduce the problem on GitHub actions: https://github.com/raphaelmenges/static-openpnp-capture/actions/runs/9820442542/job/27115023180

trcwm commented 4 months ago

Sorry, I don't know. I don't develop for Windows anymore. You can check which functions are exported by the static library using Microsoft's dumpbin I think.

raphaelmenges commented 4 months ago

I managed to build a static library under Windows by removing #define DLLPUBLIC: https://github.com/openpnp/openpnp-capture/commit/7459dd8f4319d19194dc4ce593395c18a5a2cf41

I guess one would need a proper refactoring to build both dynamic and static libraries of openpnp-capture and sadly, I have no time for that. However, my fork suits my needs. Thus, I see the scope of this issue to be fulfilled.