Home
Netbeans Eclipse Qt Java
Games
College of Engineering Aeronautics and Astronautics Agricultural and Biological Engineering Biomedical Engineering Chemical Engineering Civil Engineering Construction Engineering and Management Electrical and Computer Engineering Engineering Education Engineering Professional Education Environmental and Ecological Engineering Industrial Engineering Materials Engineering Mechanical Engineering Nuclear Engineering
EPICS (Engineering Projects In Community Service) First-Year Engineering Program First-Year Engineering Honors Program Global Engineering Program Minority Engineering Program Professional Practice (Co-Op) Program Women in Engineering Program
College Administration Schools Programs All Groups All People ECN Webmail
Purdue Home

ECE 264 Exercise 6

Create and Use Library

You have learned how to create programs using multiple source files. In some situations, you do not want to distribute the source files. Reasons may include

  • You do not want to reveal how certain features are implemented because you want to protect your intellectual properties.
  • You do not think the source files are needed or interesting to other programmers.
  • You do not want programmers to spend time writing Makefile and building the features.
  • Space is precious (for small computers) and only executable files are needed.

In these cases, you want to distribute only a library and a header file.

Create Library

Download the source code.  Create Makefile

main: main.o libex6.a
	gcc main.o -o main -L./ -lex6

main.o: main.c
	gcc -c main.c

libex6.a: f.h f1.c f2.c
	gcc -c f1.c
	gcc -c f2.c
	ar r libex6.a f1.o f2.o

clean:
	rm -f libex6.a *.o main

It should be familiar to you except two places:

        ar r libex6.a f1.o f2.o

creates a library file called libex6.a. This is the file with the functionality in f1.c and f2.c without giving the source code. The other new concept is

	gcc main.o -o main -L./ -lex6

There are two new concepts here. The first is the -L flag. It says "look for library in the following directory." Without this flag, gcc finds libraries in the standard locations, specified when gcc is installed. With this flag, gcc also looks for libraries in the current directory (./ means the same directory as the Makefile).  The last part -l means to link a library and the name of the library is libex6.a (skip the prefix lib in -l).

To build main, you only need to provide f.h and libex6.a, without giving away the source code of f1.c and f2.c.

What to Submit

The library file libex6.a.