There is no way to do it without a hack. You need at least one *.c or *.cpp file.

What I do is make a dummy null.cpp file (zero bytes) and use that. You can also use /dev/null but that only works on Linux.

file(WRITE null.cpp "")

add_executable(tester
    null.cpp
)

target_link_libraries(tester
    -Wl,--whole-archive
    libtest1
    libtest2
    libtest3
    libtest4
    -Wl,--no-whole-archive
    gtest_main
)
Answer from Mark Lakata on Stack Overflow
Top answer
1 of 2
29

There is no way to do it without a hack. You need at least one *.c or *.cpp file.

What I do is make a dummy null.cpp file (zero bytes) and use that. You can also use /dev/null but that only works on Linux.

file(WRITE null.cpp "")

add_executable(tester
    null.cpp
)

target_link_libraries(tester
    -Wl,--whole-archive
    libtest1
    libtest2
    libtest3
    libtest4
    -Wl,--no-whole-archive
    gtest_main
)
2 of 2
16

There are mainly two reasons why a source file is enforced by CMake:

  1. To determine the LINKER_LANGUAGE from the file ending(s)
  2. Not all compilers do support an object/library only link step (for details see below)

And if you move the main() function to library please keep the following in mind: Why does the order in which libraries are linked sometimes cause errors in GCC?

So if you build the libraries with CMake in the same project, I would recommend to change your libraries (at least the one containing your main() function) to an object library:

cmake_minimum_required(VERSION 2.8.8)

project(NoSourceForExe)

file(WRITE main.cc "int main() { return 0; }")

add_library(MyLibrary OBJECT main.cc)
add_executable(MyExecutable $<TARGET_OBJECTS:MyLibrary>)

The add_library() documentation lists a warning here:

Some native build systems may not like targets that have only object files, so consider adding at least one real source file to any target that references $<TARGET_OBJECTS:objlib>.

But those are rare and listed in Tests/ObjectLibrary/CMakeLists.txt:

# VS 6 and 7 generators do not add objects as sources so we need a
# dummy object to convince the IDE to build the targets below.
...
# Xcode does not seem to support targets without sources.

Not knowing which host OS(s) you are targeting, you may just give it a try.

References

  • CMake Object Lib containing main
  • CMake/Tutorials/Object Library
🌐
CMake
cmake.org › cmake › help › latest › command › add_library.html
add_library — CMake 4.3.0-rc3 Documentation
Add an Interface Library target that may specify usage requirements for dependents but does not compile sources and does not produce a library artifact on disk. An interface library with no source files is not included as a target in the generated buildsystem.
Discussions

CMake Error : Cannot find source file -> No SOURCES given to target
You’ve commented out the URL keyword, so it isn’t downloading the webkitgtk-2.42.3.tar.xz file. It is cloning the git repository you specified instead · But even if you were using URL, you don’t get access to the original downloaded file by default. It is not saved in the current source ... More on discourse.cmake.org
🌐 discourse.cmake.org
0
0
December 16, 2023
c++ - Issue regarding CMake Error: No source given to target - Stack Overflow
CMake Error at ProjectIncludes.cmake:46 ... (add_library): No SOURCES given to target: src_portable_ported_aws_bufpool Call Stack (most recent call first): CMakeLists.txt:33 (include) ... In the README they suggest to run python script AM335xFreeRTOS_cmake_makefile_args.py. Have you tried that approach? ... Their script most likely runs cmake with specific ... More on stackoverflow.com
🌐 stackoverflow.com
CMake No SOURCES given to target ERROR
You aren't giving any sources to target test, a library isn't enough (theoretically it is, but not for cmake). Removing main.cpp from the lib and moving it to the test executable should work: add_executable(${PROJECT_NAME} ${CMAKE_CURRENT_SOURCE_DIR}/src/main.cpp) target_link_libraries(${PROJECT_NAME} mylib) Relevant Stackoverflow question More on reddit.com
🌐 r/cpp_questions
4
3
September 2, 2018
add_library, sources as list not producing DLL, no error either
Hi, I’m trying to write a CMakeLists.txt file in VS Code using the Microsoft C compiler to make a DLL target. I never learned ‘make’ files the hard way so you may need to explain me some dumb. But I did get my whole process to run in command line (without CMake) and produce the DLL I want. More on discourse.cmake.org
🌐 discourse.cmake.org
0
0
September 10, 2023
🌐
CMake
cmake.cmake.narkive.com › O4Wigz8x › add-library-without-source-code
[CMake] add_library without source code
If you want, you can create that dummy file e.g. with file(WRITE ...), so it doesn't have to go into version control. lib1 and lib2 are shared libs, right ? Alex No they are static libraries. I recognized that i have the same problem with add_executable, there is also at least one source file necessary and i don't like to make dummy files for nothing. But maybe the configuration of my build is unusual for CMake.
🌐
CMake Discourse
discourse.cmake.org › code
CMake Error : Cannot find source file -> No SOURCES given to target - Code - CMake Discourse
December 16, 2023 - With this CMakeLists.txt file: cmake_minimum_required(VERSION 3.24) project(my_application LANGUAGES CXX) include(FetchContent) FetchContent_Declare( WebKit2 #URL https://webkitgtk.org/releases/webkitgtk-2.42.3.tar.xz GIT_REPOSITORY https://github.com/raphael10-collab/webkitgtkIncludable.git ) FetchContent_MakeAvailable(webkit2) add_executable(my_application src/main.cpp) add_library(webkit2 webkitgtk-2.42.3.tar.xz) target_link_libraries(webkit2 webkitgtk-2.42.3) I get this error : cm...
🌐
CMake
cmake.org › pipermail › cmake › 2010-July › 038473.html
[CMake] add_library without source code
July 28, 2010 - > I don't like to have source files in the root directory, so i don't have source files for the commands add_library or add_executable in the root CMakeLists.txt file. > > How is somethiong like that usually done with CMake? > > Matthias. Are those libraries only "convenience" libraries? I.e. are you ever going to install them? If not, just list the source-files in the top-level CMakeLists.txt: add_executable(super-duper modul1/modul1.c module2/module2.c ) This also saves time since you don't have to run ar and ranlib.
🌐
GitHub
github.com › Kitware › CMake › blob › master › Tests › ObjectLibrary › CMakeLists.txt
CMake/Tests/ObjectLibrary/CMakeLists.txt at master · Kitware/CMake
if(CMAKE_C_LINK_DEF_FILE_FLAG OR NOT WIN32) list(APPEND ABshared_SRCS $<TARGET_OBJECTS:B> AB.def) else() set(NO_A NO_A) list(APPEND ABshared_SRCS $<TARGET_OBJECTS:Bexport>) endif() · # Test shared library without its own sources. add_library(ABshared SHARED ${dummy} ${ABshared_SRCS}) target_include_directories(ABshared PUBLIC $<TARGET_PROPERTY:B,INTERFACE_INCLUDE_DIRECTORIES>) target_compile_definitions(ABshared PUBLIC $<TARGET_PROPERTY:B,INTERFACE_COMPILE_DEFINITIONS>) ·
Author   Kitware
Find elsewhere
🌐
Reddit
reddit.com › r/cpp_questions › cmake no sources given to target error
r/cpp_questions on Reddit: CMake No SOURCES given to target ERROR
September 2, 2018 -

Hello Everyone,

Been trying to learn cmake and wanted to include the newer features into my sample project. The project currently is a simple QT window. Been trying to modify cmake generated by qt to have some modern features. Example like target_sources.

underneath this line is my cmake code

cmake_minimum_required(VERSION 3.8)

project(test LANGUAGES CXX)

set(CMAKE_INCLUDE_CURRENT_DIR ON)

set(CMAKE_AUTOMOC ON)

set(CMAKE_AUTORCC ON)

set(CMAKE_PREFIX_PATH C:/Qt/5.11.1/mingw53_32)

set(CMAKE_EXPORT_COMPILE_COMMANDS OFF)

add_library(mylib

${CMAKE_CURRENT_SOURCE_DIR}/src/main.cpp

${CMAKE_CURRENT_SOURCE_DIR}/src/window.cpp

${CMAKE_CURRENT_SOURCE_DIR}/include/window.hpp

)

target_sources(mylib

PRIVATE

${CMAKE_CURRENT_SOURCE_DIR}/src/window.cpp

PUBLIC

${CMAKE_CURRENT_SOURCE_DIR}/include/window.hpp

${CMAKE_CURRENT_SOURCE_DIR}/src/main.cpp

)

find_package(Qt5 COMPONENTS Core Quick QuickControls2 Widgets REQUIRED)

add_executable(${PROJECT_NAME} mylib)

target_compile_definitions(mylib PRIVATE $<$<OR:$<CONFIG:Debug>,$<CONFIG:RelWithDebInfo>>:QT_QML_DEBUG>)

target_link_libraries(mylib PRIVATE Qt5::Core Qt5::Quick Qt5::QuickControls2 Qt5::Widgets)

The cmake file is in top directory that contains the src and include file. But i been getting a error called:

CMake Error at CMakeLists.txt:27 (add_executable):No SOURCES given to target: test

Does anyone know what i am doing wrong currently or push me to right direction ?

🌐
CMake Discourse
discourse.cmake.org › code
add_library, sources as list not producing DLL, no error either - Code - CMake Discourse
September 10, 2023 - Hi, I’m trying to write a CMakeLists.txt file in VS Code using the Microsoft C compiler to make a DLL target. I never learned ‘make’ files the hard way so you may need to explain me some dumb. But I did get my whole pro…
🌐
CMake
cmake.org › cmake › help › latest › guide › tutorial › In-Depth CMake Library Concepts.html
Step 5: In-Depth CMake Library Concepts — CMake 4.3.0 Documentation
We give this file set the name HEADERS so we can omit the TYPE, we don't need BASE_DIRS as we will use the default of the current source directory, and we can exclude the FILES list because we don't intend to install the library. ... Now we can add the MathLogger library to the MathFunctions linked libraries, and at the MathLogger folder to the project.
🌐
CMake
cmake.org › cmake › help › v3.9 › command › add_library.html
add_library — CMake 3.9.6 Documentation
They may contain custom commands generating such sources, but not PRE_BUILD, PRE_LINK, or POST_BUILD commands. Object libraries cannot be linked. Some native build systems may not like targets that have only object files, so consider adding at least one real source file to any target that references $<TARGET_OBJECTS:objlib>.
🌐
Steinberg
forums.steinberg.net › developer › vst 3 sdk
Cmake can't build project (No sources given to target: vstgui_standalone) - VST 3 SDK - Steinberg Forums
October 13, 2022 - I’m trying to generate a project using the vst3 project generator, and I get the following error whenever I try to compile it with the vstgui: – The C compiler identification is GNU 12.2.0 – The CXX compiler identification is GNU 12.2.0 – Detecting C compiler ABI info – Detecting ...
🌐
GitHub
github.com › nomic-ai › gpt4all › issues › 796
CMake Error at llama.cpp.cmake:320 (add_library) · Issue #796 · nomic-ai/gpt4all
June 1, 2023 - CMake Error at llama.cpp.cmake:341 (add_library): No SOURCES given to target: llama-mainline-default Call Stack (most recent call first): CMakeLists.txt:58 (include_ggml)
Author   stevencoveta
🌐
CMake
cmake.org › cmake › help › latest › guide › tutorial › Adding a Library.html
Step 2: Adding a Library — CMake 4.3.0 Documentation
Step 3: Adding Usage Requirements for a Library · Show Source · index · next | previous | CMake 4.3.0 » · Documentation » · CMake Tutorial » · Step 2: Adding a Library · © Copyright 2000-2026 Kitware, Inc. and Contributors.
🌐
Nordic DevZone
devzone.nordicsemi.com › f › nordic-q-a › 75978 › zephyr-cmake-error-no-sources-given-to-target-for-out-of-tree-driver-when-config_bootloader_mcuboot-is-enabled
Zephyr CMake error (No SOURCES given to target) for out of tree driver when CONFIG_BOOTLOADER_MCUBOOT is enabled - Nordic Q&A - Nordic DevZone - Nordic DevZone
June 3, 2021 - I would also like to note that out out of tree board directory does successfully get added & works fine. It is added to the main project directory's CMakeLists with the following line, just above the attempt to add the out of tree driver: list(APPEND BOARD_ROOT ${CMAKE_CURRENT_SOURCE_DIR}/../)
🌐
CMake
cmake.org › cmake › help › latest › variable › BUILD_SHARED_LIBS.html
BUILD_SHARED_LIBS — CMake 4.3.0-rc1 Documentation
Tell add_library() to default to SHARED libraries, instead of STATIC libraries, when called with no explicit library type. Calls to add_library() without any explicit library type check the current BUILD_SHARED_LIBS variable value. If it is true, then the default library type is SHARED.