Program Examples:Shared Objects
The following example demonstrates how to compile a shared object in Linux and link it with softMC at run-time.
Below there are 3 example files: A C source file, a C header file and a makefile.
The source and header files implement a simple program that is given 2 integers as arguments and returns their sum.
This program DOESN'T include a main function, therefore the program can be compiled only into a library, and not an executable.
Contents
Example Program
C Source file: Example.c
#include <stdio.h>
#include "Example.h"
int SHARED_OBJECT_SUM(int arg1, int arg2)
{
print_hello();
return arg1 + arg2;
}
void print_hello(void)
{
printf("Hello! This is just an example\n");
}
C Header file: Example.h
void print_hello(void);
Output strings of functions like printf and fprintf are usually displayed in the Linux terminal, however, a softMC user doesn't have access to the terminal,
therefore these strings are directed to an object call 'nohup' and can later be read by the user.
To display the contents of nohup do the following:
1. Connect to the softMC using ssh or serial console.
2. In the Linux terminal type:
-bash-3.2$ cat /var/home/mc/nohup.out
Makefile
The compiler to be used in order to build the shared object is defined by the CC variable in the makefile. Please notice that it is assigned with a complete path to gcc compiler. This specific gcc is a part of the tool chain we use to build softMC.
When compiling shared objects to be linked with softMC it is advised to use this tool chain.
You can download it from:
--- LINK ...
If you copy the tool chain to your user's directory, /home/my_user, you don't have to change the CC variable assignment in the makefile.
The CFLAGS in the example might cause compile errors. It is not advised, but you can remove those that prevent a successful compilation. The better option is to fix the compilation errors.
The makefile syntax:
CC=~/OSELAS.Toolchain-2011.03.1/i586-unknown-linux-gnu/gcc-4.5.2-glibc-2.13-binutils-2.21-kernel-2.6.36-sanitized/bin/i586-unknown-linux-gnu-gcc
CFLAGS=-c -g -ansi -pedantic -Wall -Werror
LD_LIBRARY_PATH := .:$(LD_LIBRARY_PATH)
C_SOURCE=Example.c
OBJECTS=$(C_SOURCE:.c=.o)
TARGET=EXAMPLE.O
$(TARGET): $(OBJECTS)
$(CC) -shared -o $@ $(OBJECTS)
$(OBJECTS): $(C_SOURCE)
$(CC) $(CFLAGS) -c $(C_SOURCE)
clean:
rm *.o *.a *.so exe
In order to adapt the makefile to your needs, please change the assignment of the variable 'C_SOURCE' to the name of the source files in your project, and the assignment of the variable 'TARGET'.
IMPORTANT | |
softMC can handle only UPPERCASE file names limited to 8.3 convention, meaning, up to 8 characters, followed by a dot, followed by the extensions PRG, or LIB or O (of course in our case it will be .O) Bear that in mind when assigning the TARGET variable |
After a successful compilation you will find in the working directory the file EXAMPLE.O, or whatever string was assigned to the variable 'TARGET'.
You can copy the shared object to softMC in 2 different ways:
1. Using scp, copy the file directly to softMC. In the Linux terminal type:
my_user@my_computer:~/working_directory$ scp EXAMPLE.O mc@mc.ip.add.ress:/FFS0/SSMC
Where mc.ip.add.ress is the sfotMC's ip address, for example, 192.168.7.152.
2. Use the ControlStudio's file manager to drag and drop the file EXAMPLE.O to the softMC.
The shared object now resides within softMC. Now we want to link softMC with the shared object during run-time, and use it. Linking the shared object is done with 'Load' command in CONFIG.PRG context. Please add to your CONFIG.PRG the line:
oload EXAMPLE.O
Now we need to declare about the shared object's contents and how to use it. This is done by the PROTO.PRO file. please add the following line to your PROTO.PRO file:
import_c SHARED_OBJECT_SUM(byval as long, byval as long) as long
Of course, 'SHARED_OBJECT_SUM' is a function implemented in Example.c. You will need to specify the prototypes of your own functions. Using ControlStudio send the edited CONFIG.PRG and PROTO.PRO to softMC and type in the ControlStudio terminal 'reset all'.
If the process was successful, you are now able to invoke functions from the shared object. Type in the ControlStudio terminal:
-->
-->
-->
-->?SHARED_OBJECT_SUM(5, 6)
11
-->
The example corresponds to commit SHA-1: ada143a7f402e6bbad24c13c56401a5393ce3d2b in GIT.