Difference between revisions of "Program Examples:Shared Objects"

From SoftMC-Wiki
Jump to: navigation, search
Line 1: Line 1:
The following example demonstrates how to compile a shared object in Linux and link it with softMC at run time.
+
The following example demonstrates how to compile a shared object in Linux and link it with softMC at run-time.
  
 
Attached are 3 example files: A C source file, a C header file and a makefile.
 
Attached are 3 example files: A C source file, a C header file and a makefile.
Line 36: Line 36:
 
</pre>
 
</pre>
  
 +
In order to adapt the makefile to your needs, please change the word 'Example' to the name of the required sources and the intended target.
 +
After a successful compilation you will find in the working directory the file libExample.so
 +
The shared object in its current name cannot be used by softMC as currently softMC can handle only UPPERCASE file names limited to 8.3 convention, meaning, up to 8 characters, followed by a dot, followed by the extension PRG, or LIB or O.
 +
 +
Copy the shared object to create the same file by a new name:
 +
<pre>
 +
my_user@my_computer:~/working_directory$ cp libExample.so EXAMPLE.O
 +
</pre>
 +
 +
You can copy this file to softMC in 2 different ways:
 +
1. Using scp, copy the file directly to softMC. In the Linux terminal type:
 +
 +
<pre>
 +
my_user@my_computer:~/working_directory$ scp EXAMPLE.O mc@mc.ip.add.ress:/FFS0/SSMC
 +
</pre>
 +
 +
2. Use the ControlStudio's file manager to drag and drop the file EXAMPLE.O to the softMC.
 +
 +
The shared object no resides within softMC. Now we want to link softMC with the shared object during run-time, and use it.
  
 
<syntaxhighlight lang="vb">
 
<syntaxhighlight lang="vb">

Revision as of 15:38, 29 December 2014

The following example demonstrates how to compile a shared object in Linux and link it with softMC at run-time.

Attached 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.

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.

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)


libExample.so: Example.o
	$(CC) -shared -o $@ Example.o

Example.o: Example.c Example.h
	$(CC) $(CFLAGS) -c Example.c

clean:
	rm *.o *.a *.so exe

In order to adapt the makefile to your needs, please change the word 'Example' to the name of the required sources and the intended target. After a successful compilation you will find in the working directory the file libExample.so The shared object in its current name cannot be used by softMC as currently softMC can handle only UPPERCASE file names limited to 8.3 convention, meaning, up to 8 characters, followed by a dot, followed by the extension PRG, or LIB or O.

Copy the shared object to create the same file by a new name:

my_user@my_computer:~/working_directory$ cp libExample.so EXAMPLE.O

You can copy this file 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

2. Use the ControlStudio's file manager to drag and drop the file EXAMPLE.O to the softMC.

The shared object no resides within softMC. Now we want to link softMC with the shared object during run-time, and use it.

-->
-->
-->
-->?SHARED_OBJECT_SUM(5, 6)
11
-->


The example corresponds to commit SHA-1: ada143a7f402e6bbad24c13c56401a5393ce3d2b in GIT.


See Also