Skip to content

Commit eb36177

Browse files
committed
[libc] Fix warning on 'extern "C" int main' in test suite
Summary: According to the C++ standard, The main function shall not be declared with a linkage-specification. after some changes in #101853 this started emitting warnings when building / testing the C library. This source file is shared with the overlay tests as well as the full build tests. The full build tests are compiled with `-ffreestanding`, as are all the startup / integration files. The standard says freestanding environment are all implementation defined, so this is valid in those cases. This patch simply prevents adding the linkage when we are compiling unit tests, which are hosted. This is a continuation on #102825.
1 parent ee5d572 commit eb36177

File tree

2 files changed

+10
-1
lines changed

2 files changed

+10
-1
lines changed

libc/test/UnitTest/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ function(add_unittest_framework_library name)
3232
_get_hermetic_test_compile_options(compile_options -nostdinc++)
3333
target_include_directories(${name}.hermetic PRIVATE ${LIBC_BUILD_DIR}/include)
3434
target_compile_options(${name}.hermetic PRIVATE ${compile_options})
35+
target_compile_definitions(${name}.hermetic PRIVATE LIBC_HERMETIC_TEST)
3536

3637
if(TEST_LIB_DEPENDS)
3738
foreach(dep IN ITEMS ${TEST_LIB_DEPENDS})

libc/test/UnitTest/LibcTestMain.cpp

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,15 @@ TestOptions parseOptions(int argc, char **argv) {
4343

4444
} // anonymous namespace
4545

46-
extern "C" int main(int argc, char **argv, char **envp) {
46+
// The C++ standard forbids declaring the main function with a linkage specifier
47+
// outisde of 'freestanding' mode, only define the linkage for hermetic tests.
48+
#ifdef LIBC_HERMETIC_TEST
49+
#define TEST_MAIN extern "C" int main
50+
#else
51+
#define TEST_MAIN int main
52+
#endif
53+
54+
TEST_MAIN(int argc, char **argv, char **envp) {
4755
LIBC_NAMESPACE::testing::argc = argc;
4856
LIBC_NAMESPACE::testing::argv = argv;
4957
LIBC_NAMESPACE::testing::envp = envp;

0 commit comments

Comments
 (0)