libprotobuf_ue4 icon indicating copy to clipboard operation
libprotobuf_ue4 copied to clipboard

Compile protobuf with Visual Studio 2017 and integrate with UE4

Open NelsonBilber opened this issue 7 years ago • 8 comments

Hi,

I already compiled protobuf with visual studio 2017 ( static compile in 64 bits) To validate the result I have developed a simple console application to check if I can use the *.pb.cc and *.pb.h files Everything is ok

Next step I tried to incorporate in a unreal engine 4 project. Set up a build files

File.Build.cs

... PublicIncludePaths.Add(ModulePath + "/Include"); PublicLibraryPaths.Add(ModulePath + "/Lib");

    PublicAdditionalLibraries.Add("libprotobuf.lib");

...

But it gave me strange compile errors

1>C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\VC\Tools\MSVC\14.11.25503\INCLUDE\type_traits(565): error C4647: behavior change: __is_pod(google::protobuf::internal::AuxillaryParseTableField) has different value in previous versions

...\Include\google/protobuf/generated_message_table_driven.h(159): note: see reference to class template instantiation 'std::is_podgoogle::protobuf::internal::AuxillaryParseTableField' being compiled

...\Messages.pb.h(992): warning C4668: 'PROTOBUF_INLINE_NOT_IN_HEADERS' is not defined as a preprocessor macro, replacing with '0' for '#if/#elif' ...\Include\google/protobuf/descriptor.pb.h(4113): warning C4668: 'PROTOBUF_INLINE_NOT_IN_HEADERS' is not defined as a preprocessor macro, replacing with '0' for '#if/#elif' ...\Messages.pb.cc(258): error C4125: decimal digit terminates octal escape sequence ...\Messages.pb.cc(259): error C4125: decimal digit terminates octal escape sequence

Search on web but I haven't mutch success: Example this error is real strange !!!!

"...\MSVC\14.11.25503\INCLUDE\type_traits(565): error C4647: behavior change: __is_pod(google::protobuf::internal::AuxillaryParseTableField) has different value in previous versions .."

Have you tried to compile this source code with recent versions of Protobuf and Visual Studio ?

Protocol Buffer 3.3.0 Visual Studio 2017 Release 64 bits

Regards Nelson Bilber

NelsonBilber avatar Aug 14 '17 20:08 NelsonBilber

Same here. Have you found a solution for this issue, @NelsonBilber ?

fweidner avatar Aug 25 '17 11:08 fweidner

Not really ... the only version that I can make work was 3.2.0.

After generate visual studio solution I need to change the projects settings on C++ -> code generation to Multi-threaded DLL (/MD)

Then when generate files with protoc on filles *.pb.cc I also have to disable some warning in order to compile project

Example: Add these pragmas on protobuff generated files " *.pb.cc " on the top of file

//... pragma warning(push) #pragma warning(disable : 4668) // warning C4668: A symbol that was not defined was used with a preprocessor directive. The symbol will evaluate to false //...

Don't forget on unreal project source code to include theses #include "AllowWindowsPlatformTypes.h" //... #iinclude "...*.pb.cc " //.. #include "HideWindowsPlatformTypes.h"

I'm not complete confident that is robust lets see during development if it's a stable solution

Regards

NelsonBilber avatar Aug 25 '17 13:08 NelsonBilber

Was able to compile it successfully. Here are my steps: https://medium.com/@0xflarion/using-ue4-w-google-protocol-buffers-ae7cab820d84

fweidner avatar Aug 29 '17 08:08 fweidner

@fweidner Any chance of a pull request (thanks for the blog post by the way!)

aaronsnoswell avatar Sep 01 '17 05:09 aaronsnoswell

@aaronsnoswell i did not base my code on this repo (yet). I'll try to reproduce it next week and will create a pull request.

fweidner avatar Sep 04 '17 10:09 fweidner

@fweidner any update on this? I tried to compile this project with protobuf 3.5 but I get several error messages:

  1. Complie Protobuf 3.5 via your guide
  2. Copy and replace the lib files into the plugin folder
  3. Update the Protobuf submodule to 3.5
  4. Changed to libprotobuf.Build.cs to:
// Copyright 2016 Code 4 Game. All Rights Reserved.
using System.IO;
using System;

using UnrealBuildTool;

public class libprotobuf : ModuleRules
{
    public libprotobuf(TargetInfo Target)
    {
        Type = ModuleType.External;

        bool is_supported = false;
        if ((Target.Platform == UnrealTargetPlatform.Win32) || (Target.Platform == UnrealTargetPlatform.Win64))
        {
            is_supported = true;

            string vs_path = "vs"
                + WindowsPlatform.GetVisualStudioCompilerVersionName()
                + ((Target.Platform == UnrealTargetPlatform.Win64) ? "win64" : "");
            string protobuf_lib_directory_full_path = System.IO.Path.Combine(ModuleDirectoryFullPath, "lib", vs_path);

            PublicLibraryPaths.Add(protobuf_lib_directory_full_path);

            PublicAdditionalLibraries.Add("libprotobuf.lib");

            Definitions.AddRange(
                new string[]
                {
                    ((Target.Platform == UnrealTargetPlatform.Win64) ? "WIN64" : "WIN32"),
                    "_WINDOWS",
                    "NDEBUG",
                    "GOOGLE_PROTOBUF_CMAKE_BUILD",
                });
        }

        if (is_supported)
        {
            string protobuf_code_directory_full_path = System.IO.Path.Combine(ModuleDirectoryFullPath, "protobuf", "src");

            PublicSystemIncludePaths.Add(protobuf_code_directory_full_path);
        }
        LoadGoogleProtocolBuffers(Target);
    }

    string ModuleDirectoryFullPath
    {
        get { return System.IO.Path.GetFullPath(ModuleDirectory); }
    }

    public bool LoadGoogleProtocolBuffers(TargetInfo Target)
    {
    bool isLibrarySupported = false;

    if ((Target.Platform == UnrealTargetPlatform.Win64)){
        isLibrarySupported = true;

        string LibrariesPath = Path.Combine(ThirdPartyPath, "libprotobuf", "lib");

        Console.WriteLine("... LibrariesPath -> " + LibrariesPath);
        PublicAdditionalLibraries.Add(Path.Combine(LibrariesPath, "libprotobufd" + ".lib"));
    }

    if (isLibrarySupported){
        string IncludePath= Path.Combine(ThirdPartyPath, "libprotobuf", "include");

        Console.WriteLine("... IncludePath -> " + IncludePath);
        PrivateIncludePaths.Add(IncludePath); }

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

        return isLibrarySupported;
    }
    private string ModulePath
    {
        get { return ModuleDirectory; }
    }
    private string ThirdPartyPath
    {
        get { return Path.GetFullPath(Path.Combine(ModulePath, "../../ThirdParty/")); }
    }

}

//Error messages when compiling my game

Schweregrad	Code	Beschreibung	Projekt	Datei	Zeile	Unterdrückungszustand
Fehler	C4800	"google::protobuf::internal::Atomic64": Variable wird auf booleschen Wert ("True" oder "False") gesetzt (Auswirkungen auf Leistungsverhalten möglich)	Community	C:\Users\Eli\Code\Unreal\Community\Source\ThirdParty\libprotobuf\protobuf\src\google\protobuf\io\coded_stream.h	857

Schweregrad	Code	Beschreibung	Projekt	Datei	Zeile	Unterdrückungszustand
Fehler	C4647	Verhaltensänderung: __is_pod(google::protobuf::internal::AuxillaryParseTableField) hat in früheren Versionen einen anderen Wert.	Community	C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\INCLUDE\type_traits	414	

Schweregrad	Code	Beschreibung	Projekt	Datei	Zeile	Unterdrückungszustand
Fehler	C4541	"dynamic_cast" für polymorphen Typ "google::protobuf::Message" mit /GR- verwendet; unvorhersehbares Verhalten möglich	Community	C:\Users\Eli\UE4\UE_4.18\Engine\Source\Runtime\CoreUObject\Public\Templates\Casts.h	390	```

kelteseth avatar Jan 21 '18 13:01 kelteseth

Hi,

afaik, the C4647 error should be gone if you do step 2 in the guide (comment out "static_assert(std::is_pod<AuxillaryParseTableField>::value, "");". I ignored the 4800 error with the by ignoring it with a pragma (see step 7). Be aware that you have to add the "pragma warning (disable ...)" in each of your pb.h file.

Currently, we are facing more problems. For more info see: https://github.com/google/protobuf/issues/4129

Best, Florian

fweidner avatar Jan 21 '18 13:01 fweidner

I was able to build and run latest 3.12.0 protobuf and it looks to be running just fine. I've unchecked MSVC_STATIC_RUNTIME, built and copied includes and lib file into according folders. Let me know if you get some questions. Also when going more complicated protocols - you need to suppress some warnings, otherwise it wont compile

avdept avatar Jul 30 '20 18:07 avdept