Beginner's question on misc functions and header files

This is a discussion on Beginner's question on misc functions and header files within the C Programming forums, part of the General Programming Boards category; Just a simple question if you please: Lets say that over the years in programming in C you wrote a ...

  1. #1
    Registered User
    Join Date
    Jan 2013
    Posts
    1

    Beginner's question on misc functions and header files

    Just a simple question if you please:

    Lets say that over the years in programming in C you wrote a lot of helper functions and macros to for instance allocate memory or do a safer strcmp or define constants, etc.

    Do you guys normally keep all these functions in one library and one header file that you include in all your programs or do you rather keep a gazillion smaller libraries and header files that have very specific functions in it to be able to include only what is needed?


    Thanks!

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,258
    You can always have many specific header files, and then have a convenience header that includes the other header files.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Nov 2012
    Posts
    1,220
    At least with GCC, if you compile each independent function into it's own object file, GCC will make sure that only what is actually used will end up in the resulting binary. For example, suppose you define these custom functions

    int myfunc1(int myparam);
    int myfunc2(int myparam);
    int myfunc3(int myparam);

    Then, you can compile each function definition with gcc's "-c" flag to produce three separate object files, for example

    myfunc1.o
    myfunc2.o
    myfunc3.o

    Now, let's say you make a program called myprogram.c, which makes use of 0 or more of these functions. Compiling myprogram with the command

    gcc -o myprogram myprogram.c myfunc1.o myfunc2.o myfunc3.o

    will automatically leave out the .o files which are not actually used by myprogram. The list of object files may get long, so normally you combine them all into an "archive file" named libfoo.a and then link it using the -lfoo flag:

    gcc -o myprogram myprogram.c -lmyfunctions

    GCC makes the assumption in this case that you have an archive file named "libmyfunctions.a" somewhere in your library search path, and the result should be the same as in listing the .o files. If you're not using GCC, a similar system is used by other compilers, but the details vary from compiler to compiler.

  4. #4
    Registered User
    Join Date
    Nov 2012
    Posts
    1,220
    A correction to the last post:

    GCC will perform the optimization described (removing instances of uncalled functions myfunc1, myfunc2 and myfunc3) only if compiled with the following procedure or its equivalent:

    gcc -fdata-sections -ffunction-sections -c myfunc1.o -Wl,--gc-sections
    gcc -fdata-sections -ffunction-sections -c myfunc2.o -Wl,--gc-sections
    gcc -fdata-sections -ffunction-sections -c myfunc3.o -Wl,--gc-sections
    # (Now combine all of your .o files into an archive file such as libmyfunctions.a)
    gcc -fdata-sections -ffunction-sections -o myprogram myprogram.c -lmyfunctions -Wl,--gc-sections

    This will allow the linker to discover whether myfunc1, myfunc2 and myfunc3 are unused in myprogram and leave them off of the resulting binary, myprogram.

    Unfortunately, this doesn't work in the standard MinGW linker. However, there is a patch available which solves it, here: Bug 11539 – ld --gc-sections should work for PE-COFF on MinGW

    Comments on that page report savings of several megabytes on programs which are linking against large static libraries such as Qt, so this patch could be interesting if the static library you are linking against gets large.

  5. #5
    Registered User
    Join Date
    Nov 2012
    Posts
    1,220
    EDIT (Clarifying the commands):

    The linker options are only needed for the last step, and the -f options are not needed in the last step. Simplified example commands:

    gcc -fdata-sections -ffunction-sections -c myfunc1.o
    gcc -fdata-sections -ffunction-sections -c myfunc2.o
    gcc -fdata-sections -ffunction-sections -c myfunc3.o
    ar r libmyfunctions.a myfunc1.o myfunc2.o myfunc3.o
    gcc -L. -o myprogram myprogram.c -lmyfunctions -Wl,--gc-sections

    The option -L. says to look for libmyfunctions.a in the current directory. This is not needed if you put libmyfunctions.a into a directory which is specified in your LIBRARY_PATH.

    Also, using an archive file with each object file containing a single function seems the best way for GCC. In this mode, MinGW with the standard linker will not link the unneeded object files if you didn't actually use them in your program.

  6. #6
    Stoned Witch Barney McGrew's Avatar
    Join Date
    Oct 2012
    Location
    astaylea
    Posts
    420
    If the function definitions are small and depend on only a few auxiliary functions, I keep them in a C file with a bunch of other related functions, then just copy and paste the functions I need into the projects I'm working on. If it's an entire library of functions that are used together, then I keep one or more C files along with a header file and copy them into the project's directory. If it's a large library consisting of many C and C header files I'll usually maintain that as a separate project and build a shared, dynamically linked library.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 09-10-2010, 03:40 PM
  2. [VERY Beginner question] Strings + functions + ...pointers?!
    By arthurhess in forum C++ Programming
    Replies: 4
    Last Post: 12-08-2009, 02:44 PM
  3. Header files question
    By Programmer_P in forum C++ Programming
    Replies: 8
    Last Post: 05-14-2009, 02:16 PM
  4. Beginner's question about functions.
    By Crocodile23 in forum C Programming
    Replies: 4
    Last Post: 01-13-2009, 07:00 AM
  5. question about header files
    By ArseMan in forum C++ Programming
    Replies: 2
    Last Post: 09-21-2001, 03:33 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21