我在尝试使用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)
编译文件lte_softmodem_test.cpp时,CMake以详细模式输出:
/usr/bin/c++-DASN1_MINIMUM_VERSION=924-BUILD_TYPE=;RelWithDebInfo"-DDRIVER2013-DENABLE_ITTI-DENABLE_NAS_UE_LOGING-DENABLE_USE_CPU_EXECUTION_TIME-DENABLE_USE_MME-DENABLE-VCD-DENB_MODE-DEXMIMO_IOT-DF1AP_RELEASE=R15-DF1AP_VERSION=3873-DFIRMWARE_VERSION="没有svn信息""-DFLPT_VERSION=V2-DFSPT_VERSION=V2-DJUMBO_FRAME-DLTE_RRC_VERSION=3696-DM2AP_RELEASE=R14-DM2AP_VERSION=3584-DM3AP_RELEASE=R14-DMC_AP_VERSION=3484-DMAX_NUM_CCs=1-DNAS_BUILT_IN_UE-DNAS_UE-DNB_ANTENAS_RX=2-DNB_ ANTENAS_TX=4-DNETTLE_VERSION_MAJOR=3-DNET TLE_VERSON_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="分支:阿布雷夫大师。哈希:9a06ceb日期:9月9日星期三16:15:50 2020+0200""-dphysm-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/Openairinterface=5g/cmake_targets/lte_build_oai/build/CMakeFiles/M2AP_R14-I/root/openairinterface5g/openair2/M2AP-I/root/Openairinterface=5g/cmake_targets/lte_build_oai/build/CMakeFiles/M3AP_R14-I/root/openairinterface5g/openair3/M3AP-I/root/Openairinterface=5g/cmake_targets/lte_build_oai/build/CMakeFiles/X2AP_R14-I/root/openairinterface5g/openair2/X2AP-I/root/Openairinterface=5g/cmake_targets/lte_build_oai/build/CMakeFiles/F1AP_R115.2.1-I/root/openairinterface5g/openair2/F1AP-I/root/oopenairinterface5G/targets/ARCH/USRP/USERSPACE/LIB-I/root/Openairinterface=5g/targets/ARCH/BLADERF/USERSPACE/LIB-I/root/openair interface5g/targets/ARCH/LMSSDR/USERSPAC/LIB-I/root/openairinterface=5G/targets/ARCH/ETHERNET/USERSPACE/LIB-I/root/openairinterface5g/targets/ARCH/IRIS/USERSPAcing/LIB-I/O目标/ARCH/COMMON-I/root/openairinterface5g/cmake_targets/lte_build_oai/build/CMakeFiles-I/root/openairinterface5g/openair2/COMMON-I/root/oopenairinterface5G/openair2/UTIL-I/root/Openairinterface=5g/openair2/UTIL/LOG-I/root/openairinterface=5G/openair3/COMMON-I/root/openairinterface5g/openair3/UTILS-I/root/nopenairinter接口5g/nfapi/opennAPI/nfapi/public_inc-I/root-openairinterfaces 5g/nfap/opennAPI/COMMON/public_inc-I/root/Oopenairinter接口5G/nfapi/OpennAPI/pnf/public_inc-I/root/openairinterface5g/nfati/open-nPAPI/nfapi/inc-I/root/oopenairinterface5G/nfati/open-nPAPI/sim_common\inc-I/root/Openairinterface=5g/nfatp/open-nPAPi/pnf_sim/inc-I/root/openairinterface5g/openair1-I/root/openairinterface=5G/openair2-I/root/oppenairinter接口5g/openair3/NAS/TOOLS-I/root-openairinterfaces 5G/openair2/ENB_APP-I/root.openairinterview5g/openair 2/MCE_APP-I/root/openairinterface5g/openair2/LAYER2/RLC-I/root/oopenairinterface5G/openair2/LAYER2/RLC/AM_v9.3.0-I/root/Openairinterface=5g/openair 2/LAYER2/RLC/UM_v9.3.0-I/root/openairinterface5g/oopenair2/LAYER/RLC/TM_v9.3.0-I/root/openair interface5g/openair2/LAYER2/PDCP_v10.1.0-I/root/openairinterface=5G/Openair 2/RRC/LTE/MESSAGES-I/root/openairinterface5g/openair2/RRC/LTE-I/root/openairinterface5g/common/utils-I/root/oopenairinterface5G/common/utils/ocp_itti-I/root/Openairinterface=5g/openair3/NAS/common-I/root/openairinterface=5G/openair3/NAS/common/API/NETWORK-I/root/oppenairinter接口5g/openair3/NAS/common/EMM/MSG-I/root-openairinterview5g/openair 3/NAS/CONM/ESM/MSG-I/root/openairinterface5g/openair3/NAS/UE/ESM-I/root/dopenairinterfaces空中接口5g/openair3/NAS/UE/EMM-I/root/openairinterface5g/openair3/NAS/UE/neneneba API/USER-I/root/oopenairinterface5G/openair3/NAS/COMMON/IES-I/root/Openairinter接口5g/openair3/NAS/COMMON/UTIL-I/root/openairinterface5g/oopenair3/SECU-I/root/openairinter接口5G/openair3/SCTP-I/root/oppenairinterface=openair3/UDP-I/root/OpenairInter接口5g/oopenAir3/GTPV1-U-I/root/openairinterface5g/Oopenair3/MM应用程序(_AP)-I/root/openairinterface5g/targets/COMMON-I/root/oopenairinterface5G/openair2/ENB_APP/CONTROL_MODULES/PHY-I/root/Openairinterface=ENB_APP/CONTROL_MODULES/MAC-I/root/openairinterfaces 5g/openair2/ENB_APP/CONTROL_MMODULES/RRC-I/root/oppenairinterfaces 5G/openair 2/ENB_APP/CONTROL_ODULES/PDCP-I/root/openairinterface5g/openair 2/UTIL/OSA-I/root/openairinterface5g/openair2/UTIL/LFDS/libfds6.1.1/libfds611/inc-I/root/openairinterface5g/openair2/UTIL/LFDS/libfds7.0.0/libfds700/inc-I/root/openairinterface5g/openair2/LAYER2/PROTO_AGENT-I/root/oopenairinterface5G/openair2/UTIL/MEM-I/root/Openairinterfaces 5g/openair 2/UTIL/LISTS-I/root/openairinterfaces 5G/openair2/UTIL/FIFO-I/root/oppenairinterface=5g/openair2/UTIL/OCG-I/root/popenairinterface=5G/openair 2/UTIL/MATH 5g/露天2/UTIL/OMG-I/root/openairinterface5g/openair2/UTIL/OTG-I/root/oopenairinterface5G/openair2/UTIL/CLI-I/root/Openairinterface=UTIL/OPT-I/root/openairinterface=5G/openair2/UTIL/OMV-I/root/oppenairinterview5g/openair 3/GTPV1-U/nw-gtpv1u/shared-I/root-openairinterfaces 5g/openair3/GTPV1/U/nw-gtpv1u/include-I/root/openairinterface 5g-I/root/openairinterface5g/cmake_targets/lte_build_oai/build/CMakeFiles/FLPT_V2-I/root/openairinterface5g/openair2/UTIL/ASYNC_IF-I/root/Openairinterface=5g/cmake_targets/lte_build_oai/build/CMakeFiles/FSPT_V2-I/root/openairinterface5g/common/utils/hashtable-I/root/oopenairinterface5G/common/utils/msc-I/root/Openairinterface=5g/nfati/open-nPAPI/pnf/inc-I/root/openairinterface5g/nfati/open-nPAPI/vnf/public_inc-I/root/openairinterface=5G/nfatp/open-nPAP/vnf/inc-I/root/Openairterface5g/nfatp/oai_integration-I/root/openairinterface5g/openair3/NAS/UE-I/root/openairinterface5g/openair3/NAS/UE/neneneba API/USIM-I/root/oopenairinterface5G/openair3/NAS/UE/EMM/SAP-I/root/Openairinterface=5g/openair 3/NAS/UE/ESM/SAP-I/root/openairinterface=5G/openair 2/UTIL/LFDS/liblfds6.1.1/liblfds111/src-I/root/openairinterface5g/openair2/UTIL/LFDS/libfds7.0.0/libfds700/src-I//usr/include/libxml2-I//usr/include/atlas-I/root/openairinterface5g/common/utils/T-mavx2-msse4.1-mssse3-Wno压缩位字段compat-fPIC-Wall-fno严格混叠-rddynamic-std=c++11-D MAKE_VERSION(a,b,c)=((a)*256+(b)*16+c)'-O2-g-DNDEBUG-o CMakeFiles/lte softmodem.dir/root/openairinterface 5g/targets/RT/USER/lte-softmodem_test.cpp.o-c/root/openairinterface5g/targets/RT/USER/let-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 {
编辑:
我已经用错误日志编辑了这个问题,并在编译文件时进行了输出。提前感谢您花费时间。
这就是我个人如何以一种干净的方式包含谷歌测试:
# 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的链接器设置(检查语法错误)。