bkaradzic / bgfx.cmake

CMake build scripts for bgfx. Released under public domain.
Creative Commons Zero v1.0 Universal
210 stars 111 forks source link

Building with BGFX_LIBRARY_TYPE=SHARED fails on Windows #226

Closed pr0g closed 6 months ago

pr0g commented 6 months ago

Issue

When using CMake to compile bgfx on Windows with -DBGFX_LIBRARY_TYPE=SHARED, linking fails on example-common.lib and texturev and geometryv that use it. Things work correctly on macOS and Linux.

A lot of errors are reported so it's quite hard to make sense of what's causing it precisely, here's a snippet:

[1/2] Linking CXX executable cmake\bgfx\Debug\geometryv.exe
FAILED: cmake/bgfx/Debug/geometryv.exe
C:\Windows\system32\cmd.exe /C "cd . && "C:\Program Files\CMake\bin\cmake.exe" -E vs_link_exe --intdir=cmake\bgfx\CMakeFiles\geometryv.dir\Debug --rc=C:\PROGRA~2\WI3CF2~1\10\bin\100226~1.0\arm64\rc.exe --mt=C:\PROGRA~2\WI3CF2~1\10\bin\100226~1.0\arm64\mt.exe --manifests  -- C:\PROGRA~1\MICROS~1\2022\COMMUN~1\VC\Tools\MSVC\1439~1.335\bin\HOSTAR~1\arm64\link.exe /nologo cmake\bgfx\CMakeFiles\geometryv.dir\Debug\__\__\bgfx\tools\geometryv\geometryv.cpp.obj  /out:cmake\bgfx\Debug\geometryv.exe /implib:cmake\bgfx\Debug\geometryv.lib /pdb:cmake\bgfx\Debug\geometryv.pdb /version:0.0 /machine:ARM64 /debug /INCREMENTAL /subsystem:console  cmake\bgfx\Debug\example-common.lib  cmake\bgfx\Debug\bgfx.lib  cmake\bimg\Debug\bimg.lib  cmake\bimg\Debug\bimg_decode.lib  cmake\bx\Debug\bx.lib  psapi.lib  kernel32.lib user32.lib gdi32.lib winspool.lib shell32.lib ole32.lib oleaut32.lib uuid.lib comdlg32.lib advapi32.lib && cd ."
LINK Pass 1: command "C:\PROGRA~1\MICROS~1\2022\COMMUN~1\VC\Tools\MSVC\1439~1.335\bin\HOSTAR~1\arm64\link.exe /nologo cmake\bgfx\CMakeFiles\geometryv.dir\Debug\__\__\bgfx\tools\geometryv\geometryv.cpp.obj /out:cmake\bgfx\Debug\geometryv.exe /implib:cmake\bgfx\Debug\geometryv.lib /pdb:cmake\bgfx\Debug\geometryv.pdb /version:0.0 /machine:ARM64 /debug /INCREMENTAL /subsystem:console cmake\bgfx\Debug\example-common.lib cmake\bgfx\Debug\bgfx.lib cmake\bimg\Debug\bimg.lib cmake\bimg\Debug\bimg_decode.lib cmake\bx\Debug\bx.lib psapi.lib kernel32.lib user32.lib gdi32.lib winspool.lib shell32.lib ole32.lib oleaut32.lib uuid.lib comdlg32.lib advapi32.lib /MANIFEST /MANIFESTFILE:cmake\bgfx\CMakeFiles\geometryv.dir\Debug/intermediate.manifest cmake\bgfx\CMakeFiles\geometryv.dir\Debug/manifest.res" failed (exit code 1120) with the following output:
geometryv.cpp.obj : error LNK2019: unresolved external symbol "public: __cdecl bgfx::Init::Init(void)" (??0Init@bgfx@@QEAA@XZ) referenced in function _main_
geometryv.cpp.obj : error LNK2019: unresolved external symbol "bool __cdecl bgfx::init(struct bgfx::Init const &)" (?init@bgfx@@YA_NAEBUInit@1@@Z) referenced in function _main_
geometryv.cpp.obj : error LNK2019: unresolved external symbol "void __cdecl bgfx::shutdown(void)" (?shutdown@bgfx@@YAXXZ) referenced in function _main_
geometryv.cpp.obj : error LNK2019: unresolved external symbol "unsigned int __cdecl bgfx::frame(bool)" (?frame@bgfx@@YAI_N@Z) referenced in function _main_
example-common.lib(entry.cpp.obj) : error LNK2001: unresolved external symbol "unsigned int __cdecl bgfx::frame(bool)" (?frame@bgfx@@YAI_N@Z)
geometryv.cpp.obj : error LNK2019: unresolved external symbol "struct bgfx::Caps const * __cdecl bgfx::getCaps(void)" (?getCaps@bgfx@@YAPEBUCaps@1@XZ) referenced in function _main_
example-common.lib(imgui.cpp.obj) : error LNK2001: unresolved external symbol "struct bgfx::Caps const * __cdecl bgfx::getCaps(void)" (?getCaps@bgfx@@YAPEBUCaps@1@XZ)
geometryv.cpp.obj : error LNK2019: unresolved external symbol "void __cdecl bgfx::dbgTextClear(unsigned char,bool)" (?dbgTextClear@bgfx@@YAXE_N@Z) referenced in function _main_
geometryv.cpp.obj : error LNK2019: unresolved external symbol "struct bgfx::ProgramHandle __cdecl bgfx::createProgram(struct bgfx::ShaderHandle,struct bgfx::ShaderHandle,bool)" (?createProgram@bgfx@@YA?AUProgramHandle@1@UShaderHandle@1@0_N@Z) referenced in function _main_
example-common.lib(imgui.cpp.obj) : error LNK2001: unresolved external symbol "struct bgfx::ProgramHandle __cdecl bgfx::createProgram(struct bgfx::ShaderHandle,struct bgfx::ShaderHandle,bool)" (?createProgram@bgfx@@YA?AUProgramHandle@1@UShaderHandle@1@0_N@Z)
example-common.lib(debugdraw.cpp.obj) : error LNK2001: unresolved external symbol "struct bgfx::ProgramHandle __cdecl bgfx::createProgram(struct bgfx::ShaderHandle,struct bgfx::ShaderHandle,bool)" (?createProgram@bgfx@@YA?AUProgramHandle@1@UShaderHandle@1@0_N@Z)
example-common.lib(bgfx_utils.cpp.obj) : error LNK2001: unresolved external symbol "struct bgfx::ProgramHandle __cdecl bgfx::createProgram(struct bgfx::ShaderHandle,struct bgfx::ShaderHandle,bool)" (?createProgram@bgfx@@YA?AUProgramHandle@1@UShaderHandle@1@0_N@Z)
geometryv.cpp.obj : error LNK2019: unresolved external symbol "void __cdecl bgfx::destroy(struct bgfx::ProgramHandle)" (?destroy@bgfx@@YAXUProgramHandle@1@@Z) referenced in function _main_
example-common.lib(imgui.cpp.obj) : error LNK2001: unresolved external symbol "void __cdecl bgfx::destroy(struct bgfx::ProgramHandle)" (?destroy@bgfx@@YAXUProgramHandle@1@@Z)
example-common.lib(debugdraw.cpp.obj) : error LNK2001: unresolved external symbol "void __cdecl bgfx::destroy(struct bgfx::ProgramHandle)" (?destroy@bgfx@@YAXUProgramHandle@1@@Z)
geometryv.cpp.obj : error LNK2019: unresolved external symbol "void __cdecl bgfx::setViewRect(unsigned short,unsigned short,unsigned short,unsigned short,unsigned short)" (?setViewRect@bgfx@@YAXGGGGG@Z) referenced in function _main_
example-common.lib(imgui.cpp.obj) : error LNK2001: unresolved external symbol "void __cdecl bgfx::setViewRect(unsigned short,unsigned short,unsigned short,unsigned short,unsigned short)" (?setViewRect@bgfx@@YAXGGGGG@Z)
geometryv.cpp.obj : error LNK2019: unresolved external symbol "void __cdecl bgfx::setViewClear(unsigned short,unsigned short,unsigned int,float,unsigned char)" (?setViewClear@bgfx@@YAXGGIME@Z) referenced in function _main_
geometryv.cpp.obj : error LNK2019: unresolved external symbol "void __cdecl bgfx::setViewTransform(unsigned short,void const *,void const *)" (?setViewTransform@bgfx@@YAXGPEBX0@Z) referenced in function _main_
example-common.lib(imgui.cpp.obj) : error LNK2001: unresolved external symbol "void __cdecl bgfx::setViewTransform(unsigned short,void const *,void const *)" (?setViewTransform@bgfx@@YAXGPEBX0@Z)
geometryv.cpp.obj : error LNK2019: unresolved external symbol "unsigned int __cdecl bgfx::setTransform(void const *,unsigned short)" (?setTransform@bgfx@@YAIPEBXG@Z) referenced in function _main_
example-common.lib(bgfx_utils.cpp.obj) : error LNK2001: unresolved external symbol "unsigned int __cdecl bgfx::setTransform(void const *,unsigned short)" (?setTransform@bgfx@@YAIPEBXG@Z)
geometryv.cpp.obj : error LNK2019: unresolved external symbol "void __cdecl bgfx::touch(unsigned short)" (?touch@bgfx@@YAXG@Z) referenced in function _main_
geometryv.cpp.obj : error LNK2019: unresolved external symbol "struct bgfx::ShaderHandle __cdecl bgfx::createEmbeddedShader(struct bgfx::EmbeddedShader const *,enum bgfx::RendererType::Enum,char const *)" (?createEmbeddedShader@bgfx@@YA?AUShaderHandle@1@PEBUEmbeddedShader@1@W4Enum@RendererType@1@PEBD@Z) referenced in function _main_
example-common.lib(imgui.cpp.obj) : error LNK2001: unresolved external symbol "struct bgfx::ShaderHandle __cdecl bgfx::createEmbeddedShader(struct bgfx::EmbeddedShader const *,enum bgfx::RendererType::Enum,char const *)" (?createEmbeddedShader@bgfx@@YA?AUShaderHandle@1@PEBUEmbeddedShader@1@W4Enum@RendererType@1@PEBD@Z)
example-common.lib(debugdraw.cpp.obj) : error LNK2001: unresolved external symbol "struct bgfx::ShaderHandle __cdecl bgfx::createEmbeddedShader(struct bgfx::EmbeddedShader const *,enum bgfx::RendererType::Enum,char const *)" (?createEmbeddedShader@bgfx@@YA?AUShaderHandle@1@PEBUEmbeddedShader@1@W4Enum@RendererType@1@PEBD@Z)
example-common.lib(entry.cpp.obj) : error LNK2019: unresolved external symbol "void __cdecl bgfx::reset(unsigned int,unsigned int,unsigned int,enum bgfx::TextureFormat::Enum)" (?reset@bgfx@@YAXIIIW4Enum@TextureFormat@1@@Z) referenced in function "bool __cdecl entry::processEvents(unsigned int &,unsigned int &,unsigned int &,unsigned int &,struct entry::MouseState *)" (?processEvents@entry@@YA_NAEAI000PEAUMouseState@1@@Z)
example-common.lib(entry.cpp.obj) : error LNK2019: unresolved external symbol "void __cdecl bgfx::setDebug(unsigned int)" (?setDebug@bgfx@@YAXI@Z) referenced in function "int __cdecl entry::cmdGraphics(struct CmdContext *,void *,int,char const * const *)" (?cmdGraphics@entry@@YAHPEAUCmdContext@@PEAXHPEBQEBD@Z)
example-common.lib(entry.cpp.obj) : error LNK2019: unresolved external symbol "void __cdecl bgfx::requestScreenShot(struct bgfx::FrameBufferHandle,char const *)" (?requestScreenShot@bgfx@@YAXUFrameBufferHandle@1@PEBD@Z) referenced in function "int __cdecl entry::cmdGraphics(struct CmdContext *,void *,int,char const * const *)" (?cmdGraphics@entry@@YAHPEAUCmdContext@@PEAXHPEBQEBD@Z)
example-common.lib(entry_windows.cpp.obj) : error LNK2019: unresolved external symbol "enum bgfx::RenderFrame::Enum __cdecl bgfx::renderFrame(int)" (?renderFrame@bgfx@@YA?AW4Enum@RenderFrame@1@H@Z) referenced in function "public: int __cdecl entry::Context::run(int,char const * const *)" (?run@Context@entry@@QEAAHHPEBQEBD@Z)
example-common.lib(imgui.cpp.obj) : error LNK2019: unresolved external symbol "public: void __cdecl bgfx::Encoder::setState(unsigned __int64,unsigned int)" (?setState@Encoder@bgfx@@QEAAX_KI@Z) referenced in function "public: void __cdecl OcornutImguiContext::render(struct ImDrawData *)" (?render@OcornutImguiContext@@QEAAXPEAUImDrawData@@@Z)
example-common.lib(debugdraw.cpp.obj) : error LNK2001: unresolved external symbol "public: void __cdecl bgfx::Encoder::setState(unsigned __int64,unsigned int)" (?setState@Encoder@bgfx@@QEAAX_KI@Z)
...

Seems related to function(add_example ARG_NAME) most likely (in examples.cmake)

Repro

Clone the repo and its submodules, then run the below commands and see the error output

cmake -B build -G "Ninja Multi-Config" -DBGFX_LIBRARY_TYPE=SHARED
cmake --build build

I might be doing something stupid so please let me know if there's some other flag I need to pass or setting to change to get things to work.

Thanks!

pr0g commented 6 months ago

It may be that this just isn't supported (see https://github.com/bkaradzic/bgfx/issues/670), but I'm just wondering what would be the workaround for compiling with BGFX_LIBRARY_TYPE=SHARED set?

Do you need to compile once with the library as STATIC to build the tools, and then again as SHARED for use by the C API? If there's any kind of workaround or optimum way to approach this I'd be really grateful to know. Thanks!

pr0g commented 6 months ago

Actually maybe it's fine, I just need to do this...

cmake -B build -G "Ninja Multi-Config" -DBGFX_LIBRARY_TYPE=SHARED -DBGFX_BUILD_TOOLS=OFF -DBGFX_BUILD_EXAMPLES=OFF

To build the library as shared, and then build the tools separately... I'll do that for now, please ignore this! 🙂