# How to link windows libs properly?

**URL:** <https://forums.unrealengine.com/t/how-to-link-windows-libs-properly/502558>\
**Category:** C++\
**Tags:** unreal-engine, Engine-Source-Code, GitHub, Engine-Source-GitHub, UE4, UE4-27, question\
**Created:** [February 27, 2022, 11:53am UTC](https://forums.unrealengine.com/t/how-to-link-windows-libs-properly/502558 "2022-02-27T11:53:46Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![dmayola](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@dmayola](https://forums.unrealengine.com/u/dmayola)\
**Post date:** [February 27, 2022, 11:53am UTC](https://forums.unrealengine.com/t/how-to-link-windows-libs-properly/502558/1 "2022-02-27T11:53:46Z")

</div>

Hello,

I’ve been building a library that requires some libs from windows. I managed to make it work but I’m struggling to find a proper solution.

```auto
        PublicAdditionalLibraries.Add("C:/Program Files (x86)/Microsoft Visual Studio/2019/Professional/VC/Tools/MSVC/14.29.30133/atlmfc/lib/x64/atls.lib");
        PrivateIncludePaths.AddRange(
            new string[] {
        		// ... add other private include paths required here ...
                "C:/Program Files (x86)/Microsoft Visual Studio/2019/Professional/VC/Tools/MSVC/14.29.30133/atlmfc/include"
            }
            );

```

As you can see, I’m pointing to a specific VS version and path, which is unacceptable.

Do you have any suggestions or can you point me to a proper example?  
I didn’t know which library might have a similar requirement to compare their solution.

Thank you all.

---

<div class="post-metadata">

**Author:** ![NickSombraNTC](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@NickSombraNTC](https://forums.unrealengine.com/u/NickSombraNTC)\
**Post date:** [November 8, 2023, 4:12pm UTC](https://forums.unrealengine.com/t/how-to-link-windows-libs-properly/502558/2 "2023-11-08T16:12:17Z")

</div>

Hi. Did you find any solution? I’m having the same problem… bruh

---

<div class="post-metadata">

**Author:** ![abizeauMaxon](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@abizeauMaxon](https://forums.unrealengine.com/u/abizeauMaxon)\
**Post date:** [October 11, 2024, 3:03pm UTC](https://forums.unrealengine.com/t/how-to-link-windows-libs-properly/502558/3 "2024-10-11T15:03:37Z")

</div>

I know this question is old, but I had the exact same problem recently! So after spending a lot of time on it, this is my current solution.

Which involve using the `VSAPPIDDIR` environment variable that is defined at runtime. Then using path parsing, I’m able to retrieve the atls.lib that I want and add it to the linkage.

```auto
// Used to discover all `environment variables` setted during this, which make me discover `VSAPPIDDIR`.
//foreach (System.Collections.DictionaryEntry e in System.Environment.GetEnvironmentVariables())
//{
// System.Console.WriteLine(e.Key + ":" + e.Value);
//}

//System.Console.WriteLine(System.Environment.GetEnvironmentVariable("VSAPPIDDIR"));
System.IO.DirectoryInfo vsappdir = new System.IO.DirectoryInfo(System.Environment.GetEnvironmentVariable("VSAPPIDDIR"));
System.IO.DirectoryInfo vsdir = vsappdir.Parent.Parent;
System.IO.DirectoryInfo msvcDir = new System.IO.DirectoryInfo(Path.Combine(vsdir.FullName, "VC", "Tools", "MSVC"));

bool foundLib = false;

// Use `descending` order, in case we have multiple "version", prefer the latest.                
foreach (System.IO.DirectoryInfo e in msvcDir.GetDirectories().OrderByDescending(p => p.Name))
{
    System.IO.DirectoryInfo libDir = new System.IO.DirectoryInfo(Path.Combine(e.FullName, "atlmfc", "lib", "x64", "atls.lib"));
    if (File.Exists(libDir.FullName))
    {
        PublicAdditionalLibraries.Add(libDir.FullName);
        foundLib = true;
        break;
    }
}

if (!foundLib)
{
    throw new BuildException("Could not found any atls.lib to link to");
}

```

---

<div class="post-metadata">

**Author:** ![oleksii-dtsc](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@oleksii-dtsc](https://forums.unrealengine.com/u/oleksii-dtsc)\
**Post date:** [January 13, 2025, 10:14am UTC](https://forums.unrealengine.com/t/how-to-link-windows-libs-properly/502558/4 "2025-01-13T10:14:36Z")

</div>

VSAPPDIR may not be set if the build is not using VS directly (for example if working with VS Code). So the solution above won’t work.

A more reliable solution (that also supports different architectures and toolchain versions) would be:

```auto
		// Workaround for the linker being unable to locate ATLS.LIB under VS Code
		if(Target.WindowsPlatform.ToolChainDir != null)
		{
			string AtlPath = Path.Combine(Target.WindowsPlatform.ToolChainDir, "atlmfc", "lib", 
				Target.Architecture.ToString());
			PublicSystemLibraryPaths.Add(AtlPath);
		}

```
