'How to properly link an external library with msvc compiler batch file

I have a project which is using a simple batch file to compile everything I need to work with. But right now I need to use imgui in my project, and I got a problems with linking it correctly to my project. I am trying to use Dear ImGui right now.

In my project, in the moment when I am trying to build it with "linked" imgui library, it complains with error: LNK2019: unresolved external symbol with every imgui function

Basically, I have this project folder set up:

/project_name
    /imgui
    file1.cpp
    file1.h
    ....

And I have this batch file to compile the whole project:

@echo off
if not defined DevEnvDir (
    call vcvarsall x64
)
REM call vcvarsall x64

set LIB_VCPKG="F:\Env\vcpkg\installed\x64-windows\lib"
set INC_VCPKG="F:\Env\vcpkg\installed\x64-windows\include"
set INC_IMGUI="..\code\imgui"

set CommonCompileFlags=-MT -nologo -fp:fast -EHa -Od -WX- -W4 -Oi -GR- -Gm- -GS -wd4100 -wd4201 -wd4505 -FC -Z7 /I%INC_VCPKG% /I%INC_IMGUI%
set CommonLinkFlags=-opt:ref -incremental:no /NODEFAULTLIB:msvcrt /SUBSYSTEM:CONSOLE /LIBPATH:%LIB_VCPKG%  

if not exist ..\build mkdir ..\build
pushd ..\build

REM cl ..\code\imgui\imgui.cpp /c /EHsc
REM lib imgui.obj
cl %CommonCompileFlags% SDL2main.lib SDL2.lib ..\code\display.cpp -LD /link -opt:ref -incremental:no /LIBPATH:%LIB_VCPKG%
cl %CommonCompileFlags% ..\code\main.cpp SDL2main.lib SDL2.lib display.obj /link %CommonLinkFlags%
popd

I already tried to compile from imgui a static library, but that did not helped. And I tried to use /I flag - that didn't helped neither.

For example, I am using header only Sean Berrets library in my project and it is working pretty fine here. But I have link issue only with the dear ImGui library. I don't know why.

Is there a possibilities that I just don't understand something and just don't do the link phase correctly? Thanks.



Solution 1:[1]

This example helped me to workout my issue: https://github.com/ocornut/imgui/tree/master/examples/example_win32_directx12

So, basically, I didn't linked to any of the Dear ImGui files. Right now my batch file is looks like this:

@echo off
if not defined DevEnvDir (
    call vcvarsall x64
)
REM call vcvarsall x64

set LIB_VCPKG="F:\Env\vcpkg\installed\x64-windows\lib"
set INC_VCPKG="F:\Env\vcpkg\installed\x64-windows\include"
set INC_IMGUI="..\code\imgui"

set CommonCompileFlags=-MT -nologo -fp:fast -EHa -Od -WX- -W4 -Oi -GR- -Gm- -GS -wd4100 -wd4201 -wd4505 -FC -Z7 /I%INC_VCPKG% /I%INV_IMGUI%
set CommonLinkFlags=-opt:ref -incremental:no /NODEFAULTLIB:msvcrt /SUBSYSTEM:CONSOLE /LIBPATH:%LIB_VCPKG%  

if not exist ..\build mkdir ..\build
pushd ..\build

cl %CommonCompileFlags% SDL2main.lib SDL2.lib imgui.obj imgui_demo.obj imgui_draw.obj imgui_impl_sdl.obj imgui_impl_sdlrenderer.obj imgui_tables.obj imgui_widgets.obj ..\code\display.cpp -LD /link -opt:ref -incremental:no /LIBPATH:%LIB_VCPKG%
cl %CommonCompileFlags% ..\code\main.cpp ..\code\imgui\imgui*.cpp ..\code\imgui\imgui_impl_sdl.cpp SDL2main.lib SDL2.lib display.obj /link %CommonLinkFlags%
popd

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Zhukov Artem