zoukankan      html  css  js  c++  java
  • Layout of larger C programs

    1. Directory structure

     

    Many times with large projects, in order to keep things neat and orderly, source and include files are distributed across several directories. Generally (but not always), source (*.c) files are kept in a directory named src/, include (*.h) files are kept in a directory named inc/ (or sometimes include/), and object files are kept in a directory named obj/. The executables for large projects are also generally kept in a directory named bin/.

    2.Creating C libraries using the archive (ar) command

    Creating a Library:

    Now that you have the code working with the source, include, object and executable files in their appropriate directories, you will create a C library with which you will compile your code. You have been using C libraries all along when compiling your labs and projects this semester. However, those libraries are system libraries, and are found on all computers that contain a C compiler.

    A library is basically an archive of various functions that may be as part of a program's execution. Usually, these functions perform similar tasks or have some aspect about them that requires them to be kept in the same library. An example library would be the math library, where the most common mathematical formulas are gathered.

    For this lab, you will create a library containing the object code for all the "functionX()" functions you created earlier in the lab. There are two types of libraries used for compiling C programs, static and dynamic. For this lab, you will be creating a static library. The command used to create a static library is the ar (archive) command. Note the similarities between this command and the tar (tape archive) command.

    The ar command uses files containing object code (*.o files) to create a library. These object files have already been compiled, and therefore contain all the information necessary for the linker to use them to create an executable. When the linker creates the executable, it pulls the necessary machine (object) code out of the library, and incorporates it into the executable. To create a static library, the general command is:

        ar rc libsomelib.a objectfile1.o objectfile2.o objectfile3.o ...


    Read the manpage for ar for more information.

    By convention, static libraries always begin with the three letter "lib" prefix, and have a ".a" extension.

    You should already have compiled object files in the obj/ directory. You will use them to create a library named "liblab9.a"  which will be placed in the lib/ directory. Use the example ar command given above to create your library and place it in the lib/ directory. Note that you can use the *.o wildcard to use all object files in the obj/ directory. You can create this library directly in the lib/ directory by changing to the lib directory, and running the ar command, using a relative path to the object directory (../obj/*.o).

    After creating the library, you should run a separate command, ranlib, to create an index for your library. This is done with the command:

        ranlib

    Try running this command on the library you just created.

    Checking your library:


    Once you run ranlib on your library, you can use the nm command to list the index of functions contained in your library. To see if your library was created properly, use the command:

        nm liblab9.a

    It should list the functions that are currently contained in the library.

    Compiling an executable with your library:

    When you initially compiled the executable for lab 9, you placed all the *.o files on the gcc command line. Now that you have a library, you can just use it, along with any other specific source files to create an executable.

    Although you used a Makefile to compile your earlier executable, for this next step you will compile your program directly on the command line. You should now be familiar with the -I option for gcc, which lets the compiler know where some include (*.h) files may be found. You will now also use the -L option. This option works in a similar manner to -I, but instead lets the compiler know where some libraries (*.a files) may be found.

    Make sure that you are in the directory that contains all your bin/ src/ inc/ obj/ and lib/ directories. You will create a new executable named "lab9_from_lib." The only source file you will have in your gcc command will be Lab9.c. You will use the following command:

        gcc -o bin/lab9_from_lib -I./inc src/Lab9.c  -L./lib  -llab9

    Let's break this command down a bit:

    • -o    The output file
    • -I     The path to the directory containing some header files. This option usually comes before any source files are listed for the gcc command.
    • The source file being compiled
    • -L     The path to the directory containing libraries that will be used during linking
    • -l      The name of a library that will be used. Note that with the -l option, the three character "lib" as well as the ".a" extension are not included in the name.


    Why use a library anyway?

    There are many reasons why libraries are important to know about when creating large C programs. Libraries are easy, compact ways to give someone access to your source code. They are also a way to "modularize" portions of source code so that similar code is all found in one place.

     

    o   Directory structure

  • 相关阅读:
    node.js 入门笔记3
    Linux常用命令大全(非常全!!!)
    数据库导入导出命令(结构+数据)?
    列举 创建索引但是无法命中索引的8种情况。
    简述触发器、函数、视图、存储过程?
    什么是事务?MySQL如何支持事务?
    简述数据库三大范式
    MySQL常见数据库引擎及比较?
    列举常见的关系型数据库和非关系型都有那些?
    操作系统常见面试题
  • 原文地址:https://www.cnblogs.com/JasperZhao/p/12898253.html
Copyright © 2011-2022 走看看