views:

29

answers:

2

So I'm writing tests for my code, and I want to stub out the calls to library functions (make sure that it's calling the right library calls at the right time, and that it handles errors appropriately).

I think I'm SOL with the C standard library functions, but those aren't the only libraries I'm using.

When building my final executable (not my test executable), I want to use -lfuse, so I included this line in my configure.ac

AC_CHECK_LIB([fuse], [fuse_main])

However, this also tosses in -lfuse when it tries to build my check_PROGRAMS.

Is there some way I can tell autotools that I don't want the -lfuse library when building my test executable (make check)? Then I should be able to stub out the library calls as I wish, since there won't be anything else linked with the same name.

A: 

Ok, I think I figured out a solution. I'm not convinced it's the right solution, so I'll be checking back here to see if anyone can come up with anything better.

If I change the default success/failure actions in my configure.ac to

 AC_SUBST([HAVE_LIBFUSE])
 AC_CHECK_LIB([fuse], [fuse_main], [HAVE_LIBFUSE=1])

And then manually add -lfuse to my <appname>_LDADD flags in src/Makefile.am, then it's included just when I make the main executable, and I'm free to stub out the functions in my tests.

At least I think I am, and this is enough for me to move forward for now.

rampion
+1  A: 

I see that libfuse supplies a fuse.pc file in its source distribution, so the proper way to check for it is to use pkg-config. You can do

PKG_CHECK_MODULES([APPNAME], [fuse ...and any other libraries to check for...])

in your configure.ac, and then

appname_CFLAGS += @APPNAME_CFLAGS@
appname_LIBS += @APPNAME_LIBS@

in src/Makefile.am. Then just don't add those variables to your test programs.

ptomato
Thanks. So I can choose the first argument to PKG_CHECK_MODULES to be some arbitrary string, and it defines `<ARBITRARYSTRING>_CFLAGS` etc? That's handy.
rampion
That's right. For more info, see `man pkg-config`.
ptomato