C in RISC OS: Difference between revisions

From RISC OS
Jump to navigationJump to search
No edit summary
 
(9 intermediate revisions by 5 users not shown)
Line 1: Line 1:
The workings of C with {{RISC OS}} and its interactions with
<hr>

<h3>Introduction</h3>

<p>The workings of C with RISC&nbsp;OS and its interactions with
various libraries often raise many questions. All too often these
various libraries often raise many questions. All too often these
questions are incompletely answered, leading to more questions or
questions are incompletely answered, leading to more questions or
confusion. This document attempts for the first time to fully explain
confusion. This document attempts for the first time to fully explain
all the concepts in an understandable way.</p>
all the concepts in an understandable way.

<p></p>


<h3>Standards</h3>
==Standards==


<p>C is defined by a number of standards. Probably the most important
C is defined by a number of standards. Probably the most important
one is the ANSI C standard. Among other things, this defines what
one is the ANSI C standard. Among other things, this defines what
functionality must be present in a standard C library implementation.
functionality must be present in a standard C library implementation.
When the &quot;Shared C Library&quot; is referred to on RISC&nbsp;OS we
When the &quot;Shared C Library&quot; is referred to on {{RISC OS}} we
are talking about the library developed by Acorn which is ANSI compliant.</p>
are talking about the library developed by Acorn which is ANSI compliant.


<p>The Shared C Library (SCL) resides in a module in ROM (or RAM if it's softloaded)
The [[Shared C Library]] (SCL) resides in a module in ROM (or RAM if it's softloaded)
and is called by C programs linked to it via &quot;stubs&quot; (see below).
and is called by C programs linked to it via &quot;stubs&quot; (see below).
It also provides other functionality required by a C program such as stack
It also provides other functionality required by a C program such as stack
extension and general environment interaction.</p>
extension and general environment interaction.


<p>POSIX is another standard which defines a considerable number of
POSIX is another standard which defines a considerable number of
behaviour characteristics of Unix operating systems. Since these are
behaviour characteristics of Unix operating systems. Since these are
invarialy C-based, it also defines many C functions that should be
invariably C-based, it also defines many C functions that should be
provided by a POSIX-compliant system.</p>
provided by a POSIX-compliant system.


<p>The SCL makes no attempt to implement any functions beyond
The SCL makes no attempt to implement any functions beyond
those defined by ANSI - indeed it was never intended that it should - but UnixLib
those defined by ANSI - indeed it was never intended that it should - but [[UnixLib]]
does implement many of these, along with all the functions found in the SCL.
does implement many of these, along with all the functions found in the SCL.
Because of this, UnixLib can be invaluable when making Unix programs
Because of this, UnixLib can be invaluable when making Unix programs
work on RISC&nbsp;OS</p>
work on {{RISC OS}}


<p>While it is certain that UnixLib doesn't conform fully to POSIX, it
While it is certain that UnixLib doesn't conform fully to POSIX, it
makes a good effort, and it is being improved all the time.</p>
makes a good effort, and it is being improved all the time.


==Programs and Libraries==
<p></p>


When a program is linked (the final stage where the executable is
<h3>Programs and Libraries</h3>

<p>When a program is linked (the final stage where the executable is
produced), the program will be linked to one of two things - SCL stubs,
produced), the program will be linked to one of two things - SCL stubs,
or UnixLib.</p>
or UnixLib.


<p>Stubs are a small piece of code that is accessed by your program when
Stubs are a small piece of code that is accessed by your program when
making a function call to a C function in the SCL occurs for the first time.
making a function call to a C function in the SCL occurs for the first time.
Stubs &quot;fix up&quot; the call, and point it directly at the appropriate
Stubs &quot;fix up&quot; the call, and point it directly at the appropriate
code in the <i>SharedCLibrary</i> module. The advantage of this is that the
code in the <i>SharedCLibrary</i> module. The advantage of this is that the
code is shared by all callers, and not duplicated for all programs. It also
code is shared by all callers, and not duplicated for all programs. It also
means that when a bug fix occurs, all programs benefit.</p>
means that when a bug fix occurs, all programs benefit.


<p>On the other hand, when a program is linked to UnixLib, all the library code
<p>On the other hand, when a program is linked to UnixLib, all the library code
used by a program from Unixlib is attached to your program. This explains why
used by a program from Unixlib is attached to your program. This explains why
UnixLib programs end up bigger than ones linked with SCL. This is referred
UnixLib programs end up bigger than ones linked with SCL. This is referred
to as statically linked.</p>
to as statically linked.


<p>There is one exception to this. UnixLib relies on the module
There is one exception to this. UnixLib relies on the module
<i>SharedUnixLibrary</i>. This module implements a small amount of
[[SharedUnixLibrary]]. This module implements a small amount of
functionality that cannont safely be put into an application. It is
functionality that cannot safely be put into an application. It is
hoped that one day more functionality will move into this module to
hoped that one day more functionality will move into this module to
give the same advantages that the SCL module does.</p>
give the same advantages that the SCL module does.


==Compilers and Libraries==
<p></p>


By default, [[Norcroft]] (Acorn C/C++) uses the SharedCLibrary, and GCC uses UnixLib.
<h3>Compilers and Libraries</h3>

<p>By default, Norcroft uses the SharedCLibrary, and GCC uses UnixLib.
LCC also uses SharedCLibrary by default.</p>
LCC also uses SharedCLibrary by default.</p>


<p>In some cases, it's appropriate that the compiler use the alternate
In some cases, it's appropriate that the compiler use the alternative
library. As with all libraries, these are split into the library's headers
library. As with all libraries, these are split into the library's headers
which advertise their functionality, and the library itself, which contains
which advertise their functionality, and the library itself, which contains
the code. The following describes use of each library with the 3 compilers</p>
the code. The following describes use of each library with the 3 compilers


<ul>
<ul>
<li><b>GCC</b>
<li><b>[[GCC]]</b>
<dl>
<dl>
<p>Inside the !gcc application are contained headers and library files
<p>Inside the !gcc application are contained headers and library files
Line 87: Line 76:
<p>Normal invocation will use Unixlib:</p>
<p>Normal invocation will use Unixlib:</p>


<pre>
<pre>gcc hello.c -o hello</pre>
gcc hello.c -o hello
</pre>


<p>Use the -mlibscl switch to use SCL:</p>
<p>Use the -mlibscl switch to use SCL:</p>


<pre>gcc -mlibscl hello.c -o hello</pre>
<pre>gcc -mlibscl hello.c -o hello</pre>
</dl>
</dl></li>
<li><b>[[LCC]]</b>

<li><b>LCC</b>


<dl>
<dl>
<dd>
<p>LCC is supplied with the same SCL files as are supplied with
<p>LCC is supplied with the same SCL files as are supplied with
GCC in order to make it a stand-alone compiler.</p>
GCC in order to make it a stand-alone compiler.</p>
Line 105: Line 90:
<p>Normal invocation will use SCL:</p>
<p>Normal invocation will use SCL:</p>


<pre>
<pre>lcc hello.c -o hello</pre>
lcc hello.c -o hello
</pre>


<p>Use the -Bunix switch to use UnixLib:</p>
<p>Use the -Bunix switch to use UnixLib:</p>


<pre>
<pre>lcc -Bunix hello.c -o hello</pre>
lcc -Bunix hello.c -o hello
</pre>


<p>This assumes that UnixLib is actually available. Currrently,
<p>This assumes that UnixLib is actually available. Currrently,
Line 120: Line 101:
for LCC and Norcroft. For more details, see LCC's !Help file.
for LCC and Norcroft. For more details, see LCC's !Help file.
</p>
</p>
</dl></li>

<li><b>[[Norcroft]]</b>
</dd>
</dl>
</li>

<li><b>Norcroft</b>
<dl>
<dl>
<dd>
<p>Norcroft is supplied with SharedCLibrary files.</p>
<p>Norcroft is supplied with SharedCLibrary files.</p>


<p>Normal invocation will use SCL:</p>
<p>Normal invocation will use SCL:</p>


<pre>
<pre>cc hello.c -o hello</pre>

cc hello.c -o hello
</pre>


<p>To use UnixLib:</p>
<p>To use UnixLib:</p>


<pre>cc -jUnix: -IUnix: hello.c -o hello</pre>
<pre>
cc -jUnix: -IUnix: hello.c -o hello
</pre>


<p>As for lcc, this assumes the presence of UnixLib. The comments
<p>As for lcc, this assumes the presence of UnixLib. The comments
in LCC's !Help file apply equally in this case.
in LCC's !Help file apply equally in this case.
</p>
</p>
</dl></li>

</dd>
</dl>
</li>
</ul>
</ul>


<p>In bigger programs, compiling and linking is usually split into two
In bigger programs, compiling and linking is usually split into two
operations. You should ensure that you use the same flags at each
operations. You should ensure that you use the same flags at each
point, otherwise you will have unexpected results.</p>
point, otherwise you will have unexpected results.


==Mixing Libraries and Compilers==
<p></p>


It is possible to mix SCL and UnixLib-targeted AOF object files, but considerable care should
be taken. In general, it is discouraged, and rarely required. Under GCC 4 it is not possible,
since GCC 4 produces ELF object files instead, and these are soft-float rather than the traditional
RISC OS hard-float.


Some of the issues are discussed in the section on [[UnixLib#Differences|UnixLib Differences]].
<h3>Mixing Libraries and Compilers</h3>

<p>Sometimes it is convenient to try and mix object files or libraries
compiled with one compiler or targeted against different C libraries.</p>

<p>In general, I would discourage this, unless you know what you are
doing. The reasons are as follows:</p>

<ul>
<li><p>The size of the FILE structure varies between SharedCLibrary and
UnixLib, as UnixLib has to hold more data to implement some POSIX
functionality. This means that programs accessing members of this
structure will run into trouble. Although this is quite rare,
you should be aware of it.</p>

<li><p>The file descriptors stdout, stdin and stderr referring to standard
streams are declared differently in SharedCLibrary and UnixLib
headers. This is responsible for the mysterious missing symbol
&quot;__iob&quot; sometimes seen at link.</p>

<li><p>Compilers may define some maths operations in terms of differently
named functions - in particular, division - so that even if object
files are compiled against the C library, you may see link errors
if you mix output from two different compilers.</p>

</ul>

<p>This is not to say that mixing output is not impossible - it is, with
care, but you should take note of the differences.</p>


{{GCC and GCCSDK pages}}
<hr>

Latest revision as of 15:06, 29 April 2018

The workings of C with RISC OS and its interactions with various libraries often raise many questions. All too often these questions are incompletely answered, leading to more questions or confusion. This document attempts for the first time to fully explain all the concepts in an understandable way.

Standards

C is defined by a number of standards. Probably the most important one is the ANSI C standard. Among other things, this defines what functionality must be present in a standard C library implementation. When the "Shared C Library" is referred to on RISC OS we are talking about the library developed by Acorn which is ANSI compliant.

The Shared C Library (SCL) resides in a module in ROM (or RAM if it's softloaded) and is called by C programs linked to it via "stubs" (see below). It also provides other functionality required by a C program such as stack extension and general environment interaction.

POSIX is another standard which defines a considerable number of behaviour characteristics of Unix operating systems. Since these are invariably C-based, it also defines many C functions that should be provided by a POSIX-compliant system.

The SCL makes no attempt to implement any functions beyond those defined by ANSI - indeed it was never intended that it should - but UnixLib does implement many of these, along with all the functions found in the SCL. Because of this, UnixLib can be invaluable when making Unix programs work on RISC OS

While it is certain that UnixLib doesn't conform fully to POSIX, it makes a good effort, and it is being improved all the time.

Programs and Libraries

When a program is linked (the final stage where the executable is produced), the program will be linked to one of two things - SCL stubs, or UnixLib.

Stubs are a small piece of code that is accessed by your program when making a function call to a C function in the SCL occurs for the first time. Stubs "fix up" the call, and point it directly at the appropriate code in the SharedCLibrary module. The advantage of this is that the code is shared by all callers, and not duplicated for all programs. It also means that when a bug fix occurs, all programs benefit.

On the other hand, when a program is linked to UnixLib, all the library code used by a program from Unixlib is attached to your program. This explains why UnixLib programs end up bigger than ones linked with SCL. This is referred to as statically linked. There is one exception to this. UnixLib relies on the module SharedUnixLibrary. This module implements a small amount of functionality that cannot safely be put into an application. It is hoped that one day more functionality will move into this module to give the same advantages that the SCL module does.

Compilers and Libraries

By default, Norcroft (Acorn C/C++) uses the SharedCLibrary, and GCC uses UnixLib.

LCC also uses SharedCLibrary by default.

In some cases, it's appropriate that the compiler use the alternative library. As with all libraries, these are split into the library's headers which advertise their functionality, and the library itself, which contains the code. The following describes use of each library with the 3 compilers

  • GCC

    Inside the !gcc application are contained headers and library files for both UnixLib and SharedCLibrary. The latter is a free version which is functionally identical to the version supplied with Norcroft (with a few additions).

    Normal invocation will use Unixlib:

    gcc hello.c -o hello

    Use the -mlibscl switch to use SCL:

    gcc -mlibscl hello.c -o hello
  • LCC

    LCC is supplied with the same SCL files as are supplied with GCC in order to make it a stand-alone compiler.

    Normal invocation will use SCL:

    lcc hello.c -o hello

    Use the -Bunix switch to use UnixLib:

    lcc -Bunix hello.c -o hello

    This assumes that UnixLib is actually available. Currrently, UnixLib is only distributed as part of the !gcc application (in the past it wasn't). There are plans to make a stand-alone version for LCC and Norcroft. For more details, see LCC's !Help file.

  • Norcroft

    Norcroft is supplied with SharedCLibrary files.

    Normal invocation will use SCL:

    cc hello.c -o hello

    To use UnixLib:

    cc -jUnix: -IUnix: hello.c -o hello

    As for lcc, this assumes the presence of UnixLib. The comments in LCC's !Help file apply equally in this case.

In bigger programs, compiling and linking is usually split into two operations. You should ensure that you use the same flags at each point, otherwise you will have unexpected results.

Mixing Libraries and Compilers

It is possible to mix SCL and UnixLib-targeted AOF object files, but considerable care should be taken. In general, it is discouraged, and rarely required. Under GCC 4 it is not possible, since GCC 4 produces ELF object files instead, and these are soft-float rather than the traditional RISC OS hard-float.

Some of the issues are discussed in the section on UnixLib Differences.

GCC and GCCSDK pages
GCC under RISC OS

GCC for RISC OS, GCC tutorial, GCC common switches, GCC for beginners, UnixLib, ELFLoader
GCCSDK and Unix porting
GCCSDK, GCCSDK Releases, GCCSDK Development, Using GCCSDK, Autobuilder Development and Packaging Cygwin setup, Accelerating autobuilder with apt-proxy, ChoX11, Developer help wanted