luxonis / XLink

A cross-platform library for communicating with devices over various physical links.
Apache License 2.0
11 stars 17 forks source link

Enable support for BUILD_SHARED_LIBS on Windows #81

Closed traversaro closed 3 weeks ago

traversaro commented 4 weeks ago

The variable CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS used to support BUILD_SHARED_LIBS works fine for all symbols, except static/global variable (see https://cmake.org/cmake/help/latest/prop_tgt/WINDOWS_EXPORT_ALL_SYMBOLS.html and https://www.kitware.com/create-dlls-on-windows-without-declspec-using-new-cmake-export-all-feature/).

For this reason, the global symbols need to be explicitly exported via __declspec(dllexport) and __declspec(dllimport), that are added via a XLinkExport.h header generated by the generate_export_header CMake function.

Without this fix, linking an executable against the XLink shared library on Windows fails with error:

FAILED: examples/boot_firmware.exe
C:\Windows\system32\cmd.exe /C "cd . && D:\miniforge3\envs\idjlluxonisenv\Library\bin\cmake.exe -E vs_link_exe --intdir=examples\CMakeFiles\boot_firmware.dir --rc=C:\PROGRA~2\WI3CF2~1\10\bin\100226~1.0\x64\rc.exe --mt=C:\PROGRA~2\WI3CF2~1\10\bin\100226~1.0\x64\mt.exe --manifests  -- C:\PROGRA~2\MICROS~4\2019\BUILDT~1\VC\Tools\MSVC\1429~1.301\bin\Hostx64\x64\link.exe /nologo examples\CMakeFiles\boot_firmware.dir\boot_firmware.cpp.obj  /out:examples\boot_firmware.exe /implib:examples\boot_firmware.lib /pdb:examples\boot_firmware.pdb /version:0.0 /machine:x64 /INCREMENTAL:NO /subsystem:console  XLink.lib  D:\miniforge3\envs\idjlluxonisenv\Library\lib\libusb-1.0.lib  kernel32.lib user32.lib gdi32.lib winspool.lib shell32.lib ole32.lib oleaut32.lib uuid.lib comdlg32.lib advapi32.lib && C:\Windows\system32\cmd.exe /C "cd /D D:\src\depthaicorestint1\XLink\build\examples && D:\miniforge3\envs\idjlluxonisenv\Library\bin\cmake.exe -E copy_if_different D:/src/depthaicorestint1/XLink/build/XLink.dll D:/src/depthaicorestint1/XLink/build/examples""
LINK: command "C:\PROGRA~2\MICROS~4\2019\BUILDT~1\VC\Tools\MSVC\1429~1.301\bin\Hostx64\x64\link.exe /nologo examples\CMakeFiles\boot_firmware.dir\boot_firmware.cpp.obj /out:examples\boot_firmware.exe /implib:examples\boot_firmware.lib /pdb:examples\boot_firmware.pdb /version:0.0 /machine:x64 /INCREMENTAL:NO /subsystem:console XLink.lib D:\miniforge3\envs\idjlluxonisenv\Library\lib\libusb-1.0.lib kernel32.lib user32.lib gdi32.lib winspool.lib shell32.lib ole32.lib oleaut32.lib uuid.lib comdlg32.lib advapi32.lib /MANIFEST:EMBED,ID=1" failed (exit code 1120) with the following output:
boot_firmware.cpp.obj : error LNK2019: unresolved external symbol mvLogLevel_default referenced in function main
examples\boot_firmware.exe : fatal error LNK1120: 1 unresolved externals
[35/35] Linking CXX executable tests\multithreading_search_test.exe
ninja: build stopped: subcommand failed.