Cmake Error during gtest compilation - gtest/internal/gtest-type-util.h:732:25: error: expected type-specifier

Cmake Error during gtest compilation - gtest/internal/gtest-type-util.h:732:25: error: expected type-specifier

我在尝试使用 cmake 构建项目时遇到错误。
我在我的电脑上手动下载并安装了 gtest(即 gtest 头文件在 /usr/include 中可用)和库,例如 gtest,gtest_main 在 /usr/lib)

下面是我的项目主要功能的代码。 (lte_softmodem_test.cpp)

#include <gtest/gtest.h>
#include <iostream>

using namespace std;

TEST(sample, sample2){

}


int main(int argc, char **argv)
{
    cout << "This is test" << endl;
    testing::InitGoogleTest(&argc, argv);
    return RUN_ALL_TESTS();
 return 0;
}

当我使用下面的命令单独编译这个文件时,它工作正常!!

g++ eNB_scheduler_test.cpp -L /usr/lib/ -lgtest -lgtest_main -pthread -std=c++11

但是,当我尝试将其包含在我的项目 CMake 文件中时,它向我显示错误。

CMake 文件:(CMake 文件有点大,所以我只包含了几行我试图编译这个文件的地方)

# lte-softmodem is both eNB and UE implementation
###################################################

add_executable(lte-softmodem
  ${OPENAIR_TARGETS}/RT/USER/rt_wrapper.c
  ${OPENAIR_TARGETS}/RT/USER/lte-enb.c
  ${OPENAIR_TARGETS}/RT/USER/lte-ru.c
  ${OPENAIR_TARGETS}/RT/USER/ru_control.c
  ${OPENAIR_TARGETS}/RT/USER/lte-softmodem.c 
  ${OPENAIR_TARGETS}/RT/USER/lte-softmodem_test.cpp
  ${OPENAIR_TARGETS}/RT/USER/lte-softmodem-common.c
  ${OPENAIR2_DIR}/ENB_APP/NB_IoT_interface.c
  ${OPENAIR1_DIR}/SIMULATION/TOOLS/taus.c
  ${OPENAIR_TARGETS}/COMMON/create_tasks.c
  ${OPENAIR_TARGETS}/COMMON/create_tasks_mbms.c
  ${OPENAIR_TARGETS}/ARCH/COMMON/common_lib.c
  ${OPENAIR2_DIR}/RRC/NAS/nas_config.c
  ${OPENAIR2_DIR}/RRC/NAS/rb_config.c
  ${OPENAIR1_DIR}/SIMULATION/ETH_TRANSPORT/netlink_init.c
  ${OPENAIR1_DIR}/SIMULATION/ETH_TRANSPORT/multicast_link.c
  ${OPENAIR1_DIR}/SIMULATION/ETH_TRANSPORT/socket.c
  ${OPENAIR3_DIR}/NAS/UE/nas_ue_task.c
  ${OPENAIR_DIR}/common/utils/utils.c
  ${OPENAIR_DIR}/common/utils/system.c
  ${GTPU_need_ITTI}
  ${XFORMSINTERFACE_SOURCE}
  ${T_SOURCE}
  ${CONFIG_SOURCES}
  ${SHLIB_LOADER_SOURCES}

add_dependencies(lte-softmodem rrc_flag s1ap_flag x2_flag)



target_link_libraries (lte-softmodem
  -Wl,--start-group
  RRC_LIB S1AP_LIB S1AP_ENB F1AP_LIB F1AP M2AP_LIB M2AP_ENB X2AP_LIB X2AP_ENB M3AP_LIB M3AP_ENB GTPV1U SECU_CN SECU_OSA UTIL HASHTABLE SCTP_CLIENT MME_APP UDP SCHED_LIB SCHED_RU_LIB PHY_COMMON PHY PHY_RU LFDS L2
  ${MSC_LIB} ${RAL_LIB} ${NAS_UE_LIB} ${ITTI_LIB} ${FLPT_MSG_LIB} ${ASYNC_IF_LIB} ${FLEXRAN_AGENT_LIB} ${FSPT_MSG_LIB} ${PROTO_AGENT_LIB} LFDS7
  NFAPI_COMMON_LIB NFAPI_LIB NFAPI_VNF_LIB NFAPI_PNF_LIB NFAPI_USER_LIB
  -Wl,--end-group z dl gtest gtest-main -lpthread)

CMake 编译文件时以详细模式输出 lte_softmodem_test.cpp:

/usr/bin/c++ -DASN1_MINIMUM_VERSION=924 -DCMAKE_BUILD_TYPE="RelWithDebInfo" -DDRIVER2013 -DENABLE_ITTI -DENABLE_NAS_UE_LOGGING -DENABLE_USE_CPU_EXECUTION_TIME -DENABLE_USE_MME -DENABLE_VCD -DENB_MODE -DEXMIMO_IOT -DF1AP_RELEASE=R15 -DF1AP_VERSION=3873 -DFIRMWARE_VERSION=""No svn information"" -DFLPT_VERSION=V2 -DFSPT_VERSION=V2 -DJUMBO_FRAME -DLTE_RRC_VERSION=3696 -DM2AP_RELEASE=R14 -DM2AP_VERSION=3584 -DM3AP_RELEASE=R14 -DM3AP_VERSION=3584 -DMAX_NUM_CCs=1 -DNAS_BUILT_IN_UE -DNAS_UE -DNB_ANTENNAS_RX=2 -DNB_ANTENNAS_TX=4 -DNETTLE_VERSION_MAJOR=3 -DNETTLE_VERSION_MINOR=2 -DNO_RRM -DNUMBER_OF_UE_MAX_NB_IoT=16 -DNone=1 -DOAI_NW_DRIVER_USE_NETLINK -DOPENAIR2 -DOPENAIR_LTE -DPACKAGE_BUGREPORT="openair4g-devel@lists.eurecom.fr" -DPACKAGE_NAME="" -DPACKAGE_VERSION=""Branch: master Abrev. Hash: 9a06ceb Date: Wed Sep 9 16:15:50 2020 +0200"" -DPHYSIM -DPHY_CONTEXT -DRel14=1 -DS1AP_RELEASE=R14 -DS1AP_VERSION=3664 -DTRACE_RLC_MUTEX -DT_TRACER -DX2AP_RELEASE=R14 -DX2AP_VERSION=3680 -I/root/openairinterface5g/cmake_targets/lte_build_oai/build/CMakeFiles/RRC_Rel14 -I/root/openairinterface5g/cmake_targets/lte_build_oai/build/CMakeFiles/S1AP_R14 -I/root/openairinterface5g/openair3/S1AP -I/root/openairinterface5g/cmake_targets/lte_build_oai/build/CMakeFiles/M2AP_R14 -I/root/openairinterface5g/openair2/M2AP -I/root/openairinterface5g/cmake_targets/lte_build_oai/build/CMakeFiles/M3AP_R14 -I/root/openairinterface5g/openair3/M3AP -I/root/openairinterface5g/cmake_targets/lte_build_oai/build/CMakeFiles/X2AP_R14 -I/root/openairinterface5g/openair2/X2AP -I/root/openairinterface5g/cmake_targets/lte_build_oai/build/CMakeFiles/F1AP_R15.2.1 -I/root/openairinterface5g/openair2/F1AP -I/root/openairinterface5g/targets/ARCH/USRP/USERSPACE/LIB -I/root/openairinterface5g/targets/ARCH/BLADERF/USERSPACE/LIB -I/root/openairinterface5g/targets/ARCH/LMSSDR/USERSPACE/LIB -I/root/openairinterface5g/targets/ARCH/ETHERNET/USERSPACE/LIB -I/root/openairinterface5g/targets/ARCH/IRIS/USERSPACE/LIB -I/root/openairinterface5g/targets/ARCH/COMMON -I/root/openairinterface5g/cmake_targets/lte_build_oai/build/CMakeFiles -I/root/openairinterface5g/openair2/COMMON -I/root/openairinterface5g/openair2/UTIL -I/root/openairinterface5g/openair2/UTIL/LOG -I/root/openairinterface5g/openair3/COMMON -I/root/openairinterface5g/openair3/UTILS -I/root/openairinterface5g/nfapi/open-nFAPI/nfapi/public_inc -I/root/openairinterface5g/nfapi/open-nFAPI/common/public_inc -I/root/openairinterface5g/nfapi/open-nFAPI/pnf/public_inc -I/root/openairinterface5g/nfapi/open-nFAPI/nfapi/inc -I/root/openairinterface5g/nfapi/open-nFAPI/sim_common/inc -I/root/openairinterface5g/nfapi/open-nFAPI/pnf_sim/inc -I/root/openairinterface5g/openair1 -I/root/openairinterface5g/openair2 -I/root/openairinterface5g/openair3/NAS/TOOLS -I/root/openairinterface5g/openair2/ENB_APP -I/root/openairinterface5g/openair2/MCE_APP -I/root/openairinterface5g/openair2/LAYER2/RLC -I/root/openairinterface5g/openair2/LAYER2/RLC/AM_v9.3.0 -I/root/openairinterface5g/openair2/LAYER2/RLC/UM_v9.3.0 -I/root/openairinterface5g/openair2/LAYER2/RLC/TM_v9.3.0 -I/root/openairinterface5g/openair2/LAYER2/PDCP_v10.1.0 -I/root/openairinterface5g/openair2/RRC/LTE/MESSAGES -I/root/openairinterface5g/openair2/RRC/LTE -I/root/openairinterface5g/common/utils -I/root/openairinterface5g/common/utils/ocp_itti -I/root/openairinterface5g/openair3/NAS/COMMON -I/root/openairinterface5g/openair3/NAS/COMMON/API/NETWORK -I/root/openairinterface5g/openair3/NAS/COMMON/EMM/MSG -I/root/openairinterface5g/openair3/NAS/COMMON/ESM/MSG -I/root/openairinterface5g/openair3/NAS/UE/ESM -I/root/openairinterface5g/openair3/NAS/UE/EMM -I/root/openairinterface5g/openair3/NAS/UE/API/USER -I/root/openairinterface5g/openair3/NAS/COMMON/IES -I/root/openairinterface5g/openair3/NAS/COMMON/UTIL -I/root/openairinterface5g/openair3/SECU -I/root/openairinterface5g/openair3/SCTP -I/root/openairinterface5g/openair3/UDP -I/root/openairinterface5g/openair3/GTPV1-U -I/root/openairinterface5g/openair3/MME_APP -I/root/openairinterface5g/targets/COMMON -I/root/openairinterface5g/openair2/ENB_APP/CONTROL_MODULES/PHY -I/root/openairinterface5g/openair2/ENB_APP/CONTROL_MODULES/MAC -I/root/openairinterface5g/openair2/ENB_APP/CONTROL_MODULES/RRC -I/root/openairinterface5g/openair2/ENB_APP/CONTROL_MODULES/PDCP -I/root/openairinterface5g/openair2/UTIL/OSA -I/root/openairinterface5g/openair2/UTIL/LFDS/liblfds6.1.1/liblfds611/inc -I/root/openairinterface5g/openair2/UTIL/LFDS/liblfds7.0.0/liblfds700/inc -I/root/openairinterface5g/openair2/LAYER2/PROTO_AGENT -I/root/openairinterface5g/openair2/UTIL/MEM -I/root/openairinterface5g/openair2/UTIL/LISTS -I/root/openairinterface5g/openair2/UTIL/FIFO -I/root/openairinterface5g/openair2/UTIL/OCG -I/root/openairinterface5g/openair2/UTIL/MATH -I/root/openairinterface5g/openair2/UTIL/TIMER -I/root/openairinterface5g/openair2/UTIL/OMG -I/root/openairinterface5g/openair2/UTIL/OTG -I/root/openairinterface5g/openair2/UTIL/CLI -I/root/openairinterface5g/openair2/UTIL/OPT -I/root/openairinterface5g/openair2/UTIL/OMV -I/root/openairinterface5g/openair3/GTPV1-U/nw-gtpv1u/shared -I/root/openairinterface5g/openair3/GTPV1-U/nw-gtpv1u/include -I/root/openairinterface5g -I/root/openairinterface5g/cmake_targets/lte_build_oai/build/CMakeFiles/FLPT_V2 -I/root/openairinterface5g/openair2/UTIL/ASYNC_IF -I/root/openairinterface5g/cmake_targets/lte_build_oai/build/CMakeFiles/FSPT_V2 -I/root/openairinterface5g/common/utils/hashtable -I/root/openairinterface5g/common/utils/msc -I/root/openairinterface5g/nfapi/open-nFAPI/pnf/inc -I/root/openairinterface5g/nfapi/open-nFAPI/vnf/public_inc -I/root/openairinterface5g/nfapi/open-nFAPI/vnf/inc -I/root/openairinterface5g/nfapi/oai_integration -I/root/openairinterface5g/openair3/NAS/UE -I/root/openairinterface5g/openair3/NAS/UE/API/USIM -I/root/openairinterface5g/openair3/NAS/UE/EMM/SAP -I/root/openairinterface5g/openair3/NAS/UE/ESM/SAP -I/root/openairinterface5g/openair2/UTIL/LFDS/liblfds6.1.1/liblfds611/src -I/root/openairinterface5g/openair2/UTIL/LFDS/liblfds7.0.0/liblfds700/src -I/usr/include/libxml2 -I/usr/include/atlas -I/root/openairinterface5g/common/utils/T -mavx2 -msse4.1 -mssse3 -Wno-packed-bitfield-compat -fPIC -Wall -fno-strict-aliasing -rdynamic -std=c++11 -D'MAKE_VERSION(a,b,c)=((a)*256+(b)*16+c)' -O2 -g -DNDEBUG -o CMakeFiles/lte-softmodem.dir/root/openairinterface5g/targets/RT/USER/lte-softmodem_test.cpp.o -c /root/openairinterface5g/targets/RT/USER/lte-softmodem_test.cpp

错误日志:

<command-line>:0:6: error: expected identifier before numeric constant
<command-line>:0:6: error: expected unqualified-id before numeric constant
<command-line>:0:6: error: expected type-specifier before numeric constant
<command-line>:0:6: error: expected type-specifier before numeric constant
In file included from /usr/local/include/gtest/gtest.h:62:0,
                 from /root/openairinterface5g/targets/RT/USER/lte-softmodem_test.cpp:2:
/usr/local/include/gtest/internal/gtest-internal.h:690:39: error: variable or field ‘GenerateNamesRecursively’ declared void
 void GenerateNamesRecursively(internal::None, std::vector<std::string>*, int) {}
                                       ^
<command-line>:0:6: error: expected unqualified-id before numeric constant
In file included from /usr/local/include/gtest/gtest.h:62:0,
                 from /root/openairinterface5g/targets/RT/USER/lte-softmodem_test.cpp:2:
/usr/local/include/gtest/internal/gtest-internal.h:690:71: error: expected primary-expression before ‘*’ token
 void GenerateNamesRecursively(internal::None, std::vector<std::string>*, int) {}
                                                                       ^
/usr/local/include/gtest/internal/gtest-internal.h:690:72: error: expected primary-expression before ‘,’ token
 void GenerateNamesRecursively(internal::None, std::vector<std::string>*, int) {}
                                                                        ^
/usr/local/include/gtest/internal/gtest-internal.h:690:74: error: expected primary-expression before ‘int’
 void GenerateNamesRecursively(internal::None, std::vector<std::string>*, int) {}
                                                                          ^
/usr/local/include/gtest/internal/gtest-internal.h:757:61: error: template argument 3 is invalid
 class TypeParameterizedTest<Fixture, TestSel, internal::None> {
                                                             ^
/usr/local/include/gtest/internal/gtest-internal.h:815:64: error: wrong number of template arguments (2, should be 3)
 class TypeParameterizedTestSuite<Fixture, internal::None, Types> {
                                                                ^
/usr/local/include/gtest/internal/gtest-internal.h:778:7: note: provided for ‘template<template<class T> class Fixture, class Tests, class Types> class testing::internal::TypeParameterizedTestSuite’
 class TypeParameterizedTestSuite {

编辑:

我已经用错误日志编辑了问题,并在编译文件时进行了输出。预先感谢您抽出宝贵时间。

这就是我个人如何以干净的方式包含Google测试:

    # To prevent overriding the parent project's compiler/linker settings on Windows.
    if(WIN32)
        set(gtest_force_shared_crt ON CACHE BOOL "" FORCE)
    endif()

    FetchContent_Declare(
            googletest
            GIT_REPOSITORY https://github.com/google/googletest.git
            GIT_TAG        master
            SOURCE_DIR        "${GOOGLETEST_DOWNLOAD_DIR}/src" # choose preferred directory
            BINARY_DIR        "${GOOGLETEST_DOWNLOAD_DIR}/bin"
    )

    FetchContent_MakeAvailable(googletest)
   #gtest and gtest-main will be available here

您还可以将 FindGTest.cmake 模块与 find_package 函数一起使用,确保定义环境变量 GTEST_ROOT。如果这些方法有效而您的方法无效,则可能是您通过 cmake 设置了链接器(检查语法错误)。