cancel
Showing results for 
Search instead for 
Did you mean: 

CMake project debugging C++ problem

Peterko
Associate

I'm wondering with implementation of CMake into stm cube.

I tried default project executable and it works for C and I'm able to debug it.

But if I change it to C++ I'm unable to debug it. Program always jumps to this line:

Infinite_Loop:

b Infinite_Loop

.size Default_Handler, .-Default_Handler

At first I separate the sources and asm file by

set (PROJECT_ASM_SOURCES

# LIST SOURCE FILES HERE

Startup/startup_stm32h745zitx.s

)

set (PROJECT_SOURCES

# LIST SOURCE FILES HERE

Sources/main.c

Sources/syscalls.c

Sources/sysmem.c

)

then I set c files to c++ by:
set_source_files_properties(${PROJECT_SOURCES} PROPERTIES LANGUAGE CXX)

and added these file to executable:

add_executable(${PROJECT_NAME} ${PROJECT_SOURCES} ${PROJECT_ASM_SOURCES})

I used NUCLEO-H745ZI-Q board. I tried several compiler settings, linker, rename files to cpp, but with no success.


1 ACCEPTED SOLUTION

Accepted Solutions
Peterko
Associate

Thanks, yes it makes sense. So I separated asm c and cpp files, that I create one cpp file with my cpp function. I'm still unable to debug it, the same error apears. I'm assuming that the correct flags is necessarry to set to the linker and compiler. I have huge project managed by cmake, so I wanted to use it. I solved it as follows:
1. I generated template CMake library project in stmcube.

2. I updated CMakeLists.txt accordingly to my project structure, generate and build library.

3. I created simple Stm32 C++ project.

4. In this project I prepared cpp file with entrypoint function to my library, don't forget to add into header: 

 

 

#ifdef __cplusplus

extern "C" {

#endif

void MyCppFunc(void);

#ifdef __cplusplus

}

#endif

 

5. I added this library and path into linker project settings.

Now it works as expected.
This solution looks better for me, because I'm able to use Pinout & Configuration wizard also.

View solution in original post

2 REPLIES 2
TDK
Guru

HAL and the rest of the STM32 code architecture is written in C and expected to be compiled in C (and a small bit in assembly). I would avoid forcing *.c files to be compiled as C++ as that will cause all sorts of these issues.

If it jumps to Default_Handler, it's probably due to an IRQ handler not being implemented. When you switch from *.c to *.cpp, you need to retain C linkage for IRQ handlers and possibly other things that are shared with C.

There is no problem with having C files and C++ files within the same project. You shouldn't need to switch everything to C++.

If you feel a post has answered your question, please click "Accept as Solution".
Peterko
Associate

Thanks, yes it makes sense. So I separated asm c and cpp files, that I create one cpp file with my cpp function. I'm still unable to debug it, the same error apears. I'm assuming that the correct flags is necessarry to set to the linker and compiler. I have huge project managed by cmake, so I wanted to use it. I solved it as follows:
1. I generated template CMake library project in stmcube.

2. I updated CMakeLists.txt accordingly to my project structure, generate and build library.

3. I created simple Stm32 C++ project.

4. In this project I prepared cpp file with entrypoint function to my library, don't forget to add into header: 

 

 

#ifdef __cplusplus

extern "C" {

#endif

void MyCppFunc(void);

#ifdef __cplusplus

}

#endif

 

5. I added this library and path into linker project settings.

Now it works as expected.
This solution looks better for me, because I'm able to use Pinout & Configuration wizard also.