win10 qemu-vexpress-a9 环境下面,局部变量的c++能正使用,但全局变量的那些类,构造函数没有调用。cplusplussysteminit函数有调用,ctors_start和__ctors_end地址一样。
RT_WEAK int cplusplus_system_init(void)
{
#if defined(__CC_ARM) || defined(__CLANG_ARM)
/* If there is no SHT$$INIT_ARRAY, calling
* $Super$$__cpp_initialize__aeabi_() will cause fault. At least until Keil5.12
* the problem still exists. So we have to initialize the C++ runtime by ourself.
*/
typedef void PROC();
extern const unsigned long SHT$$INIT_ARRAY$$Base[];
extern const unsigned long SHT$$INIT_ARRAY$$Limit[];
const unsigned long *base = SHT$$INIT_ARRAY$$Base;
const unsigned long *lim = SHT$$INIT_ARRAY$$Limit;
for (; base != lim; base++)
{
PROC *proc = (PROC *)((const char *)base + *base);
(*proc)();
}
#elif defined(__GNUC__)
typedef void(*pfunc)();
extern pfunc __ctors_start__[];
extern pfunc __ctors_end__[];
pfunc *p;
for (p = __ctors_start__; p < __ctors_end__; p++)
(*p)();
#endif
return 0;
}
main.c
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include "test.h"
int main(void)
{
printf("test: %d\n",test_b());
return 0;
}
test.h
int test_b(void);
test.cpp
#include <stdio.h>
class B{
public:
B(){
printf("B::B()\n");
b = 5;
}
~B(){}
int b;
};
B b;
extern "C" int test_b(void){
return b.b;
}
结果
PS H:\work\qemu\rt-thread\rt-thread\bsp\qemu-vexpress-a9> .\qemu.bat
WARNING: Image format was not specified for 'sd.bin' and probing guessed raw.
Automatically detecting the format is dangerous for raw images, write operations on block 0 will be restricted.
Specify the 'raw' format explicitly to remove the restrictions.
dsound: Could not initialize DirectSoundCapture
dsound: Reason: No sound driver is available for use, or the given GUID is not a valid DirectSound device ID
\ | /
- RT - Thread Operating System
/ | \ 4.0.3 build Apr 29 2021
2006 - 2021 Copyright by rt-thread team
lwIP-2.0.2 initialized!
[32m[I/sal.skt] Socket Abstraction Layer initialize success.
m[I/SDIO] SD card capacity 65536 KB.
[I/SDIO] switching card to hig h speed failed!
file system initialization done!
test: 0
msh />
已经看过基本一致差异点如下,改成一样后程序都不能运行
stm32
qemu-vexpress-a9
建议你就先使用官网中的C++的例子验证不好吗?
哪里有示例代码?@flashman2002
@ckk
我在实际应用中也遇到同样全局对象未调用构造器的问题,请问楼主解决了没有