Puzzle SL21


To show linkage in action, create a project that consists of several files. Here are three files:


/* --- fileA.c --- */
int x = 8;

/* --- fileB.c --- */
void foo()
{
  int x;
  x = 99;
}

/* --- fileC.c --- */
#include <stdio.h>
void foo();   /* prototype */

int x;
void main()
{
  foo();
  printf("%d\n", x );
  system("pause");       /* remove if not needed */
}

A prototype for foo() is needed in fileC.c so that the function call in main() can be compiled.

Compiling and linking is easily done from the command line. Assuming that the three files are in the same directory, which is currently the default directory, the three files can be compiled and linked by the following:

prompt> gcc fileC.c fileA.c fileB.c

The command "gcc" invokes the compiler on the three source files, and then invokes the linker on the resulting three object files. The linker then creates the executable file a.out in the default directory. To execute it, do this:

prompt> ./a.out

In an integrated development environment (IDE) such as Dev-C++, file creation, editing, compiling, linking, and execution are all done from inside the IDE. In Dev-C++, create a new project, create each of the above files and add each to the project, then "compile and run" the project. This compiles the source files into object files, links them into an executable, and runs the executable. (For details, see the Dev-C++ documentation under its "Help" menu.)

Question: What does the above project write to the monitor when it is compiled, linked, and executed?



Answer         Next Page         Previous Page Home