Skip to content

Instantly share code, notes, and snippets.

@amachronic
Last active October 14, 2022 15:34
Show Gist options
  • Save amachronic/31222be96330f85362751c3ee844b23b to your computer and use it in GitHub Desktop.
Save amachronic/31222be96330f85362751c3ee844b23b to your computer and use it in GitHub Desktop.

Building GCC with a freestanding libstdc++

Works with GCC 12, older versions have bugs in the configure script which prevent building libstdc++ directly. It might be possible to work around that by building GCC and then cross-compiling libstdc++ separately...?

Environment variables

export TARGET=mipsel-elf
export PREFIX=/opt/rbdev-gcc12-cxx
export CFLAGS='-U_FORTIFY_SOURCE -fgnu89-inline -O2'
export CXXFLAGS='-std=gnu++11'

Build binutils

../binutils-2.39/configure --prefix=$PREFIX --target=$TARGET \
        --disable-docs --disable-werror
make -j8
make install

Build GCC

TODO: Some of these options could be redundant...

../gcc-12.2.0/configure --target=$TARGET --prefix=$PREFIX \
        --disable-docs --enable-languages=c,c++ --disable-werror \
        --disable-threads --disable-shared --disable-libssp \
        --disable-dlopen --disable-libgomp --disable-libstdcxx-pch \
        --without-headers --with-newlib --disable-nls --disable-bootstrap \
        --disable-hosted-libstdcxx
make -j8
make install

C++ headers

At the end, the resulting toolchain only has a minimal set of C++ headers:

atomic
bit
cfloat
ciso646
climits
compare
concepts
coroutine
cstdalign
cstdarg
cstdbool
cstddef
cstdint
cstdlib
cxxabi.h
exception
initializer_list
limits
new
source_location
type_traits
typeinfo
version
diff --git a/tools/root.make b/tools/root.make
index 03c4d48986..09289b453a 100644
--- a/tools/root.make
+++ b/tools/root.make
@@ -193,6 +193,7 @@ ifdef CORE_GCSECTIONS
endif # CORE_GCSECTIONS
OBJ := $(SRC:.c=.o)
+OBJ := $(OBJ:.cpp=.o)
OBJ := $(OBJ:.S=.o)
OBJ += $(BMP:.bmp=.o)
OBJ := $(call full_path_subst,$(ROOTDIR)/%,$(BUILDDIR)/%,$(OBJ))
@@ -439,6 +440,10 @@ $(BUILDDIR)/%.o: $(ROOTDIR)/%.c
$(SILENT)mkdir -p $(dir $@)
$(call PRINTS,CC $(subst $(ROOTDIR)/,,$<))$(CC) $(CFLAGS) -c $< -o $@
+$(BUILDDIR)/%.o: $(ROOTDIR)/%.cpp
+ $(SILENT)mkdir -p $(dir $@)
+ $(call PRINTS,CXX $(subst $(ROOTDIR)/,,$<))mipsel-elf-g++ $(CFLAGS) -c $< -o $@
+
$(BUILDDIR)/%.o: $(ROOTDIR)/%.S
$(SILENT)mkdir -p $(dir $@)
$(call PRINTS,CC $(subst $(ROOTDIR)/,,$<))$(CC) $(CFLAGS) -c $< -o $@
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment