Skip to content

CMake - When executing cmake --build build on the command line the project does not build #1724

Closed

Description

@Sailanarmo

Godot version

4.3, 4.4

godot-cpp version

4.3, 4.4

System information

Rocky Linux 9.5, Ubuntu 24.04.2

Issue description

When attempting to build godot-cpp from the command line using traditional CMake methods the Makefile will not execute a default project when cmake --build . or make is executed. It reports that there are files that are to be generated but executing make does nothing. It was only until I opened up the Makefile that CMake generated that I was able to find the target names that can be built:

  • godot-cpp.generate_bindings
  • godot-cpp.template_debug
  • godot-cpp.template_release
  • godot-cpp.editor

I have no idea which of these are supposed to be built or if they all need to be built in order to create the necessary tools and libraries. CMake is supposed to act as a use and forget about it. A seasoned CMake user as myself expects the following:

  cmake -B build -S . -DCMAKE_INSTALL_PREFIX=<My custom installation prefix> -DCMAKE_BUILD_TYPE=Release
  cmake --build build --target install --parallel 10

However, it is expected that user does:

  cmake -B build -S .
  cmake --build build --target godot-cpp.generate_bindings --parallel 10
  cmake --build build --target godot-cpp.template_debug --parallel 10
  cmake --build build --target godot-cpp.template_release --parallel 10
  cmake --build build --target godot-cpp.editor --parallel 10

This is not straight forward and if this is the intended behavior then it should be available in the README.md or in the Official Build Documents. Ideally, it would be nice to have an install version as well for portability. It's nice to be able to point to a customized install of a package using something like the following:

   find_package(godot-cpp 4.4 EXACT REQUIRED HINTS /my/install/location)

Steps to reproduce

Clone the repo down:

  git clone -b godot-4.4-stable https://github.com/godotengine/godot-cpp.git --recursive

Change Directories into godot-cpp and execute:

  cmake -B build -S .
  cmake --build build

Nothing builds.

Minimal reproduction project

Just need CMake, a compatible compiler with C++17, and a command line.

Activity

  1. dsnopek commented on Mar 4, 2025

    @dsnopek
    Collaborator

    I have no idea which of these are supposed to be built or if they all need to be built in order to create the necessary tools and libraries.

    This depends entirely on how the GDExtension you're building works. If you're building someone else's GDExtension, take a look in the .gdextension file - you need to build all the targets that are used there (although, hopefully they've documented that in their documentation). If you're building your own GDExtension, you need the ones you need for your purposes :-)

    That said, with scons, we default to building the template_debug target.

    I don't know if that's a good default, but if it's possible to do a similar default with CMake, perhaps we should for build system parity? But perhaps there's a good reason not to do a default for CMake?

    cc @enetheru the maintainer of the cmake configuration

  2. added
    topic:buildsystem
    enhancement
    on Mar 4, 2025
  3. added this to the 4.x milestone on Mar 4, 2025
  4. enetheru commented on Mar 4, 2025

    @enetheru
    Collaborator

    Hey yeah, so from what I gathered from your comments is that you want to build godot-cpp like a library and install it. But its really not designed to be used like that. It's not a stand alone library.

    What you need to do instead is consume it as part of your gdextension library, by including it in your project and then linking to it.

    Edit: The targets are there to match whatever godot executables you are using.

  5. enetheru commented on Mar 4, 2025

    @enetheru
    Collaborator

    eg using fetchcontent, but you could use any method to include the godot-cpp source.

    cmake_minimum_required( VERSION 3.17 )
    include( FetchContent )
    # Python
    find_package(Python3 3.4 REQUIRED) # pathlib should be present
    
    # Godot-cpp
    set( GODOTCPP_GIT_URL "http://github.com/godotengine/godot-cpp.git" CACHE STRING "The git url of godot-cpp to fetch" )
    set( GODOTCPP_GIT_BRANCH "master" CACHE STRING "The git branch of godot-cpp to fetch" )
    set( GODOT_BUILD_PROFILE "${CMAKE_SOURCE_DIR}/build_profile.json" )
    FetchContent_Declare( godot-cpp
            GIT_REPOSITORY ${GODOTCPP_GIT_URL}
            GIT_TAG ${GODOTCPP_GIT_BRANCH}
            GIT_PROGRESS ON
            SOURCE_DIR "${CMAKE_SOURCE_DIR}/extern/godot-cpp"
    )
    FetchContent_MakeAvailable( godot-cpp )
    
    project( gdexample
            LANGUAGES CXX
    )
    
    add_library( my_gdextension SHARED )
    
    target_sources( my_gdextension 
            PRIVATE
            ...
    )
    
    target_link_libraries( my_gdextension  PRIVATE <one of the godot-cpp targets t match your godot target> )
  6. enetheru commented on Mar 4, 2025

    @enetheru
    Collaborator

    Please close this as it is working as intended.

  7. Sailanarmo commented on Mar 4, 2025

    @Sailanarmo
    Author

    This is kind of where I disagree, and maybe this is me not understanding how the cpp bindings are supposed to work, but I have my CMake project set up in a way where I can point to modular builds of third party dependencies. In which this case, godot-cpp is a third party library with a static library provided.

    And since it provides a static library, then one should be safe to assume that it’s okay to link said library with the overall project.

    I do have a .gdextension which I can point to my shared object or DLL to a custom location in my build folder. And I’ve been able to load my shared library just fine and build against it.

    I still think if CMake is trying to achieve the same thing as scons then it should build 1 target by default.

  8. dsnopek commented on Mar 4, 2025

    @dsnopek
    Collaborator

    I still think if CMake is trying to achieve the same thing as scons then it should build 1 target by default.

    If there's no technical reason not to have a default target, we should probably have the same one as scons

    However, like I said above, I'm not sure it's the best default. If you're making an editor-only extension, then you won't want to make a template_debug build. But there really isn't a default that will work for all use cases

  9. Sailanarmo commented on Mar 4, 2025

    @Sailanarmo
    Author

    I still think if CMake is trying to achieve the same thing as scons then it should build 1 target by default.

    If there's no technical reason not to have a default target, we should probably have the same one as scons

    However, like I said above, I'm not sure it's the best default. If you're making an editor-only extension, then you won't want to make a template_debug build. But there really isn't a default that will work for all use cases

    Which I think can be solved in two ways:

    Either put it in the readme in this repository. Which would be the quickest and most versatile.

    Or have it build all targets. I’m more than happy to have a conversation about this or contribute in any way. You don’t have to listen to me though haha. I’m a CMake snob and purest and I actually put in a install function on my end, built it, ran it, and it worked great.

  10. dsnopek commented on Mar 4, 2025

    @dsnopek
    Collaborator

    Or have it build all targets.

    If we have a default, it shouldn't be different than the default used by scons, unless there's a special cmake-specific reason to do so, but it doesn't sound like there is in this case

  11. Sailanarmo commented on Mar 4, 2025

    @Sailanarmo
    Author

    If we have a default, it shouldn't be different than the default used by scons, unless there's a special cmake-specific reason to do so, but it doesn't sound like there is in this case

    Then I think if the behavior is to be the same as scons being executed then template_debug should be the way to go.

  12. enetheru commented on Mar 5, 2025

    @enetheru
    Collaborator

    There is a conflict here that I do not have a complete solution for.

    Currently all the targets are excluded from ALL on purpose because when godot is consumed by a project it isnt supposed to build anything that isnt directly asked for.

    Adding(or more specifically not excluding) template_debug to the default all target, means that it will build no matter if you are using it or not, which is undesirable.

    CMake 3.21 added PROJECT_IS_TOP_LEVEL, but we are on 3.17
    I can work around that by checking if PROJECT_NAME is set prior to the godot-cpp project call and setting a variable.
    then I can add template_debug as a target for ALL predicated on that result.

    However, it is impossible to create a 1:1 match to SCons because the CMake build type default is Debug, which is not fixable for multi-config generators like Ninja-Multi-Config, Visual Studio, and I believe XCode.

    No matter how its arranged input from the user is expected in that regard.

    Side note for @Sailanarmo why are you building godot-cpp separately and using find package, rather than either fetchconfig or include? It's not the kind of library that gets build separately from a project, or installed on a system.

  13. dsnopek commented on Mar 5, 2025

    @dsnopek
    Collaborator

    However, it is impossible to create a 1:1 match to SCons because the CMake build type default is Debug, which is not fixable for multi-config generators like Ninja-Multi-Config, Visual Studio, and I believe XCode.

    If there's CMake-specific technical issues with having a default, could we maybe instead give a warning if someone tries to build without giving a target?

  14. Sailanarmo commented on Mar 5, 2025

    @Sailanarmo
    Author

    Side note for @Sailanarmo why are you building godot-cpp separately and using find package, rather than either fetchconfig or include? It's not the kind of library that gets build separately from a project, or installed on a system.

    @enetheru I apologize if I come off as aggressive, or snarky, since that is not my intent. But it is the lack of clear documentation that leaves me to believe that if developer gives me the power to build something as a library, I am going to do it and link it into my project. I know that traditionally, this repository gets cloned down into the game project as a submodule and it is built into the project all together at once.

    However, it is my understanding, that a static library gets built and it is linked into the project. Now, if there is clear documentation that says this is not the intention of the repository and that all code MUST be compiled and linked. Then I would have followed that route. However, because I am so familiar with CMake I see the following lines in the code:

    Image

    This tells me that files are going to be built and populated. If I see something use FetchContent, that tells me that the project can be fetched, built, and linked into your project as a dependency. So if I see that, I will assume that I can build the package myself, deploy and installation, and then link to it whenever.

    This was further confirmed to me when I cloned down the repository, ran the build, and found static libraries populated. I also found targets.cmake files where it tells CMake, "Here is where the include files are, here is where the library is, and here is the godot-cpp:: namespace that you can append to it."

    So with that knowledge, I decided to add install macros here:

    Image

    I then tested it locally by doing, cmake -B build -S . -DCMAKE_INSTALL_PREFIX=install and then ran cmake --build build --target godot-cpp.template_debug and cmake --build build --target install and watched it populate all of the headers, cmake targets, cmake package configs, and the static library libgodot-cpp.linux.template_debug.x86_64.a. I then pointed my godot project to it by doing:

      find_package(godot-cpp.template_debug 4.4 EXACT REQUIRED HINTS /home/sailanarmo/godot-cpp/install/lib/cmake)

    I cleaned my build, watched it run, link, compile, I then opened up the Godot Editor to test if it was working and my class popped up in the editor exactly the same as I had done with fetch content. So that told me that this was a success and I am indeed able to create this as a modularized package that I don't have to recompile every time if I have to clean my build.

    So I hope that answers your question on to why I am doing it that way. The bigger answer to your question is, "If I can find a way to not pull in a dependency's source code into my build I am going to do everything in my power to do that." Why? Because I am not touching the thirdparty libraries code. I am modifying my own code and the only thing that should be built is my code. It saves on compile time dramatically.

    Again, if there is clear documentation that says that this repo must be built with the source code then I will not go down this route anymore. I am all about making libraries and working that way. If I notice that what I am building is a library I will build it into a library. My current CMake project looks like:

    Image

    Which then I can built my smaller libraries like this:

    Image

    Which then does this:

    Image

    Which then populates my binary that I can pull into godot via the .gdextension script:

    Image

    Which then happily loads in godot:

    Image

    Image

    So again, if I am doing this wrong then I can correct it. But I again implore that the value of documentation on how CMake, or this repo, is meant to be treated or built. I apologize for the long essay of an explanation. And again, I am not trying to be argumentative, snarky, rude, or arrogant. I am a very skilled C++ developer with a lot of knowledge on CMake and build architecture who is trying their hand in a new world of game development and has little to no understanding on how this project specifically is supposed to work.

  15. Sailanarmo commented on Mar 5, 2025

    @Sailanarmo
    Author

    If there's CMake-specific technical issues with having a default, could we maybe instead give a warning if someone tries to build without giving a target?

    I think this would be a perfectly valid solution!

  16. dsnopek commented on Mar 5, 2025

    @dsnopek
    Collaborator

    However, it is my understanding, that a static library gets built and it is linked into the project. Now, if there is clear documentation that says this is not the intention of the repository and that all code MUST be compiled and linked.

    There's a number of very commonly used features that aren't compatible with keeping a single godot-cpp static library and reusing it in all projects, namely:

    • Using a custom extension_api.json. We only bundle the latest stable JSON for a given branch. Commonly extension developers want to use either (1) an older one, like if we bundle 4.2.2 and they want to use 4.2.0 for compatibility with more Godot versions, or (2) one from a custom build of Godot where extra features were included (one that comes up a lot is building Godot with double precision), or certain features were compiled out
    • Using a build_profile.json to only build the godot-cpp classes that are actually used in a specific extension. This can dramatically reduce the binary size of a GDExtension, so many (most?) GDExtensions that are distributed publicly do this

    We probably could do a better job of documenting those features :-)

  17. Sailanarmo commented on Mar 5, 2025

    @Sailanarmo
    Author

    Using a build_profile.json to only build the godot-cpp classes that are actually used in a specific extension. This can dramatically reduce the binary size of a GDExtension, so many (most?) GDExtensions that are distributed publicly do this

    We probably could do a better job of documenting those features :-)

    Thank you for the response and I believe this clears up a lot of misunderstandings from my end. This would warrant a conversation outside of this issue but for now I believe I understand the point of this repository.

    I guess if I have a Class Foo, that does some fooey stuff. Then I need to build Foo with this repository and then import that extension(?) into my greater game project?

    And if I have a Class Bar, that does some barey stuff, I will need to build Bar with this repository and do the same thing that I did with Foo?

    I just want to be clear that is the intended behavior? I'm not even sure that I made myself clear with this haha.

  18. dsnopek commented on Mar 5, 2025

    @dsnopek
    Collaborator

    I'm not entirely sure I understand your example, but godot-cpp generates code for a lot of classes (953 in a quick ls of the gen/src/classes directory). If your extension only uses, for example, 10 of those, you can provide a build_profile.json that will build godot-cpp with only those 10 classes, which will result in your GDExtension binary being dramatically smaller than if you used a build of godot-cpp with all the classes.

  19. Sailanarmo commented on Mar 5, 2025

    @Sailanarmo
    Author

    I'm not entirely sure I understand your example, but godot-cpp generates code for a lot of classes (953 in a quick ls of the gen/src/classes directory). If your extension only uses, for example, 10 of those, you can provide a build_profile.json that will build godot-cpp with only those 10 classes, which will result in your GDExtension binary being dramatically smaller than if you used a build of godot-cpp with all the classes.

    That actually makes a ton of sense! You actually answered the question on why creating a default could be difficult. Since the default would build all the classes and create one static library from that.

    The example I gave was poorly structured as I typed it out. So I will try again.

    I have an abstract class in which I created a concrete child class from. We will call it Foo. Foo inherits from the Abstract Foo, and the Abstract Foo inherits from a Character2D object. As of right now, I can register both oh them with the register types and produce the DLL to load in.

    I was asking if I want to create another class Bar. That is not abstract, that I eventually want to use with Foo. But I want to test the behavior and functionality before I give it to Foo. So I assumed I could build Bar into the same DLL to use. But perhaps that isn't the case? Am I supposed to build Bar outside of Foo, with it's own cpp-godot repo?

    Essentially is it supposed to look like:

    Game Project
    -> bin/foo.dll, bar.dll
    -> foo.gdextension
    -> bar.gdextension

    Foo
    -> godot-cpp
    -> build/foo.dll
    -> CMakeLists.txt
    -> Souce Code for Foo

    Bar
    -> godot-cpp
    -> build/bar.dll
    -> CMakeLists.txt
    -> Source Code for Bar

  20. dsnopek commented on Mar 5, 2025

    @dsnopek
    Collaborator

    So I assumed I could build Bar into the same DLL to use. But perhaps that isn't the case? Am I supposed to build Bar outside of Foo, with it's own cpp-godot repo?

    No, you absolutely can build both classes into the same GDExtension using the same build of godot-cpp.

    However, your GDExtension is unlikely to need to use all 900+ classes that godot-cpp can generate code for, so if you only need 2 Godot classes from godot-cpp, you can have it only build those 2. The idea is that each GDExtension can (and probably should) only build the godot-cpp classes it needs for any build that gets distributed. (In development, you may want to build with all classes, but that's up to the developer.)

  21. Sailanarmo commented on Mar 5, 2025

    @Sailanarmo
    Author

    @dsnopek Thanks for the response. One final off topic question then. So I am encountering an issue where my class Bar cannot link against a class it has been compiled against. Call it Fuzz.

    So:

      #pragma once 
    
      #include <memory>
      #include <godot_cpp/classes/object.hpp>
    
      class Fuzz;
      class Bar : public godot::Object
      { 
        GDCLASS(Bar, godot::Object);
    
        public:
          static void _bind_methods();
    
        private:
          std::shared_ptr<Fuzz> m_fuzz;
      };

    I've registered it in the register_types.cpp file, everything compiles fine and seems to link fine in the binary. But when it tries to load through Godot it gets angry and says it has an undefined symbol to one of the functions in Fuzz. Now, Fuzz doesn't rely on Godot at all. It's bare bones C++ stuff. Honestly, it's just a class that has an Id, which is an integer, and a name, which is a std::string. With getters and setters to it.

    Is the reason that it is having issues with Fuzz is because Fuzz does not inherit from a godot::Object or godot::Ref?

  22. enetheru commented on Mar 6, 2025

    @enetheru
    Collaborator

    merged #1728 which solves the primary issue here.

  23. enetheru commented on Mar 14, 2025

    @enetheru
    Collaborator

    even better solution merged, should #1733, thanks for your feedback.

    Is the reason that it is having issues with Fuzz is because Fuzz does not inherit from a godot::Object or godot::Ref?

    If you are still having issues with this can you submit a new bug, or discuss in forums or discord please.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    cmakeenhancementtopic:buildsystem

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions