Thread: GCC Linking against static library

  1. #1
    Matt Conway bobthebullet990's Avatar
    Join Date
    Nov 2005

    GCC Linking against static library

    I'm looking at setting up a simple development environment for an embedded project.

    I want to write unit tests as I code, so I have decided to go with two trees:

    /proj/target (contains project code and test files)

    /proj/hosted (contains makefiles that specify rules to build for host environment)

    Now lets say I develop a library, in this case call it math:

    /* Add two numbers */
    int addition(int x, int y);
    /* Subtract two numbers */
    int subtraction(int x, int y);
    #include "math.h"
    int addition(int x, int y)
    	return x + y;
    int subtraction(int x, int y)
    	return x - y;

    And now I want to create a hosted build of this on the "hosted" tree, so I have a runner that will eventually run some unit tests:

    #include <stdio.h>
    #include <math.h>
    int main(void)
    	printf("addtion(3,6)=%d\n", addition(3,6));
    	printf("subtraction(8,3)=%d\n", subtraction(8,3));
    And then a makefile that I want to:

    1. Compile the math source into a static library.
    2. Compile the runner and link against the math library.

    The makefile looks like this...

    LIB_BASE = /proj/target/lib/
    LIB_NAME = math
    MATH_SOURCES = $(abspath $(wildcard $(LIB_BASE)$(LIB_NAME)/*.c))
    	# Compile the library code, put the built library in current directory.
    	gcc -c $(MATH_SOURCES) -o ${CURDIR}/$(LIB_NAME).o
    	# Compile the runner, link against the math lib.
    	gcc runner.c -I$(LIB_BASE)$(LIB_NAME) -L$(CURDIR) -lmath -o runner

    Library math is compiled into the local directory, then runner is compiled linking against the math library, however, something is wrong at the linking stage; when running make it can't find the math library:

    [user@localhost math]$ make
    # Compile the library code, put the built library in current directory.
    gcc -c /proj/target/lib/math/math.c -o /proj/hosted/lib/math/math.o
    # Compile the runner, link against the math lib.
    gcc runner.c -I/proj/target/lib/math -L/proj/hosted/lib/math -lmath -o runner
    /usr/bin/ld: cannot find -lmath
    collect2: error: ld returned 1 exit status
    makefile:7: recipe for target 'default' failed
    make: *** [default] Error 1
    Any ideas what I'm doing wrong/missing?
    Last edited by bobthebullet990; 08-27-2015 at 06:46 AM.
    Many junglists take pride in their belongin to what may be referred to as a globalised drum & bass subculture, as a subculture though, it is not nearly as distinct at gothic or punk!

  2. #2
    Registered User
    Join Date
    Sep 2007
    First, it's probably a bad idea to name a header “math.h”, since that's a standard header name. You're already accidentally including the wrong header in runner.c when you use <> instead of "" to surround the header name. But that's not the real issue.

    The real issue is that you're just compiling math.c into math.o and calling that a library, but it's not. It's an object file. It can be turned into a static library with ar:

    ar cru libmath.a math.o
    When linking, if you do “-lfoo”, gcc will look for “” and then “libfoo.a”. If neither of those exists, it won't search elsewhere, such as for “foo.o”.

  3. #3
    Registered User
    Join Date
    Mar 2014
    Corning, New York, USA
    Cas is right here. If you were making a shared library, you wouldn't have to use the ar command I believe. But because you're creating a static library, you need to use the ar command to create the library. The c option means create archive
    The r option means to replace the files in the archive, ie, insert them, regardless of whether they're already in there or not. And the u option affects the r option in a way. I believe the u option updates the files, ie, it only adds them if they're newer than the ones that are already added into the archive.

    I can't remember the reason, but I want to say I remember something about having to run ranlib too or something.
    Last edited by Spork Schivago; 08-27-2015 at 06:35 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. static library linking
    By jcfuller in forum C Programming
    Replies: 4
    Last Post: 10-07-2014, 10:43 AM
  2. User-Defined Static Library Not Linking Against Program
    By QuadraticFighte in forum C Programming
    Replies: 2
    Last Post: 10-15-2010, 02:10 PM
  3. LNK2019 when linking against a static library.
    By g4j31a5 in forum C++ Programming
    Replies: 4
    Last Post: 06-29-2010, 01:39 PM
  4. Help building/linking to static library
    By Kernel Sanders in forum C++ Programming
    Replies: 19
    Last Post: 08-17-2008, 04:35 PM
  5. Replies: 4
    Last Post: 07-06-2006, 02:53 AM