How can I use the boost library with the Scons file from the GDextension Demo?

I followed the GD extension tutorial. It worked, but I wanted to use some functions from the boost library. I added boost as a submodule to my project inside the addons folder.

git submodule add --recurse-submodules https://github.com/boostorg/boost.git addons/boost

I needed to add the following code to my Scons file.

# Allow exception handling
env.Append(CCFLAGS=['-fexceptions'])

# Function to find all Boost include paths
def find_boost_include_paths(base_path):
    include_paths = []
    for root, dirs, files in os.walk(base_path):
        if 'include' in dirs:
            include_paths.append(os.path.join(root, 'include'))
    return include_paths

# Base path for Boost headers
boost_base_dir = "addons/boost/libs"

# Find and add all Boost include paths
boost_include_paths = find_boost_include_paths(boost_base_dir)
env.Append(CPPPATH=boost_include_paths)

My file structure was something like this after using boost as a git submodule. So a single rule wouldn’t be sufficient for boost.

addons/
  boost/
    libs/
      config/
        include/
          boost/
            config.hpp
      lambda/
        include/
          boost/
            lambda/
              lambda.hpp
      type_traits/
        include/
          boost/
            type_traits/
              transform_traits.hpp
      # Other necessary components

EDIT 1: Updated the Sconstruct file based on some problems I had further down the line.

1 Like

I wanted to do a similar thing but with boost::asio to make an UDP server. I didn’t wanted to clone boost repo, but even after that - the OP’s solution didn’t work for me as I had errors in Godot editor: undefined symbol: _ZN5boost15throw_exceptionERKSt9exceptionRKNS_15source_locationE

I managed to fix this by using system provided boost and adding the following lines to SConstruct file:

env.Append(CXXFLAGS=["-fexceptions"])
env.Append(LIBPATH=["/usr/lib"])
env.Append(LIBS=["boost_thread", "boost_coroutine", "boost_regex", "boost_system"])
1 Like

Hey, I had those problems too some time later. I was originally just using functions in the boost graph library and my coding and test loop was fine. But when I tried to use some other library, compile failed again with include not found. The change was to check if ‘include’ is in dirs and then add header files. I also had the same fexception error you did some hours later, but that was much harder to debug. I’ve added in my updated Sconstruct file in the answer post, with a note mentioning you.