配置要包含在 C++ 中的库测试

发布于 2024-08-26 16:11:29 字数 777 浏览 6 评论 0原文

我想在测试文件中使用 UnitTest++ 库。但是,我在编译时包含该库时遇到一些困难。所以这是我当前的目录结构:

tests/
  UnitTests++/
    libUnitTest++.a
    src/
      UnitTests++.h
  unit/
    test.cpp

我刚刚使用了 UnitTest++ 入门 指南只需进行库设置即可。这是 test.cpp:

// test.cpp
#include <UnitTest++.h>

TEST(FailSpectacularly)
{
 CHECK(false);
}

int main()
{
 return UnitTest::RunAllTests();
}

我目前正在尝试编译:

gcc -lUnitTest++ -L../UnitTest++/ -I../UnitTest++/src/ test.cpp

我目前得到一堆输出,最后带有 ld: symbol(s) not find 。那么,在编译该程序时,如何才能正确包含 UnitTest++ 库呢?我使用的是 Mac,并且我还希望 Linux 机器上的人们能够有一种简单的方法来运行这些相同的测试。

哇,我希望这提供了足够的信息,如果没有,请告诉我。

I would like to utilize the UnitTest++ library in a testing file. However, I am having some difficulty getting the library to be included at compile time. So here is my current directory structure:

tests/
  UnitTests++/
    libUnitTest++.a
    src/
      UnitTests++.h
  unit/
    test.cpp

I have just used the UnitTest++ getting started guide to just get the library setup. Here is test.cpp:

// test.cpp
#include <UnitTest++.h>

TEST(FailSpectacularly)
{
 CHECK(false);
}

int main()
{
 return UnitTest::RunAllTests();
}

And I am currently trying to compile with:

gcc -lUnitTest++ -L../UnitTest++/ -I../UnitTest++/src/ test.cpp

I am currently getting a bunch output with ld: symbol(s) not found at the end. So how would I be able to get the UnitTest++ library properly included when this program is compiled? I am on a Mac and I'd also like for there to be an easy way for people on a Linux machine to run these same tests.

Whew, I hope this provides enough information, if not please let me know.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

疏忽 2024-09-02 16:11:29

我能够通过以下方式构建它

gcc -L../UnitTest++/ -I../UnitTest++/src/ test.cpp -lUnitTest++ -lstdc++

g++ -L../UnitTest++/ -I../UnitTest++/src/ test.cpp -lUnitTest++

自动链接到 libstdc++。

海湾合作委员会文档说:

-l库

-l 库

链接时搜索名为library的库。 (将库作为单独参数的第二种选择仅用于 POSIX 合规性,不推荐。)

在命令中写入此选项的位置会有所不同;链接器按照指定的顺序搜索和处理库和目标文件。

因此,foo.o -lz bar.o' 在文件 foo.o 之后但在 bar.o 之前搜索库z'。如果 bar.o 引用 `z' 中的函数,则可能不会加载这些函数。

我想这就是为什么当您第一次指定 -lUnitTest++ 然后 test.cpp 时找不到库符号的原因

I was able to build it in the following manner

gcc -L../UnitTest++/ -I../UnitTest++/src/ test.cpp -lUnitTest++ -lstdc++

or

g++ -L../UnitTest++/ -I../UnitTest++/src/ test.cpp -lUnitTest++

that links to libstdc++ automatically.

GCC documentation says:

-llibrary

-l library

Search the library named library when linking. (The second alternative with the library as a separate argument is only for POSIX compliance and is not recommended.)

It makes a difference where in the command you write this option; the linker searches and processes libraries and object files in the order they are specified.

Thus, foo.o -lz bar.o' searches libraryz' after file foo.o but before bar.o. If bar.o refers to functions in `z', those functions may not be loaded.

I guess that's why the library symbols are not found when you first specify -lUnitTest++ and then test.cpp

你如我软肋 2024-09-02 16:11:29

编译 test.cpp 得到 test.o

并使用

g++ test.o libUnitTest++.a -o ./exectest

得到 ./exectest 可执行文件

libUnitTest++.a 只是 UnitTest++ 所有目标文件的存档。您只需要链接所有目标文件(您的测试目标文件+ libUnitTest++.a)

尝试编辑unittest++附带的makefile并使其适合您的情况

Compile test.cpp to get test.o

and use

g++ test.o libUnitTest++.a -o ./exectest

to get the ./exectest executable

libUnitTest++.a is just an archive of all the object files of UnitTest++. You just need to link all the object files (your test object file + libUnitTest++.a)

Try editing the makefile that came with unittest++ and make it suitable for your case

瀞厅☆埖开 2024-09-02 16:11:29

消息ld:未找到符号表示您尚未编译该库。所以你需要进入UnitTest++文件夹,编译并安装它。

我从来没有在 MAC 上工作过,但在 Linux 中,库通常是通过以下方式编译和安装的:

./configure
make
make install

在您发布的 UnitTest++ 链接中,您应该简单地:

make install

然后您将在操作系统的库文件夹中拥有 UnitTest++.so 库。现在可以使用 -lUnitTest++ 命令将该库与您的程序链接。

The message ld: symbol(s) not found means you haven't compiled the library. So you need to go in the UnitTest++ folder, compile and install it.

I've never worked on a MAC, but in linux, libraries are usually compiled and installed with:

./configure
make
make install

In the UnitTest++ link you posted, you should simply:

make install

Then you will have the UnitTest++.so library in the libraries folder of your OS. Now the library can be linked with your program with the -lUnitTest++ command.

趁微风不噪 2024-09-02 16:11:29

通常你必须把 -Lsomething 放在需要它的 -lsomething 之前。

Usually you have to put -Lsomething before the -lsomething that requires it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文