Skip to content

Commit 778fa86

Browse files
committed
Fix "undefined symbol: operator new"
Clang's default C++ standard is now gnu++17 instead of gnu++14. Supporting C++17 requires aligned new & delete. Implementing this requires posix_memalign. Picolibc only provides posix_memalign if the correct feature test macros are set. https://www.gnu.org/software/libc/manual/html_node/Feature-Test-Macros.html
1 parent aace7b8 commit 778fa86

File tree

2 files changed

+9
-7
lines changed

2 files changed

+9
-7
lines changed

CMakeLists.txt

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -418,7 +418,10 @@ while(library_variants)
418418

419419
# compiler-rt
420420

421-
set(runtimes_flags "${common_flags} -ffunction-sections -fdata-sections -fno-ident --sysroot ${sysroot}")
421+
# Defining _DEFAULT_SOURCE means that extensions like posix_memalign
422+
# can be used by the C++ runtimes. This is required to implement
423+
# C++17's aligned new & delete operators.
424+
set(runtimes_flags "${common_flags} -ffunction-sections -fdata-sections -fno-ident --sysroot ${sysroot} -D_DEFAULT_SOURCE")
422425

423426
ExternalProject_Add(
424427
compiler_rt_${variant}
@@ -476,7 +479,6 @@ while(library_variants)
476479

477480
# Other LLVM runtimes
478481

479-
set(runtimes_cxx_flags "${runtimes_flags} -D_LIBCPP_HAS_NO_LIBRARY_ALIGNED_ALLOCATION")
480482
ExternalProject_Add(
481483
runtimes_${variant}
482484
SOURCE_DIR ${llvmproject_SOURCE_DIR}/runtimes
@@ -489,7 +491,7 @@ while(library_variants)
489491
-DCMAKE_BUILD_TYPE=MinSizeRel
490492
-DCMAKE_CXX_COMPILER=${llvm_bin}/clang++
491493
-DCMAKE_CXX_COMPILER_TARGET=${target}
492-
-DCMAKE_CXX_FLAGS=${runtimes_cxx_flags}
494+
-DCMAKE_CXX_FLAGS=${runtimes_flags}
493495
-DCMAKE_C_COMPILER=${llvm_bin}/clang
494496
-DCMAKE_C_COMPILER_TARGET=${target}
495497
-DCMAKE_C_FLAGS=${runtimes_flags}

scripts/make.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -372,11 +372,11 @@ def build_cxx_libraries(self, lib_spec: config.LibrarySpec) -> None:
372372
Build and install a single variant of lib++abi, libc++ & libunwind
373373
"""
374374
cmake_defs = self._get_common_cmake_defs_for_libs(lib_spec)
375-
# Disable C++17 aligned allocation feature because its implementation
376-
# in libc++ relies on posix_memalign() which is not available in our
377-
# picolibc build
375+
# Defining _DEFAULT_SOURCE means that extensions like
376+
# posix_memalign can be used by the C++ runtimes. This is
377+
# required to implement C++17's aligned new & delete operators.
378378
cxx_flags = (cmake_defs.get('CMAKE_CXX_FLAGS', '')
379-
+ ' -D_LIBCPP_HAS_NO_LIBRARY_ALIGNED_ALLOCATION')
379+
+ ' -D_DEFAULT_SOURCE')
380380
install_dir = os.path.join(self.cfg.target_llvm_rt_dir,
381381
lib_spec.name)
382382
cmake_defs.update({

0 commit comments

Comments
 (0)