Compile C programs in Linux

Compile C programs in Linux

c99 – compile standard C programs

1. The following usage example compiles foo.c and creates the executable file foo:

c99 -o foo foo.c

The following usage example compiles foo.c and creates the object file foo.o:

c99 -c foo.c

The following usage example compiles foo.c and creates the executable file a.out:

c99 foo.c

The following usage example compiles foo.c, links it with bar.o, and creates the executable file a.out. It may also create and leave foo.o:

c99 foo.c bar.o

2. The following example shows how an application using threads interfaces can test for support of and use a programming environment supporting 32-bit int, long, and pointer types and an off_t type using at least 64 bits:

 

if [ $(getconf _POSIX_V6_ILP32_OFFBIG) != "-1" ]
then
     c99 $(getconf POSIX_V6_ILP32_OFFBIG_CFLAGS) -D_XOPEN_SOURCE=600 \
     $(getconf POSIX_V6_ILP32_OFFBIG_LDFLAGS) foo.c -o foo \
     $(getconf POSIX_V6_ILP32_OFFBIG_LIBS) -l pthread
else

     echo ILP32_OFFBIG programming environment not supported
     exit 1

fi

3. The following examples clarify the use and interactions of -L options and -l operands.

Consider the case in which module a.c calls function f() in library libQ.a, and module b.c calls function g() in library libp.a. Assume that both libraries reside in /a/b/c. The command line to compile and link in the desired way is:

#c99 -L /a/b/c main.o a.c -l Q b.c -l p

In this case the -l Q operand need only precede the first -l p operand, since both libQ.a and libp.a reside in the same directory.

Multiple -L operands can be used when library name collisions occur. Building on the previous example, suppose that the user wants to use a new libp.a, in /a/a/a, but still wants f() from /a/b/c/libQ.a:

#c99 -L /a/a/a -L /a/b/c main.o a.c -l Q b.c -l p

In this example, the linker searches the -L options in the order specified, and finds /a/a/a/libp.a before /a/b/c/libp.a when resolving references for b.c. The order of the -l operands is still important, however.

4. The following example shows how an application can use a programming environment where the widths of the following types:

blksize_t, cc_t, mode_t, nfds_t, pid_t, ptrdiff_t, size_t, speed_t, ssize_t, suseconds_t, tcflag_t,

useconds_t, wchar_t, wint_t

are no greater than the width of type long:

# First choose one of the listed environments …

# … if there are no additional constraints, the first one will do:

CENV=$(getconf _POSIX_V6_WIDTH_RESTRICTED_ENVS | head -n l)

# … or, if an environment that supports large files is preferred,

# look for names that contain “OFF64” or “OFFBIG”. (This chooses

# the last one in the list if none match.)

for CENV in $(getconf _POSIX_V6_WIDTH_RESTRICTED_ENVS)
do
   case $CENV in
      *OFF64*|*OFFBIG*) break ;;
   esac
done

# The chosen environment name can now be used like this:

c99 $(getconf ${CENV}_CFLAGS) -D _POSIX_C_SOURCE=200112L \
$(getconf ${CENV}_LDFLAGS) foo.c -o foo \
$(getconf ${CENV}_LIBS)

Leave a Reply

Your email address will not be published. Required fields are marked *