LNK2005 error when integrating LuaJit in UE4 (Unreal Engine 4)

484 Views Asked by At

lua51.lib(lua51.dll) : error LNK2005: _vsnprintf already defined in libcurl_a.lib(cryptlib.obj)

GameName.Build.cs ->

// Fill out your copyright notice in the Description page of Project Settings.

using System.IO;
using UnrealBuildTool;

public class GameName : ModuleRules
{
    private string ThirdPartyPath
    {
        get { return Path.GetFullPath(Path.Combine(ModuleDirectory, "../../ThirdParty/")); }
    }  
    private bool LoadLua()
    {
        bool isLibSupported = false;

        string LibrariesPath = Path.Combine(ThirdPartyPath, "Lua", "libraries");

        PublicAdditionalLibraries.Add(Path.Combine(LibrariesPath, "lua51.lib"));

        PublicIncludePaths.Add(Path.Combine(ThirdPartyPath, "Lua", "includes"));

        Definitions.Add(string.Format("WITH_LUA_BINDING={0}", isLibSupported ? 1 : 0));

        return true;
    }
    public GameName(ReadOnlyTargetRules Target) : base(Target)
    {
        PCHUsage = PCHUsageMode.UseExplicitOrSharedPCHs;

        PublicDependencyModuleNames.AddRange(new string[] { "Core", "CoreUObject", "Engine", "InputCore" });

        PrivateDependencyModuleNames.AddRange(new string[] {  });

        LoadLua();

        // Uncomment if you are using Slate UI
        // PrivateDependencyModuleNames.AddRange(new string[] { "Slate", "SlateCore" });

        // Uncomment if you are using online features
        // PrivateDependencyModuleNames.Add("OnlineSubsystem");

        // To include OnlineSubsystemSteam, add it to the plugins section in your uproject file with the Enabled attribute set to true
    }
}

ActorName.cpp ->

// Fill out your copyright notice in the Description page of Project Settings.

#include "ActorName.h"

extern "C" {
#include "lua.h"
#include "lualib.h"
#include "lauxlib.h"
#include "luaconf.h"
#pragma comment(lib, "lua51.lib")
}

// Sets default values
APart::APart()
{
    // Set this actor to call Tick() every frame.  You can turn this off to improve performance if you don't need it.
    PrimaryActorTick.bCanEverTick = true;
}

// Called when the game starts or when spawned
void APart::BeginPlay()
{
    Super::BeginPlay();
    lua_State *L = luaL_newstate();
    luaL_openlibs(L);
    FVector NewLocation = GetActorLocation();
    NewLocation.Z = NewLocation.Z + 200.0f;
}

// Called every frame
void APart::Tick(float DeltaTime)
{
    Super::Tick(DeltaTime);
    /*FVector NewLocation = GetActorLocation();
    float DeltaHeight = (FMath::Sin(RunningTime + DeltaTime) - FMath::Sin(RunningTime));
    NewLocation.Z += DeltaHeight * 100.0f;
    NewLocation.Y += DeltaHeight * 800.0f;//Scale our height by a factor of 20
    RunningTime += DeltaTime;
    SetActorLocation(NewLocation);*/
}

I compiled LuaJit for 64x, I'm not including the 32x build, do I need to? I don't intend to release my game for 32 bit systems as there's no point lol (Other than IOS because I'm pretty sure you have to upload 32 bit and 64 bit versions of the app :3)

I only ever included Lua51.lib once? Have I done something wrong?

1

There are 1 best solutions below

1
On

I know this is a pretty old question, but since I haven't seen an appropriate answer anywhere else and spent far too long figuring this out, I figured I'd help out any poor soul that is running into the same issue.

The basic, afaik unavoidable problem is that the msvcbuild version of LuaJIT has extra symbols in it for (apparently) no reason. Here are the steps I took to compile a working version:

  • Download the LuaJIT Windows source
  • Open the Makefile and
    • Set the mode to dynamic
    • OPTIONALLY enable the DLUAJIT_ENABLE_LUA52COMPAT flag
  • Compile using mingw32-make (mingw-w64)
  • Copy the lua51.dll file to another folder.
  • Using the x64 Native Tools Comand Prompt for VS 2017, export the external symbols found in the DLL dumpbin /EXPORTS lua51.dll > lua51.exports
  • From the exports file, create a separate .def file pointing to the dll containing all the symbol references
  • Using the x64 Native Tools Comand Prompt for VS 2017, generate the lib and exp files based on the def file lib /def:lua51.def /out:lua51.lib

You now have a DLL and a LIB that links to it!

  • You may now copy the dll, lib and exp file to the location you want to install Lua to.
  • Finish following the Installation instructions on the LuaJIT website, for my own paths, that was
    • Copy all files from K:/Git Repos/luajit205-52-libdll/luajit205-52-dll/src to C:/LUA
    • Copy the files from K:/Git Repos/luajit205-52-libdll/luajit205-52-dll/src/jit to C:/LUA/lua/jit

I wrote out the full procedure and how I worked through it, along with the dll/lib/exp I use to one of my git repos, so for extra doc and potentially useful files, head here:
https://github.com/Zaltu/luajit205-52-libdll