0

I am trying to set up c++ development environment on Linux (openSuse) with this tutorial https://youtu.be/LKLuvoY6U0I?t=154

When I try to build CMake project I'm getting add_executable called with incorrect number of arguments

My 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})

My build.sh:

#!/bin/sh

cmake -G "CodeLite - Unix Makefiles" -DCMAKE_BUILD_TYPE=Debug

My terminal output:

/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!
Szymon
  • 3
  • 1
  • 5

2 Answers2

2

To expand on @arrowd's answer, which is correct, you have a couple options for listing your source files. Your approach, file(GLOB ...), will only look for source files matching .cpp in the current directory. If your project is structured such that you have nested directories within ${source_dir}, you need the recursive option instead:

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

This will search recursively finding all of the .cpp files in that directory and any nested directories.

You can also do a brute-force method using set, which is the best option for CMake, by doing something like this:

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
    ...
)
Kevin
  • 16,549
  • 8
  • 60
  • 74
1

Your source_files variable seems empty. Verify that by adding message("source_files: ${source_files}") somewhere in CMakeLists.txt.

arrowd
  • 33,231
  • 8
  • 79
  • 110
  • I've done it and got: `/Dev/CppEnvTest/src> ./build.sh source_files: CMake Error at CMakeLists.txt:12 (add_executable): add_executable called with incorrect number of arguments -- Configuring incomplete, errors occurred! ` – Szymon Sep 11 '19 at 19:09