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

标签 c++ linux cmake

<分区>

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

当我尝试构建 CMake 项目时,我得到了 使用不正确数量的参数调用了 add_executable

我的 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} 中有嵌套目录,则您需要 recursive选项改为:

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
    ...
)

关于c++ - 使用不正确数量的参数调用 Cmake add_executable,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57895138/

相关文章:

c++ - Visual C++ - 将插件 DLL 链接到 EXE?

c++ - OpenGL - 在 ubuntu 中的线程上创建 vbo?

Linux shell 脚本 - 如何切换用户并运行脚本?

opencv - 如何在 Eclipse 上使用 opencv 进行数字图像处理

c++ - CMakeLists + gtest

c++ - GDB中自动变量的打印类型和模板函数/方法的原型(prototype)

C++ 类型是地址的抽象? - C++ 模板

php - 从 PHP 运行 shell 命令 - 使用 root 权限或 sudo

c - 为什么我的 C 程序中会出现 "Program failed: Memory fault, core dumped"?

cmake - 如何设置 CMAKE_AR 的选项?