📄 c-library-startup.html
字号:
<!-- Copyright (C) 2003 Red Hat, Inc. --><!-- This material may be distributed only subject to the terms --><!-- and conditions set forth in the Open Publication License, v1.0 --><!-- or later (the latest version is presently available at --><!-- http://www.opencontent.org/openpub/). --><!-- Distribution of the work or derivative of the work in any --><!-- standard (paper) book form is prohibited unless prior --><!-- permission is obtained from the copyright holder. --><HTML><HEAD><TITLE>C library startup</TITLE><meta name="MSSmartTagsPreventParsing" content="TRUE"><METANAME="GENERATOR"CONTENT="Modular DocBook HTML Stylesheet Version 1.76b+"><LINKREL="HOME"TITLE="eCos Reference Manual"HREF="ecos-ref.html"><LINKREL="UP"TITLE="C and math library overview"HREF="c-and-math-library-overview.html"><LINKREL="PREVIOUS"TITLE="Thread safety"HREF="libc-thread-safety.html"><LINKREL="NEXT"TITLE="I/O Package (Device Drivers)"HREF="io.html"></HEAD><BODYCLASS="SECT1"BGCOLOR="#FFFFFF"TEXT="#000000"LINK="#0000FF"VLINK="#840084"ALINK="#0000FF"><DIVCLASS="NAVHEADER"><TABLESUMMARY="Header navigation table"WIDTH="100%"BORDER="0"CELLPADDING="0"CELLSPACING="0"><TR><THCOLSPAN="3"ALIGN="center">eCos Reference Manual</TH></TR><TR><TDWIDTH="10%"ALIGN="left"VALIGN="bottom"><AHREF="libc-thread-safety.html"ACCESSKEY="P">Prev</A></TD><TDWIDTH="80%"ALIGN="center"VALIGN="bottom">Chapter 13. C and math library overview</TD><TDWIDTH="10%"ALIGN="right"VALIGN="bottom"><AHREF="io.html"ACCESSKEY="N">Next</A></TD></TR></TABLE><HRALIGN="LEFT"WIDTH="100%"></DIV><DIVCLASS="SECT1"><H1CLASS="SECT1"><ANAME="C-LIBRARY-STARTUP">C library startup</H1><P>The C library includes a function declared as:</P><TABLEBORDER="5"BGCOLOR="#E0E0F0"WIDTH="70%"><TR><TD><PRECLASS="PROGRAMLISTING">void <TTCLASS="FUNCTION">cyg_iso_c_start</TT>( void )</PRE></TD></TR></TABLE><P>This function is used to start an environment in which anISO C style program can run in the most compatible way.</P><P>What this function does is to create a thread which will invoke <TTCLASS="FUNCTION">main()</TT> — normallyconsidered a program's entry point. In particular, it cansupply arguments to <TTCLASS="FUNCTION">main()</TT> using the CYGDAT_LIBC_ARGUMENTSconfiguration option, and when returning from <TTCLASS="FUNCTION">main()</TT>,or calling <TTCLASS="FUNCTION">exit()</TT>, pending stdio file outputis flushed and any functions registered with <TTCLASS="FUNCTION">atexit()</TT> areinvoked. This is all compliant with the ISO C standard in this respect. </P><P>This thread starts execution when the <SPANCLASS="emphasis"><ICLASS="EMPHASIS">eCos</I></SPAN> scheduleris started. If the <SPANCLASS="emphasis"><ICLASS="EMPHASIS">eCos</I></SPAN> kernel package is notavailable (and hence there is no scheduler), then <TTCLASS="FUNCTION">cyg_iso_c_start()</TT> willinvoke the <TTCLASS="FUNCTION">main()</TT> function directly, i.e.it will not return until the <TTCLASS="FUNCTION">main()</TT> functionreturns. </P><P>The <TTCLASS="FUNCTION">main()</TT> function should be definedas the following, and if defined in a C++ file,should have “C” linkage: </P><TABLEBORDER="5"BGCOLOR="#E0E0F0"WIDTH="70%"><TR><TD><PRECLASS="PROGRAMLISTING">extern int <TTCLASS="FUNCTION">main</TT>( int <SPANCLASS="emphasis"><ICLASS="EMPHASIS">argc,</I></SPAN> char *<SPANCLASS="emphasis"><ICLASS="EMPHASIS">argv[] </I></SPAN>)</PRE></TD></TR></TABLE><P>The thread that is started by <TTCLASS="FUNCTION">cyg_iso_c_start()</TT> canbe manipulated directly, if you wish. For example you can suspendit. The kernel C API needs a handle to do this, which is availableby including the following in your source code.</P><TABLEBORDER="5"BGCOLOR="#E0E0F0"WIDTH="70%"><TR><TD><PRECLASS="PROGRAMLISTING">extern cyg_handle_t cyg_libc_main_thread;</PRE></TD></TR></TABLE><P>Then for example, you can suspend the thread with the line:</P><TABLEBORDER="5"BGCOLOR="#E0E0F0"WIDTH="70%"><TR><TD><PRECLASS="PROGRAMLISTING">cyg_thread_suspend( cyg_libc_main_thread );</PRE></TD></TR></TABLE><P>If you call <TTCLASS="FUNCTION">cyg_iso_c_start()</TT> anddo not provide your own <TTCLASS="FUNCTION">main()</TT> function,the system will provide a <TTCLASS="FUNCTION">main()</TT> for youwhich will simply return immediately.</P><P>In the default configuration, <TTCLASS="FUNCTION">cyg_iso_c_start()</TT> isinvoked automatically by the <TTCLASS="FUNCTION">cyg_package_start()</TT> functionin the infrastructure configuration. This means that in the simplestcase, your program can indeed consist of simply:</P><TABLEBORDER="5"BGCOLOR="#E0E0F0"WIDTH="70%"><TR><TD><PRECLASS="PROGRAMLISTING">int main( int argc, char *argv[] ){ printf("Hello eCos\n");}</PRE></TD></TR></TABLE><P>If you override <TTCLASS="FUNCTION">cyg_package_start()</TT> or <TTCLASS="FUNCTION">cyg_start()</TT>,or disable the infrastructure configuration option CYGSEM_START_ISO_C_COMPATIBILITYthen you must ensure that you call <TTCLASS="FUNCTION">cyg_iso_c_start()</TT> yourselfif you want to be able to have your program start at the entry pointof <TTCLASS="FUNCTION">main()</TT> automatically.</P></DIV><DIVCLASS="NAVFOOTER"><HRALIGN="LEFT"WIDTH="100%"><TABLESUMMARY="Footer navigation table"WIDTH="100%"BORDER="0"CELLPADDING="0"CELLSPACING="0"><TR><TDWIDTH="33%"ALIGN="left"VALIGN="top"><AHREF="libc-thread-safety.html"ACCESSKEY="P">Prev</A></TD><TDWIDTH="34%"ALIGN="center"VALIGN="top"><AHREF="ecos-ref.html"ACCESSKEY="H">Home</A></TD><TDWIDTH="33%"ALIGN="right"VALIGN="top"><AHREF="io.html"ACCESSKEY="N">Next</A></TD></TR><TR><TDWIDTH="33%"ALIGN="left"VALIGN="top">Thread safety</TD><TDWIDTH="34%"ALIGN="center"VALIGN="top"><AHREF="c-and-math-library-overview.html"ACCESSKEY="U">Up</A></TD><TDWIDTH="33%"ALIGN="right"VALIGN="top">I/O Package (Device Drivers)</TD></TR></TABLE></DIV></BODY></HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -