godotengine / godot-cpp

C++ bindings for the Godot script API
MIT License
1.72k stars 569 forks source link

CMake test project attempting to link with wrong file #945

Open justofisker opened 1 year ago

justofisker commented 1 year ago

Issue description

When attempting to build the test project for this using CMake it fails do to looking for the godot-cpp library file

Output when attempting to build test project using VSCode's CMake extension:

[main] Building folder: test 
[build] Starting build
[proc] Executing command: "C:\Program Files\CMake\bin\cmake.EXE" --build c:/Users/bea/Desktop/godot-cpp/test/build --config Debug --target godot-cpp-test -j 26 --
[build] MSBuild version 17.4.0+18d5aef85 for .NET Framework
[build]   Checking Build System
[build]   Building Custom Rule C:/Users/bea/Desktop/godot-cpp/test/CMakeLists.txt
[build]   example.cpp
[build]   register_types.cpp
[build]   Generating Code...
[build] LINK : fatal error LNK1104: cannot open file 'godot-cpp.windows.release.64.lib' [C:\Users\bea\Desktop\godot-cpp\test\build\godot-cpp-test.vcxproj]
[proc] The command: "C:\Program Files\CMake\bin\cmake.EXE" --build c:/Users/bea/Desktop/godot-cpp/test/build --config Debug --target godot-cpp-test -j 26 -- exited with code: 1 and signal: null
[build] Build finished with exit code 1

My workaround

The current way I am working around this is adding "lib" to the beginning of this filename

https://github.com/godotengine/godot-cpp/blob/f1d501f97749fd70f590a3e82b81e55d6cf1d2d7/test/CMakeLists.txt#L138

and changing

https://github.com/godotengine/godot-cpp/blob/f1d501f97749fd70f590a3e82b81e55d6cf1d2d7/test/CMakeLists.txt#L112-L121

to

 set(BITS x86_32) 
 if(CMAKE_SIZEOF_VOID_P EQUAL 8) 
    set(BITS x86_64) 
 endif(CMAKE_SIZEOF_VOID_P EQUAL 8)

if(CMAKE_BUILD_TYPE MATCHES Debug)
    set(GODOT_CPP_BUILD_TYPE Template_Debug)
else()
    set(GODOT_CPP_BUILD_TYPE Template_Release)
endif()

Issues with my workaround

There are two problems with this second change. Firstly, for non x86 platforms the x86_ prefix in not valid. Secondly, while the only release binary build type is template_release, both template_debug and editor are valid build types for a debug builds and can both be used by the editor if linked against, but this change only checks for template_debug.

enetheru commented 1 month ago

I have a PR for modernising the cmake build scripts that I believe would resolve this issue.

The test project becomes a target of the main project and doesn't need to be configured separately.

Also for visual studio is a multi-config generator which means passing in CMAKE_BUILD_TYPE at the configure stage is ignored by visual studio always setting the config to the default which is Debug. The build configuration needs to be passed at the build command like so cmake --build . --config Release. With the current master both the config stage and build stage require to set the config type. but if my PR gets merged, then its only required at the build command for visual studio.