简体   繁体   English

使用Makefile对共享库进行单元测试

[英]Unit-testing shared library with Makefile

I'm writing a shared library, which I have written a unit-testing executable. 我正在编写一个共享库,我编写了一个单元测试可执行文件。 I would like to create a target in the Makefile which will initiate the the testing routine. 我想在Makefile中创建一个目标,它将启动测试例程。

The unit-testing executable is linked to the library that's created a moment ago. 单元测试可执行文件链接到刚刚创建的库。

When running the executable within Makefile, I get 在Makefile中运行可执行文件时,我得到了

./starttest: error while loading shared libraries: libllist.so: cannot open shared object file: No such file or directory

Which way is better to approach this issue ? 哪种方式更好地解决这个问题?

  1. defining LD_LIBRARY_PATH from within the Makefile like this ? 如下所示在Makefile中定义LD_LIBRARY_PATH

    export LD_LIBRARY_PATH=$(CURDIR)/lib export LD_LIBRARY_PATH = $(CURDIR)/ lib

  2. force the user to install the library before running the tests ? 强制用户在运行测试之前安装库?

  3. Don't start unit-testing from Makefile, the user will do it by himself. 不要从Makefile开始单元测试,用户可以自己完成。

or maybe I missed something and there's even a better way ? 或许我错过了一些东西,甚至还有更好的方法?

I would use option #1, if all you want is for the unit tests to be invoked through the makefile. 我会使用选项#1,如果你想要的是通过makefile调用单元测试。 Definitely option #2 should not be used, and I don't like option #3 much either. 绝对不应该使用选项#2,我也不喜欢选项#3。

A fourth alternative is to build your unit test program using the -rpath linker option to encode the location of the shared library into the unit test program. 第四种方法是使用-rpath链接器选项构建单元测试程序,以将共享库的位置编码到单元测试程序中。 For example if you're using GCC this would be: -Wl,-rpath=$(CURDIR) . 例如,如果您使用GCC -Wl,-rpath=$(CURDIR)-Wl,-rpath=$(CURDIR) This allows the unit test to be invoked from the command line, without needing LD_LIBRARY_PATH etc. to be set. 这允许从命令行调用单元测试,而不需要设置LD_LIBRARY_PATH等。

This isn't a good idea for final binaries (to encode your build directory into them) but it's fine for unit test binaries that you won't be using outside your build directory anyway. 对于最终的二进制文件(将构建目录编码到它们中)这不是一个好主意,但是对于单元测试二进制文件来说这很好,无论如何你都不会在构建目录之外使用它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM