Download

Issues with Linking External Libraries

Hello all,

How do you link libraries in 4.18/4.19 I’ve followed the following wiki’s but none seem to be working, and im using VS 2017.

https://wiki.unrealengine.com/Linking_Static_Libraries_Using_The_Build_System#Visual_Studio_2013.2F2015_-Library_Configuration
https://wiki.unrealengine.com/How_to_Link_External_C_Libraries
.dll_.lib_With_Your_Project_%26_Package_With_Game,_Fast_And_Easy

And I’m trying to include the sqlite3 library. NOT THE PLUGIN. I’d rather not use the plugin, and figure out how to include 3rd party libraries.

With the first link i can include “sqlite3.h” in the code, but I can’t call any of the functions from that library or Unreal throws a LINK 2019 Error. Does anyone know how to fix this.

So if anyone could tell me why this is erroring or how to fix that would be great! Thanks:)

Did you add the .lib to additional dependencies in your build.cs file?

Yeah I did, it just doesn’t pop up. I can #include “sqlite3.h” but i just can’t call anything from the library even if VS is picking up on it.

Can you include the relevant bits from your build.cs file (or maybe even the whole file for clarity)? It’s possible you’re not linking to the .lib correctly in there.

I’m at work.

But the file i was trying to include “sqlite3.h” from was throwing the errors.

I have a ThirdParty folder with the following structure.

in there I have SQLite3
SQLite/include - contains sqlite3.h
SQLite/lib - contains sqlite3.lib

In my Build.cs

PublicDependencyModuleNames.AddRange(new string] { “Engine”, “Core”, “CoreUObject” });
string ThirdPartyPath = Path.GetFullPath( Path.Combine( ModulePath, “…/…/ThirdParty/” ) );
string LibrariesPath = Path.Combine(ThirdPartyPath, “SQLite3”, “lib”);
string IncludesPath = Path.Combine(ThirdPartyPath, “SQLite3”, “include”);
string LibraryName = Path.Combine(LibrariesPath, “sqlite3.h”);

PublicAdditionalLibraries.Add(LibraryName);
PublicIncludePaths.Add(IncludesPath);

This is what i have but for some reason i get the LINK2019 error, so the question is what is causing this error.

In a function i have TestDBConnection() {
sqlite3* db;
int rc = sqlite3_open(“PathToDB”, &db);

}

and in the function i am throwing and unresolved external symbol error on both of them.
This is a very weird error cause I am able to include the sqlite3.h library in the .cpp file, so the question is what the heck am i missing. Also will this cause packaging issues later on?

You’re adding the wrong path to PublicAdditionalLibraries, this should be the .lib file.

string LibraryName = Path.Combine(LibrariesPath, “sqlite3.lib”);

A link error means your header include is working fine but the methods were not found during linking, i.e. because the library was not found.

Eh i can’t remember off the top of my head if that was correct. What if i am adding the sqlite3.lib? What would be causing the issue?

Do I need to include the .dll, and is there a specific way to call the functions.

Maybe it can be solved by regenerating project files and doing a rebuild in that case. It seems correct otherwise.

A Rebuild as in in regenerating a solution, or regenerating a solution, and deleting the binaries, intermediate and saved files, and then compile in editor or build from Visual Studio.

Also could it be an issue with the sqlite is a .c file and not a .cpp?

So here are pictures with my issue and im still getting the Original Post issue

Build.cs


And at that end im getting the LINK2019 Error.

Any help would be greatly appreciated. Thanks

Was this issue resolved? I am facing the same issue.

Joining this thread, hoping for a solution :heart:

I’m going to take the renewed interest here to be about linking libraries and not addressing anything involving the source code/header inclusion from third party libraries. Consequently, this is just going to address linking to third party libraries compiled outside of UE.

The best way I’ve found to do this to support both editor and game builds (e.g., packaging) so far is shown in the following plugin build file:

For Windows with dlls, I do three things:

  1. Add the .lib file to PublicAdditionalLibraries
  2. Add the dll PublicDelayLoadDLLs
  3. Copy the dll to the Binaries/Win64 directory in your project root directory

You can see an example of this in the following code segment:



    string BaseDirectory = Path.GetFullPath(Path.Combine(ModuleDirectory,
      "..", "..", "..", "..", ".."));
    string BaseLibDirectory = Path.GetFullPath(Path.Combine(ModuleDirectory,
      "..", "..", ".."));
    string MadaraLibDirectory = Path.Combine(BaseLibDirectory, "ThirdParty",
      "madara", Target.Platform.ToString());

    if (Target.Platform == UnrealTargetPlatform.Win64)
    {
      string BinariesDir = Path.Combine(BaseDirectory, "Binaries", "Win64");

      System.IO.File.Copy(Path.Combine(MadaraLibDirectory, "MADARA.dll"),
        Path.Combine(BinariesDir, "MADARA.dll"), true);

      PublicAdditionalLibraries.Add(Path.Combine(MadaraLibDirectory, "MADARA.lib"));
      PublicDelayLoadDLLs.Add(Path.Combine(MadaraLibDirectory, "MADARA.dll"));
    }


Is this documented in the Engine, wiki, and blogs accurately? No, absolutely not. It took me a long time to debug what was going on. So, let me explain why these steps are necessary (I’ve been working primarily in 4.21-4.24, but these steps should work for earlier versions as well).

  1. You have to add the .lib to PublicAdditionalLibraries, regardless of the fact that you usually don’t need the .lib file with DLL loading in C++ linking in general. This appears to be an issue with linking in the editor mode. I’m only copying the dll over to the binaries folders for packaging and it seems to work, as you would expect with normal DLL loading.
  2. DLLs are best put in PublicDelayLoadDLLs. It may work in other places, but I know it works in this array on Windows as you would expect it to.
  3. To package the library with the game, you need to tell the build system to copy the .dll to the Binaries directory and the appropriate folder. If you don’t copy this to the Binaries folder, it won’t be packaged. However, if you do copy this as I’m doing here, you will have a strange side effect to keep in mind. This System.IO.File.Copy command needs to overwrite the destination file (the dll in the Binaries directory). If it can’t, it will fail the build. This is a problem in the editor after you package a build. If you try to run in the editor or build while the editor is still up, it is likely to have locked the dll if your plugin is opening the dll (mine does during the startup method). So, if you get failure messages after packaging, close the editor, and then try deleting the .dll files in your Binaries/Win64 directory. It should work after that. I’ve seen other methods of potentially packaging libs (i.e., you don’t necessarily have to use System.IO.File.Copy), but this is the method I’m currently using to include the libs in packaging.

Linux and Mac should be a bit simpler to debug. Here’s the process I’m currently using and how the code looks in one of my current third party libs.

For Linux so, I do the following:

  1. Add the lib*.so files to PublicAdditionalLibraries
  2. Copy the .so to the Binaries/Linux directory


    string BaseDirectory = Path.GetFullPath(Path.Combine(ModuleDirectory,
      "..", "..", "..", "..", ".."));
    string BaseLibDirectory = Path.GetFullPath(Path.Combine(ModuleDirectory,
      "..", "..", ".."));
    string MadaraLibDirectory = Path.Combine(BaseLibDirectory, "ThirdParty",
      "madara", Target.Platform.ToString());

    if (Target.Platform == UnrealTargetPlatform.Linux)
    {
      string BinariesDir = Path.Combine(BaseDirectory, "Binaries", "Linux");

      System.IO.File.Copy(Path.Combine(MadaraLibDirectory, "libMADARA.so"),
        Path.Combine(BinariesDir, "libMADARA.so"), true);
      PublicAdditionalLibraries.Add(Path.Combine(MadaraLibDirectory, "libMADARA.so"));
    }


1 Like