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:
/proj/target/lib/math/math.h
Code:
/* Add two numbers */
int addition(int x, int y);
/* Subtract two numbers */
int subtraction(int x, int y);
/proj/target/lib/math/math.c
Code:
#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:
/proj/hosted/lib/math/runner.c
Code:
#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...
/proj/hosted/lib/math/makefile
Code:
LIB_BASE = /proj/target/lib/
LIB_NAME = math
MATH_SOURCES = $(abspath $(wildcard $(LIB_BASE)$(LIB_NAME)/*.c))
default:
# 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:
Code:
[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?