如何在CoIDE中使用C.docx
《如何在CoIDE中使用C.docx》由会员分享,可在线阅读,更多相关《如何在CoIDE中使用C.docx(6页珍藏版)》请在冰点文库上搜索。
![如何在CoIDE中使用C.docx](https://file1.bingdoc.com/fileroot1/2023-5/26/444ca227-4a4f-415a-803d-0358c0e758f0/444ca227-4a4f-415a-803d-0358c0e758f01.gif)
如何在CoIDE中使用C
如何在CoIDE中使用C++
CooCox论坛上时不时有人问:
CoIDE是否支持C++?
CoIDE默认支持C语言,工程中的*.cpp或*.C(大写的C,会默认为cpp文件)不参与编译。
这让不少惯用C++的用户觉得可惜。
好在这个世界上解决问题的方法永远比问题多得多,感谢用户YuryKuchura的贡献和分享,现在只需要几处改动,CoIDE中就能使用C++了。
步骤如下:
1.修改build.xml文件,使*.cpp文件能被编译
""2.修改link.ld文件,使连接时C++所需的段能被识别
/*SectionDefinitions*/
SECTIONS
{
.text:
{
KEEP(*(.isr_vector.isr_vector.*))
*(.text.text.*.gnu.linkonce.t.*)
*(.glue_7t)*(.glue_7)
*(.rodata.rodata*.gnu.linkonce.r.*)
/*C++Staticconstructors/destructors(eabi)*/
.=ALIGN(4);
KEEP(*(.init))
.=ALIGN(4);
__preinit_array_start=.;
KEEP(*(.preinit_array))
__preinit_array_end=.;
.=ALIGN(4);
__init_array_start=.;
KEEP(*(SORT(.init_array.*)))
KEEP(*(.init_array))
__init_array_end=.;
.=ALIGN(4);
KEEP(*(.fini))
.=ALIGN(4);
__fini_array_start=.;
KEEP(*(.fini_array))
KEEP(*(SORT(.fini_array.*)))
__fini_array_end=.;
/*C++Staticconstructors/destructors(elf)*/
.=ALIGN(4);
_ctor_start=.;
KEEP(*crtbegin.o(.ctors))
KEEP(*(EXCLUDE_FILE(*crtend.o).ctors))
KEEP(*(SORT(.ctors.*)))
KEEP(*crtend.o(.ctors))
_ctor_end=.;
.=ALIGN(4);
KEEP(*crtbegin.o(.dtors))
KEEP(*(EXCLUDE_FILE(*crtend.o).dtors))
KEEP(*(SORT(.dtors.*)))
KEEP(*crtend.o(.dtors))
}>rom
.ARM.extab:
{
*(.ARM.extab*.gnu.linkonce.armextab.*)
}>rom
…
…
/*stacksection*/
.co_stack(NOLOAD):
{
.=ALIGN(8);
*(.co_stack.co_stack.*)
}>ram
__exidx_start=.;
.ARM.exidx:
{
*(.ARM.exidx*.gnu.linkonce.armexidx.*)
}>rom
__exidx_end=.;
.=ALIGN(4);
_end=.;
}
3.修改启动代码,使构造和析构函数能被正确调用
…
externunsignedlong__preinit_array_start;
externunsignedlong__preinit_array_end;
externunsignedlong__init_array_start;
externunsignedlong__init_array_end;
externunsignedlong_ctor_start;
externunsignedlong_ctor_end;
staticvoidcall_constructors(unsignedlong*start,unsignedlong*end)__attribute__((noinline));
staticvoidcall_constructors(unsignedlong*start,unsignedlong*end)
{
unsignedlong*i;
void(*funcptr)();
for(i=start;i<end;i++)
{
funcptr=(void(*)())(*i);
funcptr();
}
}
…
voidDefault_Reset_Handler(void)
{
…
/*Setupthemicrocontrollersystem.*/
SystemInit();
//InitializeCoOS(inorderthenew/deleteoperatorstoworkproperly
//priortocallingconstructors).Commentitoutifyoudon'tuseCoOS!
CoInitOS();
//CallC++globalconstructors
call_constructors(&__preinit_array_start,&__preinit_array_end);
call_constructors(&__init_array_start,&__init_array_end);
call_constructors(&_ctor_start,&_ctor_end);
/*Calltheapplication'sentrypoint.*/
main();
}
4.在组件页勾选CLibrary和Retargetprintf,之后注释掉printf.c文件中的如下语句,以避免重定义_impure_ptr
struct_reent*_impure_ptr=&r;
5.在工程配置(Projectconfiguration)中添加LinkedLibrarieslibstdc++和libsupc++,它们可以从GCCtoolchain的库中找到
http:
//www.coocox.org/Forum/topic.php?
id=730
http:
//www.coocox.org/Forum/topic.php?
id=873
例程:
103vb_cpp.zip
STM32103vb+CoIDE1.3.1+闪灯
说明——因CoIDE1.4.0存在bug“修改build.xml文件后无法保存”,故使用CoIDE1.3.1版本。
此bug在新版CoIDE中将被修复。
欢迎大家常来CooCox论坛交流讨论~