GoogleTest CMake无法识别TEST_F:像它不能识别GTest的东西 [英] GoogleTest CMake doesn't recognize TEST_F: Like it's not recognizing GTest something
问题描述
好的,我承认,这是一个独特的情况。当我们构建我们的应用程序时,我们使用make,所以我把我的测试包含在src下的测试文件夹中。然后在与发布文件夹相同的级别,我们创建了一个单元测试文件夹,其中包括所有的源文件和我们的测试源文件。
OK, I admit it, this is a unique case. When we build our application we are using make, so I've included my tests in a test folder under src. Then at the same level as our release folder we have created a unit-test folder that includes all of our source files and our test source files.
但是我的IDE是CLion ,其中使用CMake。在我的CMakeLists.txt文件中,我包括:
But my IDE is CLion, which uses CMake. In my CMakeLists.txt file, I have included:
enable_testing()
find_package(GTest REQUIRED)
include_directories(${GTEST_INCLUDE_DIRS})
add_executable(TestProject ${SOURCE_FILES})
target_link_libraries(TestProject ${GTEST_BOTH_LIBRARIES})
我正在创建我的第一个测试夹具。下面是代码:
I am creating my first Test Fixture. Here is the code:
#include "OPProperties.h"
#include "gtest/gtest.h"
namespace {
// The fixture for testing class OPPropertiesTestTest.
class OPPropertiesTestTest : public ::testing::Test {
protected:
// You can remove any or all of the following functions if its body
// is empty.
OPPropertiesTestTest() {
// You can do set-up work for each test here.
}
virtual ~OPPropertiesTestTest() {
// You can do clean-up work that doesn't throw exceptions here.
}
// If the constructor and destructor are not enough for setting up
// and cleaning up each test, you can define the following methods:
virtual void SetUp() {
// Code here will be called immediately after the constructor (right
// before each test).
}
virtual void TearDown() {
// Code here will be called immediately after each test (right
// before the destructor).
}
// Objects declared here can be used by all tests in the test case for OPPropertiesTestTest.
};
TEST_F(OPPropertiesTestTest, ThisTestWillFail) {
EXPECT_EQ(0, 2);
}
} // namespace
int main(int argc, char **argv) {
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}
以下是拍摄的图片:
>
请注意我的TEST_F函数中的语法检查程序错误。当我开始键入TEST_F代码完成是试图找到一个Boost测试功能。
Notice the syntax checker errors in my TEST_F function. When I started to type TEST_F code completion is trying to find a Boost Test Function.
有人可以告诉我什么我需要添加到CMakeLists.txt文件或什么
Can someone tell me what else I need to add to the CMakeLists.txt file or what I am not doing that GTest functions are not being recognized?
推荐答案
正如πάνταῥεῖ指出的,我没有真正尝试过构建代码。当我第一次收到pthread的链接器错误,所以我们添加了以下行CMakeLists.txt文件:
As πάντα ῥεῖ pointed out, I hadn't actually tried to build the code. When I did I first received a linker error for pthread, so we added the following line the the CMakeLists.txt file:
target_link_libraries(OnePrint pthread)
然后我再次尝试构建并收到这些错误:
Then I tried again to build and received these errors:
/home/user/gtest-1.7.0/lib/.libs/libgtest.so: undefined reference to `pthread_key_create'
/home/user/gtest-1.7.0/lib/.libs/libgtest.so: undefined reference to `pthread_getspecific'
/home/user/gtest-1.7.0/lib/.libs/libgtest.so: undefined reference to `pthread_key_delete'
/home/user/gtest-1.7.0/lib/.libs/libgtest.so: undefined reference to `pthread_setspecific'
collect2: error: ld returned 1 exit status
So, I ran a search on these errors and found this question.
对我有用的答案是这里。
这篇关于GoogleTest CMake无法识别TEST_F:像它不能识别GTest的东西的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!