GoogleTest CMake不承认TEST_F:就像它不承认GTest的东西

好吧,我承认,这是一个独特的例子。 当我们构build我们的应用程序时,我们正在使用make,所以我将testing包括在src下的一个testing文件夹中。 然后在与我们的发布文件夹相同的级别上,我们创build了一个包含我们所有源文件和testing源文件的unit testing文件夹。

但是我的IDE是使用CMake的CLion。 在我的CMakeLists.txt文件中,我包括:

enable_testing() find_package(GTest REQUIRED) include_directories(${GTEST_INCLUDE_DIRS}) add_executable(TestProject ${SOURCE_FILES}) target_link_libraries(TestProject ${GTEST_BOTH_LIBRARIES}) 

我正在创build我的第一个testing夹具。 这里是代码:

 #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代码完成时,试图find一个升压testingfunction。

有人可以告诉我还有什么我需要添加到CMakeLists.txt文件,或者我没有做什么GTestfunction不被识别?

正如πτνταῥεῖ指出的那样,我实际上并没有尝试构建代码。 当我第一次接收到pthread的链接器错误时,所以我们在CMakeLists.txt文件中添加了以下行:

 target_link_libraries(OnePrint pthread) 

然后我再次尝试构建并收到这些错误:

 /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 

所以,我搜索了这些错误,发现了这个问题 。

对我有效的答案就在这里 。