caseymcc / UE4CMake

Provides a simple way to add a cmake lib to any Unreal Engine 4 (UE4) or 5 (UE5) project.
MIT License
78 stars 14 forks source link

Can't compile JoltPhysics in debug using this library #19

Closed AndreaCatania closed 4 months ago

AndreaCatania commented 4 months ago

Hi, I'm trying to compile Jolt (a Physics Library using CMake) in unreal engine using this plugin.

When I compile the engine in Development it works, when I try to compile in debug this error appears:

Jolt.lib: Error  : LNK2038: mismatch detected for '_ITERATOR_DEBUG_LEVEL': value '2' doesn't match value '0' in SharedPCH.Engine.NonOptimized.Cpp17.h.obj
0>Jolt.lib: Error  : LNK2038: mismatch detected for 'RuntimeLibrary': value 'MDd_DynamicDebug' doesn't match value 'MD_DynamicRelease' in SharedPCH.Engine.NonOptimized.Cpp17.h.obj

Does it means that Unreal expects the Jolt Library to be compiled in release target even when Unreal is compiled in debug?

caseymcc commented 4 months ago

It might be a mix-match in the builds. The 2nd entry seems to imply that the engine is trying to load a debug version of the library but the library it loads is built in release.

You will have to dig through the logs a bit to find out what is actually happening.

caseymcc commented 4 months ago

NM I think I was reading the above wrong, yeah looks like your take seems right, assuming SharedPCH.Engine.NonOptimized.Cpp17.h.obj is coming from the unreal engine. Have to dig through the cmake logs to see what was requested vs what was built. I am not sure I covered all the cases in the build tools I know from the few libs I have used release/debug/development seem to work.

Also from the iterator mismatch something seems to be trying to use stdlib algos across library boundaries which can be fraught with complications.

AndreaCatania commented 4 months ago

image According to this description it seems that the library should never be compiled as Debug, rather it should just compile without optimization. Do you think it make sense to change this plugin to enforce that or at least add a way to control the building target from CMakeTarget.add(Target, this, ....?

caseymcc commented 4 months ago

In that case, likely the build type is wrong https://github.com/caseymcc/UE4CMake/blob/0be0d4a380235f30ca60d2fbf9a4db3d32716c1a/Source/CMakeTarget.Build.cs#L243

caseymcc commented 4 months ago

You could remove that above line and see if it works then.

AndreaCatania commented 4 months ago

Trying, I let you know in a bit.

caseymcc commented 4 months ago

I went through the build system (at least for linux) to try and replicate the build configs exactly based on Unreal's build tool. However although the code is there, there is no programmatic way of fetching it as most of the structs are listed as private, so you have to replicate the flags manually which means you would have to be constantly on guard for every change and have multiple versions of the flags every time they change them. https://github.com/caseymcc/UE4CMake/compare/main...linux_clang#diff-87630ceacda601567408a06c4f99aa53527db8687f0403c2bf7335bf4f653365R547

AndreaCatania commented 4 months ago

Yeah, that's bad 🤔

caseymcc commented 4 months ago

replicating the build flags is the only safe way to use the std lib across library boundaries.

AndreaCatania commented 4 months ago

Btw, removing the line you mentioned, fixed the build issue. I'll provide a PR in a bit.

AndreaCatania commented 4 months ago

It would be great to build the library using distribution target, when shipping the game. I wonder if it's desirable to add that here: https://github.com/caseymcc/UE4CMake/blob/0be0d4a380235f30ca60d2fbf9a4db3d32716c1a/Source/CMakeTarget.Build.cs#L243

If not. I've noticed it's possible to enforce the build target. Can you please tell me how?

caseymcc commented 4 months ago

if you just add -DCMAKE_BUILD_TYPE={Debug/Release/etc...} to the cmake_args of CMakeTarget.add(...) it will notice it and force that build type, https://github.com/caseymcc/UE4CMake/blob/0be0d4a380235f30ca60d2fbf9a4db3d32716c1a/Source/CMakeTarget.Build.cs#L84

caseymcc commented 4 months ago

btw internally via the link above with the DebugGame. It is already using the Target.Configuration to determine what the cmake build should be. Just seems that although DebugGame says debug it is expecting Release with optimizations off. Just need to change the optimization flag to debug when DebugGame is used but leave it is Release mode.

AndreaCatania commented 4 months ago

I'm not a huge CMake user, so please feel free to modify my PR accordingly to make sure we can change the library optimization, as you suggested above.

SalamiArmi commented 1 month ago

I don't think this solution is correct.

When we say something is "compiled without optimisation" my understanding is that this means "compiled in debug". There's a more detailed discussion of what each configuration means at this link. An earlier pull request I made synced the configurations in a way that reflects the documentation.

After PR #20, is it possible to launch the editor with the ability to debug modules? Now under the "DebugGame Editor" config, everything builds in release. I think the "Debug" profile is only for standalone builds outside of the editor, which is crippling for iteration time.

Going back to the very first post about Jolt, is it possible that it was trying to grab the release library instead of the debug one? It should have been grabbing the debug Jolt.lib, but the error message indicates it was grabbing the release one. Is difficult to parse out the specifics from the brief description, but feels like a config issue on Jolt's end.

I'm happy to try and debug the specific Jolt issue for you @AndreaCatania if it lets me debug my modules again.

AndreaCatania commented 1 month ago

Hello @SalamiArmi, this link as this code desc explicitly say that unreal is never build in "debug" mode but it's always build in release but without optimization.

If you want to build unreal in debug you have to build unreal from source.

If you try to link your module compiled in debug mode, unreal will not link it and will ask you to compile the library in "release". The correct solution to the problem is to compile the library in release but with optimization disabled.

Regarding your question:

After PR https://github.com/caseymcc/UE4CMake/pull/20, is it possible to launch the editor with the ability to debug modules?

Yes it's still possible to do it.

SalamiArmi commented 3 weeks ago

Perhaps I wasn't entirely clear on what I meant in my earlier comment. My understanding is that there are three categories you might want to debug:

  1. Game modules (which are defined in the project's xyz.Target.cs files)
  2. Plugin modules (which may be configured via CMake in this plugin)
  3. Unreal Engine itself

I'm interested in option no. 2 here. As I understand it "release without optimisations" is functionally identical to debug mode (specific preprocessor options excluded). This doesn't map directly onto the release configuration which the tool will now default to. In your case, building your project under the "DebugGame" configuration Jolt will be be compiled as release (with optimisations).

If you still think this is correct then we can agree to disagree on our interpretations of the UE docs. I'll just keep my interpretation on my fork, because it's relevant to my workflow.