CMake Warning: You have called ADD_LIBRARY for library my_src without any source files

10,006

For some reason your

file( GLOB my_gui_sources *.cc *.h)

Is not finding any file. To debug, you can print:

message(STATUS "my_gui_sources = ${my_gui_sources}")

Probably you want to use GLOB_RECURSE, which search in sub-directories:

file( GLOB_RECURSE my_gui_sources *.cc *.h)

Note that you don't need to add headers files to the source list.

Take care that you will have to rerun cmake every time you add a file to your project (cmake won't be called automatically, thing that instead happens if you touch one of the cmake files).

Link to documentation of command "file"

Edit:

The actual problem is that in your first CMakeLists.txt file you are using inconsistent naming for your variable (note that casing is important), therefore you have to change your add_library command to:

add_library( my_src ${my_sources} )

Note (off the records :-) ): the fact that casing is important for variable names might be confusing because, on the other hand, in cmake command names are case insensitive. It's also sometimes weird to notice that the character - (minus) might be used as part of the variable name: using _ (underscore) is most of the time preferable.

Share:
10,006
Sadık
Author by

Sadık

CV

Updated on June 05, 2022

Comments

  • Sadık
    Sadık almost 2 years

    I'm trying to call add_library for all files with certain endings.

    The dir structure is:

    src 
     | - CMakeLists.txt (1)
     | - main.cpp
     | - gui
          | - CMakeLists.txt (2)
          | - some source and header files
    

    So currently all cc files are in the gui directory.

    (1) CMakeLists.txt:

    file( GLOB_RECURSE my_sources *.cc ) 
    message(STATUS "my_sources = ${my_sources}")
    add_subdirectory( gui )
    add_library( my_src ${my_SOURCES} )
    
    target_link_libraries( my_src
      my_gui
    )
    qt5_use_modules( my_src Core Gui Widgets)
    

    (2) CMakeLists.txt:

    file( GLOB my_gui_sources *.cc)
    
    add_library( my_gui ${my_gui_sources} )
    qt5_use_modules( my_gui Core Gui Widgets)
    

    But I keep getting this output:

    You have called ADD_LIBRARY for library my_src without any source files. This typically indicates a problem with your CMakeLists.txt file
    -- my_sources = /home/bla/bla/src/gui/BorderLayout.cc;...;/home/bla/bla/my/src/gui/MainWindow.cc
    -- my_gui_sources = /home/bla/bla/my/src/gui/BorderLayout.cc;...;/home/bla/bla/my/src/gui/MainWindow.cc
    -- Configuring done
    -- Generating done
    -- Build files have been written to: /home/bla/bla/my/build
    

    I know that I currently don't need the add_library in the first CMakeLists.txt, but later I will. I changed the first GLOB to GLOB_RECURSE, so that it finds at least anything.