Cmake add_executable使用不正确的参数数量调用

Cmake add_executable called with incorrect number of arguments

本文关键字:参数 数数 调用 不正确 add executable Cmake      更新时间:2023-10-16

我正在尝试使用本教程在Linux(openSuse(上设置c ++开发环境 https://youtu.be/LKLuvoY6U0I?t=154

当我尝试构建CMake项目时,我得到了add_executable called with incorrect number of arguments

我的CMakeLists.txt:

cmake_minimum_required (VERSION 3.5)
project (CppEnvTest)
set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Werror -std=c++17")
set (source_dir "${PROJECT_SOURCE_DIR}/src/")
file (GLOB source_files "${source_dir}/*.cpp")
add_executable (CppEnvTest ${source_files})

我 build.sh:

#!/bin/sh
cmake -G "CodeLite - Unix Makefiles" -DCMAKE_BUILD_TYPE=Debug

我的终端输出:

/Dev/CppEnvTest/src> ./build.sh 
CMake Error at CMakeLists.txt:10 (add_executable):
add_executable called with incorrect number of arguments

-- Configuring incomplete, errors occurred!

要扩展@arrowd的答案(这是正确的(,您有几个选项可以列出源文件。file(GLOB ...),您的方法将只查找与当前目录中.cpp匹配的源文件。如果您的项目结构使得您在${source_dir}中嵌套了目录,则需要递归选项:

file (GLOB_RECURSE source_files "${source_dir}/*.cpp")

这将递归搜索,查找该目录任何嵌套目录中的所有.cpp文件。

您还可以使用set执行暴力破解方法,这是 CMake 的最佳选择,方法是执行以下操作:

set(source_files 
${source_dir}/mySrcs1/Class1.cpp
${source_dir}/mySrcs1/Class2.cpp
${source_dir}/mySrcs1/Class3.cpp
...
${source_dir}/mySrcs42/testClass1.cpp
${source_dir}/mySrcs42/testClass2.cpp
...
)

您的source_files变量似乎为空。通过在CMakeLists.txt中的某处添加message("source_files: ${source_files}")来验证这一点。