Skip to content

Conversation

@leducp
Copy link
Contributor

@leducp leducp commented Sep 3, 2025

Summary

Update the CMake C++ guide that is too outdated to be used.

Testing

Ongoing

close #16782

@github-actions github-actions bot added Area: Documentation Improvements or additions to documentation Size: M The size of the change in this PR is medium labels Sep 3, 2025
@leducp
Copy link
Contributor Author

leducp commented Sep 3, 2025

ping @trns1997

@trns1997
Copy link
Contributor

trns1997 commented Sep 3, 2025

I will follow the procedure step by step and get back :)

@trns1997
Copy link
Contributor

trns1997 commented Sep 4, 2025

@leducp since nuttx-12.10.0 i get the following error when trying to build the app:

➜  build make
[ 33%] Building CXX object src/CMakeFiles/hello.dir/HelloWorld.cpp.obj
[ 66%] Building CXX object src/CMakeFiles/hello.dir/main.cpp.obj
[100%] Linking CXX executable hello
/home/thomas/arm-gnu-toolchain-13.2.Rel1-x86_64-arm-none-eabi/bin/arm-none-eabi-ld: /home/thomas/nuttxspace/hellocpp/nuttx-export-12.10.0/scripts/../libs/libarch.a(stm32_start.o): in function `__start':
stm32_start.c:(.text.__start+0x0): multiple definition of `__start'; /home/thomas/nuttxspace/hellocpp/nuttx-export-12.10.0/scripts/../startup/crt0.o:crt0.c:(.text.__start+0x0): first defined here
/home/thomas/arm-gnu-toolchain-13.2.Rel1-x86_64-arm-none-eabi/bin/arm-none-eabi-ld: warning: hello has a LOAD segment with RWX permissions
/home/thomas/arm-gnu-toolchain-13.2.Rel1-x86_64-arm-none-eabi/bin/arm-none-eabi-ld: /home/thomas/nuttxspace/hellocpp/nuttx-export-12.10.0/scripts/../startup/crt0.o: in function `__start':
crt0.c:(.text.__start+0x38): undefined reference to `_sctors'
/home/thomas/arm-gnu-toolchain-13.2.Rel1-x86_64-arm-none-eabi/bin/arm-none-eabi-ld: crt0.c:(.text.__start+0x3c): undefined reference to `_ectors'
/home/thomas/arm-gnu-toolchain-13.2.Rel1-x86_64-arm-none-eabi/bin/arm-none-eabi-ld: crt0.c:(.text.__start+0x40): undefined reference to `main'
/home/thomas/arm-gnu-toolchain-13.2.Rel1-x86_64-arm-none-eabi/bin/arm-none-eabi-ld: crt0.c:(.text.__start+0x44): undefined reference to `_sdtors'
/home/thomas/arm-gnu-toolchain-13.2.Rel1-x86_64-arm-none-eabi/bin/arm-none-eabi-ld: crt0.c:(.text.__start+0x48): undefined reference to `_edtors'
Memory region         Used Size  Region Size  %age Used
           flash:       19304 B         1 MB      1.84%
            sram:        1704 B       112 KB      1.49%
make[2]: *** [src/CMakeFiles/hello.dir/build.make:113: src/hello] Error 1
make[1]: *** [CMakeFiles/Makefile2:98: src/CMakeFiles/hello.dir/all] Error 2
make: *** [Makefile:91: all] Error 2

some linker definitions are missing in the recent exports. I think we need to resolve this issue before coming back to this

linguini1
linguini1 previously approved these changes Sep 4, 2025
@leducp
Copy link
Contributor Author

leducp commented Sep 5, 2025

@leducp since nuttx-12.10.0 i get the following error when trying to build the app:

➜  build make
[ 33%] Building CXX object src/CMakeFiles/hello.dir/HelloWorld.cpp.obj
[ 66%] Building CXX object src/CMakeFiles/hello.dir/main.cpp.obj
[100%] Linking CXX executable hello
/home/thomas/arm-gnu-toolchain-13.2.Rel1-x86_64-arm-none-eabi/bin/arm-none-eabi-ld: /home/thomas/nuttxspace/hellocpp/nuttx-export-12.10.0/scripts/../libs/libarch.a(stm32_start.o): in function `__start':
stm32_start.c:(.text.__start+0x0): multiple definition of `__start'; /home/thomas/nuttxspace/hellocpp/nuttx-export-12.10.0/scripts/../startup/crt0.o:crt0.c:(.text.__start+0x0): first defined here
/home/thomas/arm-gnu-toolchain-13.2.Rel1-x86_64-arm-none-eabi/bin/arm-none-eabi-ld: warning: hello has a LOAD segment with RWX permissions
/home/thomas/arm-gnu-toolchain-13.2.Rel1-x86_64-arm-none-eabi/bin/arm-none-eabi-ld: /home/thomas/nuttxspace/hellocpp/nuttx-export-12.10.0/scripts/../startup/crt0.o: in function `__start':
crt0.c:(.text.__start+0x38): undefined reference to `_sctors'
/home/thomas/arm-gnu-toolchain-13.2.Rel1-x86_64-arm-none-eabi/bin/arm-none-eabi-ld: crt0.c:(.text.__start+0x3c): undefined reference to `_ectors'
/home/thomas/arm-gnu-toolchain-13.2.Rel1-x86_64-arm-none-eabi/bin/arm-none-eabi-ld: crt0.c:(.text.__start+0x40): undefined reference to `main'
/home/thomas/arm-gnu-toolchain-13.2.Rel1-x86_64-arm-none-eabi/bin/arm-none-eabi-ld: crt0.c:(.text.__start+0x44): undefined reference to `_sdtors'
/home/thomas/arm-gnu-toolchain-13.2.Rel1-x86_64-arm-none-eabi/bin/arm-none-eabi-ld: crt0.c:(.text.__start+0x48): undefined reference to `_edtors'
Memory region         Used Size  Region Size  %age Used
           flash:       19304 B         1 MB      1.84%
            sram:        1704 B       112 KB      1.49%
make[2]: *** [src/CMakeFiles/hello.dir/build.make:113: src/hello] Error 1
make[1]: *** [CMakeFiles/Makefile2:98: src/CMakeFiles/hello.dir/all] Error 2
make: *** [Makefile:91: all] Error 2

some linker definitions are missing in the recent exports. I think we need to resolve this issue before coming back to this

Indeed... 😥

@leducp
Copy link
Contributor Author

leducp commented Sep 5, 2025

It seems the build is broken since this commit: 422c439

@trns1997 can you cross check on your side?

@trns1997
Copy link
Contributor

trns1997 commented Sep 5, 2025

It seems the build is broken since this commit: 422c439

@trns1997 can you cross check on your side?

@leducp , here #16976 (comment) i will resolve this in this PR.

@trns1997
Copy link
Contributor

trns1997 commented Sep 5, 2025

@leducp if you use this toolchain.cmake the app should build:

set(CMAKE_SYSTEM_NAME Generic)
set(CMAKE_SYSTEM_VERSION 1)
set(NUTTX 1)

set(CMAKE_TRY_COMPILE_TARGET_TYPE STATIC_LIBRARY)

set(NUTTX_PATH ${CMAKE_CURRENT_LIST_DIR}/..)
include(${NUTTX_PATH}/scripts/target.cmake)

set(LINKER_SCRIPT ${NUTTX_PATH}/scripts/${LDNAME})

set(CMAKE_C_FLAGS "${ARCHCPUFLAGS} ${ARCHCFLAGS} -D__NuttX__")
set(CMAKE_CXX_FLAGS "${ARCHCPUFLAGS} ${ARCHCXXFLAGS} -D__NuttX__")

set(CMAKE_C_STANDARD_INCLUDE_DIRECTORIES ${NUTTX_PATH}/include
                                         ${NUTTX_PATH}/arch/chip)

set(CMAKE_CXX_STANDARD_INCLUDE_DIRECTORIES
    ${NUTTX_PATH}/include/${NUTTX_CXX} ${NUTTX_PATH}/include
    ${NUTTX_PATH}/arch/chip)

# Use only crt0.o, not every .o in startup/
set(STARTUP_OBJ ${NUTTX_PATH}/startup/crt0.o)

add_compile_options(-nostdlib)
add_compile_options(-ffunction-sections -fdata-sections)

# same entry used for all build modes in crt0.c and arch/.../xxx_start.c

set(ENTRY_NAME "__start")

set(CMAKE_C_LINK_EXECUTABLE
    "<CMAKE_LINKER> ${LDFLAGS} --entry=${ENTRY_NAME} -T${LINKER_SCRIPT} <OBJECTS> ${STARTUP_OBJS} -o <TARGET> <LINK_LIBRARIES> -L${NUTTX_PATH}/libs --start-group ${LDLIBS} ${EXTRA_LIBS} --end-group"
)
set(CMAKE_CXX_LINK_EXECUTABLE
    "<CMAKE_LINKER> ${LDFLAGS} --entry=${ENTRY_NAME} -T${LINKER_SCRIPT} <OBJECTS> ${STARTUP_OBJS} -o <TARGET> <LINK_LIBRARIES> -L${NUTTX_PATH}/libs --start-group ${LDLIBS} ${EXTRA_LIBS} --end-group"
)

set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)
set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)
set(CMAKE_FIND_ROOT_PATH_MODE_PACKAGE ONLY)

relates to this comment #16976 (comment)

@trns1997
Copy link
Contributor

@leducp if you use this toolchain.cmake the app should build:

set(CMAKE_SYSTEM_NAME Generic)
set(CMAKE_SYSTEM_VERSION 1)
set(NUTTX 1)

set(CMAKE_TRY_COMPILE_TARGET_TYPE STATIC_LIBRARY)

set(NUTTX_PATH ${CMAKE_CURRENT_LIST_DIR}/..)
include(${NUTTX_PATH}/scripts/target.cmake)

set(LINKER_SCRIPT ${NUTTX_PATH}/scripts/${LDNAME})

set(CMAKE_C_FLAGS "${ARCHCPUFLAGS} ${ARCHCFLAGS} -D__NuttX__")
set(CMAKE_CXX_FLAGS "${ARCHCPUFLAGS} ${ARCHCXXFLAGS} -D__NuttX__")

set(CMAKE_C_STANDARD_INCLUDE_DIRECTORIES ${NUTTX_PATH}/include
                                         ${NUTTX_PATH}/arch/chip)

set(CMAKE_CXX_STANDARD_INCLUDE_DIRECTORIES
    ${NUTTX_PATH}/include/${NUTTX_CXX} ${NUTTX_PATH}/include
    ${NUTTX_PATH}/arch/chip)

# Use only crt0.o, not every .o in startup/
set(STARTUP_OBJ ${NUTTX_PATH}/startup/crt0.o)

add_compile_options(-nostdlib)
add_compile_options(-ffunction-sections -fdata-sections)

# same entry used for all build modes in crt0.c and arch/.../xxx_start.c

set(ENTRY_NAME "__start")

set(CMAKE_C_LINK_EXECUTABLE
    "<CMAKE_LINKER> ${LDFLAGS} --entry=${ENTRY_NAME} -T${LINKER_SCRIPT} <OBJECTS> ${STARTUP_OBJS} -o <TARGET> <LINK_LIBRARIES> -L${NUTTX_PATH}/libs --start-group ${LDLIBS} ${EXTRA_LIBS} --end-group"
)
set(CMAKE_CXX_LINK_EXECUTABLE
    "<CMAKE_LINKER> ${LDFLAGS} --entry=${ENTRY_NAME} -T${LINKER_SCRIPT} <OBJECTS> ${STARTUP_OBJS} -o <TARGET> <LINK_LIBRARIES> -L${NUTTX_PATH}/libs --start-group ${LDLIBS} ${EXTRA_LIBS} --end-group"
)

set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)
set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)
set(CMAKE_FIND_ROOT_PATH_MODE_PACKAGE ONLY)

relates to this comment #16976 (comment)

@leducp i can confirm it works :) here is the output:

Constructor: mSecret=42
HelloWorld: mSecret=42
TEST=Hello
CHelloWorld: HelloWorld: Hello, world!
Constructor: mSecret=42
HelloWorld: mSecret=42
TEST=Hello
CHelloWorld: HelloWorld: Hello, world!

@trns1997
Copy link
Contributor

Honestly the STARTUP_OBJS does not seem necessary it seems to compile without

@leducp
Copy link
Contributor Author

leducp commented Sep 11, 2025

Honestly the STARTUP_OBJS does not seem necessary it seems to compile without

OK so the exported toolchain was broken as suspected: waiting for #16976 before updating the doc again (and taking into account the comments).

@trns1997
Copy link
Contributor

@leducp cherry-pick this commit plz: trns1997@ecd91aa, i tested this new procedure with these updates and everything works fine :)

@trns1997
Copy link
Contributor

ping @leducp, #16976 has been merged :)

@leducp
Copy link
Contributor Author

leducp commented Sep 15, 2025

@trns1997 Doc updated with tree simplification in mind. I tested on xmc4800-relax on the latest commit (71f5587) and everything run fine: good job!

@leducp leducp marked this pull request as ready for review September 15, 2025 08:43
Use the exported CMake toolchain file instead of a  custom and broken one.
Slightly modify the C++ example code to introduce modern tools like auto keyword and shared_ptr

Signed-off-by: Philippe Leduc <philippe.leduc@mailfence.com>
@acassis acassis merged commit 1daa2d5 into apache:master Sep 15, 2025
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Area: Documentation Improvements or additions to documentation Size: M The size of the change in this PR is medium

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG] Feedback on C++/CMake Guide – Outdated Folder Structure and Build Issues

5 participants