• Aucun résultat trouvé

Multiple Input Files to Executables

Dans le document For Mary (Page 187-191)

public void speak() {

System.out.println(string);

} }

These three classes can be compiled into a native executable in several ways.

The most straightforward is to do it in a single command line, as follows:

$ gcj --main=SayHello Say.java SayHello.java WordCat.java -o SayHello

This command will compile all three source files into object files and link the object files into a single executable namedSayHello(establishing themain()method of SayHelloas the program’s entry point). The same result can be achieved by using a sequence of commands to compile the individual object files and then linking them together into an executable:

$ gcj -c SayHello.java

$ gcj -c Say.java

$ gcj -c WordCat.java

$ gcj --main=SayHello Say.o SayHello.o WordCat.o -o SayHello It is possible to first compile the source files into class files and then compile and link them into an executable, as described in the next section.

Multiple Input Files to Executables

Using the same source code examples as in the previous section, the following command can be used to compile three source files into three class files:

$ gcj -C SayHello.java Say.java WordCat.java

The result is a set of class files that can be executed by using the Java Virtual Machine, as follows:

USINGTHECOMPILERCOLLECTION

$ java SayHello

All the Java source code in the current directory can be compiled into class files with the following single command:

$ gcj -C *.java

Java class files can be treated as if they were source code files that can be compiled and linked into a native executable. In the following example, the first command compiles the source into a collection of class files, and the second command compiles the class files into a native executable program:

$ gcj -C SayHello.java Say.java WordCat.java

$ gcj --main=SayHello Say.class WordCat.class SayHello.class -o SayHello

Thegcjcommand determines what to do with an input file named on the command line by looking at the file name suffix. If the suffix is.java, the compiler knows that it is a Java source code file that must be compiled. If the suffix is .class, the file is assumed to be Java bytecodes that are to be compiled. The.osuffix indicates a native object file that can be linked directly into the native executable. Because of this, it is possible to mix the input and have a program compiled and linked from a combination of source, class, and object files, as in the following example:

$ gcj -c SayHello.java -o SayHello.o

$ gcj -C WordCat.java

$ gcj --main=SayHello SayHello.o Say.java WordCat.class -o SayHello

Generating Assembly Language

The following class, when executed, creates an instance of itself that it uses to display a string on standard output:

/* Jasm.java */

public class Jasm {

public static void main(String arg[]) { Jasm jsm = new Jasm();

jsm.speak();

}

public void speak() {

System.out.println("Jasm speaks");

} }

This class is a complete application and can be compiled and run. It can also be compiled into native assembly language with the following command:

$ gcj -S Jasm.java

The output from this command is a file namedJasm.swith the assembler code that can be used to create an executable.

An alternate method of producing an assembly language file is to use a class file as input. The two following commands create a class file fromJasm.javaand use it to generate an assembly language file:

$ gcj -C Jasm.java

$ gcj -S Jasm.class

The output files from these commands are namedJasm.classandJasm.s.

Creating a Static Library

A static library is a collection of.ofiles stored in a single file, called astatic libraryor an archive file. Linking a program with the contents of the library is the same as linking a program with the individual object files.

Using the example source files from earlier in this chapter, the following command will create the object filesWordCat.oandSay.oto be stored in a library:

$ gcj -c WordCat.java Say.java

Thearutility is used to construct and maintain static libraries. Usingarwith the -roption will cause the named library file to be created from the named object files, or if the library already exists, the-roption will update the library with newer versions of the object files. The following command creates a library named libsay.a that contains the two object files:

$ ar -r libsay.a WordCat.o Say.o

To use the object files stored in the library, it is only necessary to include the name of the library on thegcjcommand line, as in the following example, which produces an executable program namedlibhello:

$ gcj --main=SayHello SayHello.java libsay.a -o libhello

Specifying the library name on the command line this way requires that the library be in the current directory. If the library is in a directory thatgcjsearches to find libraries, you can use the-loption for specifying the library name, as in the following example:

$ gcj --main=SayHello SayHello.java -lsay -o libhello More information on the location of libraries can be found in Chapter 12.

Creating a Shared Library

A shared library is a collection of object files stored inside a single file, in much the same way as a static library, with two main differences. First, the object files inside the shared (also calleddynamic) library are loaded and linked to the program at the time the program starts running. Second, the object files must be compiled in a special way so they can be executed without modification wherever they happen to be loaded into memory. The following example uses the source files described earlier in this chapter.

To create the object files to be stored in the shared library, they must be compiled with the-fpicoption to produceposition independent code. This is code that uses only relative addressing for internal references and branching, which precludes the necessity of an extensive relocation process every time the code is loaded into memory. The following command will produce object files in the desired format:

$ gcj -fpic -c WordCat.java Say.java

Thegcjcommand is used with the-sharedoption to link the object files into a new shared library namedlibsay.so, as follows:

$ gcj -shared WordCat.o Say.o -o libsay.so

The source fileSayHello.javacan be compiled into an executable program named shlibhello that uses the object files stored in the library by including the library name on the command line as follows:

$ gcj --main=SayHello SayHello.java libsay.so -o shlibhello

The actual content oflibsay.sois not included inside theshlibhelloexecutable.

What is included in the executable are the instructions necessary to load the required object modules from a shared library with the correct name. For this to happen, the executable must be able to locate the library whenever it is run. Information on the location of shared libraries can be found in Chapter 12.

USINGTHECOMPILERCOLLECTION

Dans le document For Mary (Page 187-191)