链接GoogleTest和CodeBlocks [英] Linking GoogleTest and CodeBlocks

查看:109
本文介绍了链接GoogleTest和CodeBlocks的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

恐怕这是一个基本问题,我可能已经找到了其他IDE的答案,但是没有CodeBlocks =(

I'm afraid that it is a basic problem, I might have found the answer with other IDEs but not with CodeBlocks =(

我尝试使用GoogleTest。我下载了该项目,并使用cmake -G CodeBlocks-Unix Makefiles(我在Ubuntu上)构建了该项目。

I try to use GoogleTest. I downloaded the project, built it with cmake -G "CodeBlocks - Unix Makefiles" (I'm on Ubuntu)

这给了我libgtest.a和libgtest_main.a我在/ usr / lib

That gave me the libgtest.a and libgtest_main.a files that I transfered in /usr/lib

我认为我的不足是在最后一步:链接GoogleTest和CodeBlocks。一些教程要求添加 -lgtest链接器选项,我尝试了很多事情,但是都一样:当我尝试编译我的#include gtest / gtest.h时,返回致命错误没有这样的文件或目录。

And I think my lacks are on the last step : linking GoogleTest and CodeBlocks. Some tutorials told to add the "-lgtest" linker option, I tried many things but it's all the same : when I try to compile my #include "gtest/gtest.h" returns the fatal error "No such file or directory".

您能帮助我可怜的灵魂吗?

Could you help my poor soul ?

推荐答案

不要将自己构建的文件放在<$以下c $ c> / usr /...
,除非您将它们放在 / usr / local /...下。 / usr /...
的内容由您的Linux发行版保留,应由您的软件包专门填充年龄经理。如果您在包裹管理者的背后弄乱了它,
可能会破坏包裹并陷入任何痛苦。

Don't put files that you have built yourself under /usr/... unless you put them under /usr/local/.... The rest of /usr/... is reserved by your Linux distro and should be exclusively populated by your package manager. If you mess about in it behind the package manager's back, you risk breaking your packages and getting into any amount of pain.

首先删除 libgtest.a libgtest_main.a 来自 / usr / lib

您已经下载了 googletest ,并由第一个
运行并将其构建在某个目录中CMake 在该目录中,然后运行 make

You've downloaded googletest and built it in some directory by first running CMake in that directory and then running make.

运行 make 的同一目录,运行:

Having done that, in the same directory where you ran make, run:

sudo make install

这将安装所有 gmock / <$ / usr / local / lib 中的c $ c> gtest 库。
它将在 / usr / local / include / gmock 中安装 gmock 头文件并安装
/ usr / local / include / gtest 中的 gtest 头文件。到那里去检查。

This will install all the gmock / gtest libraries in /usr/local/lib. It will install the gmock header files in /usr/local/include/gmock and install the gtest header files in /usr/local/include/gtest. Go there and check.

/ usr / local /...属于 you ,并且您在该处执行的任何操作都不会干扰
与程序包管理器。

/usr/local/... belongs to you and whatever you do there will not interfere with the package manager.

下一步,使用 googletest 在单元测试程序中,您可能需要执行以下
6项操作:

Next, to use googletest in your unit-testing program, you might need to do the following 6 things:

1: #include< gtest / gtest.h> (适用于您的源文件)。 (请注意:
< gtest / gtest.h> ,没有什么不同)。

1: #include <gtest/gtest.h> where applicable in your source files. (Note: <gtest/gtest.h>, and not anything different).

2:告诉编译器哪个目录包含 gtest / gtest.h ,除非
是编译器的默认搜索目录之一。为此,您需要
将选项 -I / usr / local / include 传递给 g ++ 编译源文件。但是您不需要
这样做,因为 / usr / local / include 编译器的默认搜索$ b之一$ b目录。

2: Tell the compiler what directory contains gtest/gtest.h, unless it's one of the compiler's default search directories. To do that, you would pass the option -I/usr/local/include to g++ for compiling the source files. But you don't need to do that, because /usr/local/include is one of the compiler's default search directories.

3::告诉链接器您要链接 libgtest.a 。为此,您
将选项 -lgtest 传递给 g ++ 来链接程序。

3: Tell the linker that you want to link libgtest.a. To do that that, you pass the option -lgtest to g++ for linking the program.

4:告诉链接器哪个目录包含 libgest.a ,除非
它是链接器的默认搜索目录之一。为此,您需要
将选项 -L / usr / local / lib 传递给 g ++ 链接程序。但是您不需要
这样做,因为 / usr / local / lib 链接程序的默认搜索$ b之一$ b目录。

4: Tell the linker what directory contains libgest.a, unless it's one of the linker's default search directories. To do that, you would pass the option -L/usr/local/lib to g++ for linking the program. But you don't need to do that, because /usr/local/lib is one of the linker's default search directories.

5:告诉编译器生成线程安全代码。 (为什么?因为默认情况下,
libgest 是多线程库。如果您需要
,则可以将其构建为单线程,但是我假设为此,请将选项 -pthread
传递给 g ++ 进行编译

5: Tell the compiler to generate thread-safe code. (Why? Because by default, libgest is a multi-threaded library. You can build it single-threaded if you want, but I assume you didn't.) To do that, you pass the option -pthread to g++ for compiling source files.

6 :告诉链接器链接多线程可执行文件。为此,将选项
-pthread 传递给 g ++ 来链接程序。 (是的, -pthread 用于编译,
-pthread 再次用于链接)。

6: Tell the linker to link a multi-threading executable. To do that, you pass the option -pthread to g++ for linking the program. (Yes, -pthread for compiling and -pthread again for linking).

因此,实际上,您只需要执行 1 3 5 6

So in practice you just need to do 1, 3, 5 and 6.

1 无需进一步说明。其余的要求您正确设置Code :: Blocks项目
构建选项,以便它们生成正确的 g ++ ... 用于编译和
链接的命令。

1 needs no futher explanation. The rest require you to set up your Code::Blocks project Build options correctly, so that they generate the correct g++ ... commands for compiling and linking.

似乎您已经正确使用 3 :在 Build中选项... -> 链接器设置->
其他链接器选项,添加以下行: -lgtest

It looks as if you've already got 3 right: In Build options... -> Linker settings -> Other linker options, add the line: -lgtest

对于 5 ,在 Build options ... -> 编译器设置中-> 其他编译器选项,添加以下行: -pthread

For 5, in Build options... -> Compiler settings -> Other compiler options, add the line: -pthread

对于 6 ,在构建选项... -> 链接器设置-> 其他链接器选项中,添加另一个行: -pthread ,在带有 -lgtest 的那个之后的

For 6, in Build options... -> Linker settings -> Other linker options, add another line: -pthread, after the one with -lgtest.

仅此而已。保存这些设置后,即可构建项目。如果有错误,您可以查看 Build log 标签下的
构建日志(而不是 Build messages 标签下),以确切地了解 g ++ ... 命令
被执行以进行编译和链接,并且如果您的设置为 g ++ 提供了正确的命令行选项。

That's all. When you've saved those settings you can build your project. If there are errors, youi can look at the build log under the Build log tab (not the Build messages tab) to see exactly what g++ ... commands got executed for compiling and linking, and if your settings produced the correct commandline options for g++.

这篇关于链接GoogleTest和CodeBlocks的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆