Here's a complete example:
Get Dear ImGui from its git repository and change to its directory
QZPB\mjb D:\>git clone https://github.com/ocornut/imgui.git
Cloning into 'imgui'...
remote: Enumerating objects: 42932, done.
remote: Counting objects: 100% (328/328), done.
remote: Compressing objects: 100% (165/165), done.
remote: Total 42932 (delta 243), reused 213 (delta 163), pack-reused 42604 eceiving objects: 100% (42932/42932), 75.47 MiB | 2.99 MiB/s
Receiving objects: 100% (42932/42932), 76.27 MiB | 2.98 MiB/s, done.
Resolving deltas: 100% (32594/32594), done.
QZPB\mjb D:\>cd imgui |
Now list the contents of the directory, keeping an eye out for files named README, INSTALL, BUILDING and the like. We're particularly interested in the README file, which needs to be read carefully and completely.
QZPB\mjb D:\imgui>dir /B
.editorconfig
.gitattributes
.github
.gitignore
backends
docs
examples
imconfig.h
imgui.cpp
imgui.h
imgui_demo.cpp
</snip>
imstb_truetype.h
LICENSE.txt
misc |
There's no README file here, but there is a
docs folder.
Before entering the
docs folder to continue our search for a
README, we can note that there are no subdirectories which conventionally contain source code. Such subdirectories are almost always named something like
src,
source,
code, or
include. Initially it looks like all the code in the project lives directly in this root directory.
QZPB\mjb D:\imgui>cd docs
QZPB\mjb D:\imgui\docs>dir /b
BACKENDS.md
CHANGELOG.txt
EXAMPLES.md
FAQ.md
FONTS.md
README.md
TODO.txt |
In this case the
README file lives in the
docs directory; its name is
README.md. Read it in its entirety, using any markdown viewer of your choice. Any plaintext editor will make do, but it is better to use one with support for markdown files.
QZPB\mjb D:\imgui\docs>emacsclient -c README.md |
README.md says "No specific build process is required. You can add the .cpp files to your existing project." This is about as easy as it gets. It also tells us about the examples, so we can enter the examples directory and list its contents:
QZPB\mjb D:\imgui\docs>cd ..\examples\
QZPB\mjb D:\imgui\examples>dir /b
example_allegro5
</snip>
example_win32_directx9
imgui_examples.sln
libs
README.txt |
There is another README file here, which should be read as well.
QZPB\mjb D:\imgui\examples>type README.txt
</snip>
|
You're trying to build the DX11 example, which seems fine, so we'll enter that directory too:
QZPB\mjb D:\imgui\examples>cd example_win32_directx11\
QZPB\mjb D:\imgui\examples\example_win32_directx11>dir /b
build_win32.bat
example_win32_directx11.vcxproj
example_win32_directx11.vcxproj.filters
main.cpp |
There's no README file, but there is a shell script called
build_win32.bat which presumably builds the example from scratch. The files
example_win32_directx11.vcxproj and
example_win32_directx11.vcxproj.filters have to do with Microsoft Visual Studio; you should be able to ignore these.
Let's read the shell script:
QZPB\mjb D:\imgui\examples\example_win32_directx11>type build_win32.bat
@REM Build for Visual Studio compiler. Run your copy of vcvars32.bat or vcvarsall.bat to setup command-line compiler.
@set OUT_DIR=Debug
@set OUT_EXE=example_win32_directx11
@set INCLUDES=/I..\.. /I..\..\backends /I "%WindowsSdkDir%Include\um" /I "%WindowsSdkDir%Include\shared" /I "%DXSDK_DIR%Include"
@set SOURCES=main.cpp ..\..\backends\imgui_impl_dx11.cpp ..\..\backends\imgui_impl_win32.cpp ..\..\imgui*.cpp
@set LIBS=/LIBPATH:"%DXSDK_DIR%/Lib/x86" d3d11.lib d3dcompiler.lib
mkdir %OUT_DIR%
cl /nologo /Zi /MD %INCLUDES% /D UNICODE /D _UNICODE %SOURCES% /Fe%OUT_DIR%/%OUT_EXE%.exe /Fo%OUT_DIR%/ /link %LIBS% |
You're not using Microsoft's compiler,
cl.exe, so this won't solve your problem, but thankfully this script is simple, much simpler than usual.
The script tells us a couple things: we must link d3d11.lib and d3dcompiler.lib, we must define _UNICODE and UNICODE during the build, and most importantly that all the source code is in the root directory, except for some files in the
backends directory. The READMEs had stuff to say about this too.
From the contents of build_win32.bat, we can come up with an initial guess for a g++ invocation (sorry about the long lines):
QZPB\mjb D:\imgui\examples\example_win32_directx11>cd example_win32_directx11
QZPB\mjb D:\imgui\examples\example_win32_directx11>g++ -fno-strict-aliasing -g -DUNICODE -D_UNICODE -I ../.. -I ../../backends main.cpp ../../backends/imgui_impl_dx11.cpp ../../backends/imgui_impl_win32.cpp ../../imgui*.cpp -ld3d11 -ld3dcompiler -o example_win32_directx11.exe
</snip>
undefined reference to `__imp_GetDeviceCaps'
</snip>
undefined reference to `DwmEnableBlurBehindWindow'
collect2.exe: error: ld returned 1 exit status |
To fix the linker's complaints, read the error messages for the names of the missing symbols. The linker can't find a definition for the symbols because they're provided by libraries we haven't told it about. To fix it, go to MSDN and search - first for
GetDeviceCaps and then for
DwmEnableBlurBehindWindow:
https://docs.microsoft.com/en-us/windows/win32/api/wingdi/nf-wingdi-getdevicecaps
https://docs.microsoft.com/en-us/windows/win32/api/dwmapi/nf-dwmapi-dwmenableblurbehindwindow
On each of those web pages, scroll to the bottom and find where it says "Library: Gdi32.lib" and "Library: Dwmapi.lib", respectively.
This tells us we must link
gdi32.lib and
Dwmapi.lib. Add those to the link line.
QZPB\mjb D:\imgui\examples\example_win32_directx11>g++ -fno-strict-aliasing -g -DUNICODE -D_UNICODE -I ../.. -I ../../backends main.cpp ../../backends/imgui_impl_dx11.cpp ../../backends/imgui_impl_win32.cpp ../../imgui*.cpp -ld3d11 -ld3dcompiler -lgdi32 -ldwmapi -o example_win32_directx11.exe |
In general you'd repeat that process, being extremely careful to select the proper versions of any needed libraries, until the linker shuts up.
Success! Now run ./example_win32_directx11.exe and make sure it doesn't crash.