Register
Handmade Hero»Forums»Code»I seem to be unable to pass linker options as environment variable in build.bat
2 posts
I seem to be unable to pass linker options as environment variable in build.bat
2 weeks, 6 days ago Edited by Lichinka on May 23, 2021, 5:30 a.m. Reason: Initial post
I am on Day 17.
These are the contents of my build.bat:
1
2
3
4
5
6
7
@echo off
set CmnCompileFlags = -MT -Gm- -nologo -GR- -EHsc -EHa- -Oi -W4 -wd4201 -wd4100 -FC -Zi -Fmwin32_game.map 
set CmnLinkerFlags = -subsystem:windows,5.2 -opt:ref user32.lib Gdi32.lib Ole32.lib
IF NOT EXIST ..\build mkdir ..\build
pushd ..\build
cl %CmnCompileFlags% ..\code\win32_game.cpp /link %CmnLinkerFlags%
popd

When trying to run it I receive a bunch of LNK2019 erros such as this one:
win32_game.obj : error LNK2019: ссылка на неразрешенный внешний символ __imp_StretchDIBits в функции "void __cdecl Win32DisplayBufferInWin
However this compiles just fine:
1
2
3
4
5
6
7
@echo off
set CmnCompileFlags = -MT -Gm- -nologo -GR- -EHsc -EHa- -Oi -W4 -wd4201 -wd4100 -FC -Zi -Fmwin32_game.map 
set CmnLinkerFlags = -subsystem:windows,5.2 -opt:ref user32.lib Gdi32.lib Ole32.lib
IF NOT EXIST ..\build mkdir ..\build
pushd ..\build
cl %CmnCompileFlags% ..\code\win32_game.cpp /link -subsystem:windows,5.2 -opt:ref user32.lib Gdi32.lib Ole32.lib
popd

Yes, I checked if compiler flags were in effect.
I also tried moving -subsystem:windows,5.2 and -opt:ref ouside %CmnLinkerFlags% to see if that would help. It seems that linker just doesn't see any libraries inside of a variable.
I would really like to know what is going on here and whether there is a workaround.
Mārtiņš Možeiko
2229 posts / 1 project
I seem to be unable to pass linker options as environment variable in build.bat
2 weeks, 6 days ago Edited by Mārtiņš Možeiko on May 23, 2021, 6:57 a.m.
The problem is that you setting environment variables with set CmnCompileFlags = xxx syntax. This actually sets CmnCompileFlags(space) environment variable.

So if you want to use it like that, you need to add space when using it with % symbols: %CmnCompileFlags % (notice space before closing % symbol. Or you can do set CmnCompileFlags=xxx without any spaces around = symbol.

You can check this easily by adding following lines to your .bat file:
1
2
echo CmnCompileFlags without space = %CmnCompileFlags%
echo CmnCompileFlags WITH space = %CmnCompileFlags %


Same thing with CmnLinkerFlags variable.
2 posts
I seem to be unable to pass linker options as environment variable in build.bat
2 weeks, 6 days ago Edited by Lichinka on May 23, 2021, 10 a.m.
mmozeiko
So if you want to use it like that, you need to add space when using it with % symbols: %CmnCompileFlags %

Thanks, this solved the issue. Any ideas why CmnCompileFlags worked just fine? Cmd output for illustration:
1
2
3
4
CmnCompileFlags without space =  -MT -Gm- -nologo -GR- -EHsc -EHa- -Oi -W4 -wd4201 -wd4100 -FC -Zi -Fmwin32_game.map
CmnCompileFlags WITH space =  -MT -Gm- -nologo -GR- -EHsc -EHa- -Oi -W4 -wd4201 -wd4100 -FC -Zi -Fmwin32_game.map
CmnLinkerFlags without space =
CmnLinkerFlags WITH space =  -subsystem:windows,5.2 -opt:ref user32.lib Gdi32.lib Ole32.lib

Edit: I did a little bit more fidgeting with the bat file, and I can't reproduce this behaviour anymore. At this point I do need to add an extra space into %CmnCompileFlags % for it to work. I am not sure what change in particular caused it, but I do not think it is worth your attention to investigate what was probably user error on my part.