cmake cannot find source file (new to cmake)

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

cmake cannot find source file (new to cmake)

CMake mailing list

Hi,


    I tried to follow some examples and wrote some cmake files to build my code. But cmake is complaining that it cannot find a source file. Here is the code structure:


    Toplevel

       |----- CMakeLists.txt

       |----- src --------|------CMakeLists.txt

       |                      |------common---------------|-------CMakeLists.txt

       |                      |------dir1-----|                   |------- src1.h

       |                                          |-----CMakeLists.txt

       |----bin                                |-----src2.c

                                                  |-----src3.c


    Here is the CMakeLists.txt in toplevel:

                project(proj)

                add_subdirectory(src)



    Here is the CMakeLists.txt in src:

                project(proj)

                add_subdirectory(common)

                add_subdirectory(dir1)

                include_directories(common)

                set(src_files src1.h src2.c src3.c)

                add_executable(exec ${src_files})


     CMakeLists.txt in common and dir1 are empty.


      When I ran "cmake .." in bin, cmake complained that it could not find source file src1.h in add_executable. What is wrong here?


Thanks,

Lei


--

Powered by www.kitware.com

Please keep messages on-topic and check the CMake FAQ at: http://www.cmake.org/Wiki/CMake_FAQ

Kitware offers various services to support the CMake community. For more information on each offering, please visit:

CMake Support: http://cmake.org/cmake/help/support.html
CMake Consulting: http://cmake.org/cmake/help/consulting.html
CMake Training Courses: http://cmake.org/cmake/help/training.html

Visit other Kitware open-source projects at http://www.kitware.com/opensource/opensource.html

Follow this link to subscribe/unsubscribe:
https://cmake.org/mailman/listinfo/cmake
Reply | Threaded
Open this post in threaded view
|

Re: cmake cannot find source file (new to cmake)

Oleksii Vilchanskyi
>     Toplevel

>
>        |----- CMakeLists.txt
>
>        |----- src --------|------CMakeLists.txt
>
>        |                     
> |------common---------------|-------CMakeLists.txt
>
>        |                      |------dir1-----|                 
>  |------- src1.h
>
>        |                                          |-----CMakeLists.txt
>
>        |----bin                                |-----src2.c
>
>                                                   |-----src3.c
It's quite hard to figure out what's going on here, but I assume src1.h
is under src/common. Please post the output of 'tree' command next time,
if you are on a Unix.

>                 set(src_files src1.h src2.c src3.c)

You don't have to add headers as target dependencies unless you program
in an IDE (just fyi).

>      CMakeLists.txt in common and dir1 are empty.

So, either partially move your CMake scripts into these subdirectories,
or don't do `add_subdirectory()`. For example:

src/CMakeLists.txt:
        add_executable(exec dir1/src1.c dir1/src2.c dir1/src3.c)
        target_include_directories(exec PRIVATE
                "${CMAKE_CURRENT_SOURCE_DIR}/common")

--
Alex


--

Powered by www.kitware.com

Please keep messages on-topic and check the CMake FAQ at: http://www.cmake.org/Wiki/CMake_FAQ

Kitware offers various services to support the CMake community. For more information on each offering, please visit:

CMake Support: http://cmake.org/cmake/help/support.html
CMake Consulting: http://cmake.org/cmake/help/consulting.html
CMake Training Courses: http://cmake.org/cmake/help/training.html

Visit other Kitware open-source projects at http://www.kitware.com/opensource/opensource.html

Follow this link to subscribe/unsubscribe:
https://cmake.org/mailman/listinfo/cmake

signature.asc (849 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: cmake cannot find source file (new to cmake)

CMake mailing list
In reply to this post by CMake mailing list

When I ran "cmake .." in bin, cmake complained that it could not find source file src1.h in add_executable. What is wrong here?


Hi Lei,
Alex hit several of these points, but to wrap it up together:

A couple of things...
  • There's no need for a CMakeLists.txt in a directory if it's not actually going to do anything.  i.e. if you're not adding targets or executing any other CMake code then just traversing the directories doesn't serve any particular purpose.
  • The list of source files that a target uses to build don't have to necessarily be full paths, they can be absolute or relative paths, but the relative paths need to be relative to where the target is created, i.e. the add_executable(...) call.  So in your case, the target is "exec" in the "src" folder so all paths to source files, if relative paths and not absolute paths, need to be relative to the src directory.
  • include_directories is the older style CMake and will apply to all targets created after it.  Use target_include_directories instead for the more modern target-centric approach of having commands only apply to the appropriate target instead of globally.
  • Rather than use a list of sources, you can also use the target_sources command to programmatically add source files to a target based on necessary logic.  The same restriction applies though that the sources added need to be relative to the target location or an absolute path.
  • The preferred project layout for CMake is to really have out-of-source builds.  So instead having bin as a sub-directory of your project, just eliminate that level entirely.  You can certainly create your structure however you want but that's the suggested and preferred way of organising.
Combining these things, you could have nested CMakeLists.txt in which the subdirectories are explicitly adding the sources:
  • Top Level:
    • CMakeLists.txt
      cmake_minimum_required(VERSION 3.8)
      project(proj)

      add_executable(exec)

      add_subdirectory(common)
      add_subdirectory(dir1)

    • common/
      • src1.h
      • CMakeLists.txt
        target_include_directories(exec PRIVATE ${CMAKE_CURRENT_SOURCE_DIR})
        target_sources(exec PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/src1.h)
    • dir1/
      • src1.c
      • src2.c
      • CMakeLists.txt
        target_sources(exec PRIVATE
          ${CMAKE_CURRENT_SOURCE_DIR}/src1.c
          ${CMAKE_CURRENT_SOURCE_DIR}/src2.c
        )
Or a much simpler single file where no nested CMakeLists.txt are needed since they wouldn't be doing anything:
  • Top Level:
    • CMakeLists.txt
      cmake_minimum_required(VERSION 3.8)
      project(proj)

      add_executable(exec
        common/src1.h
        dir1/src1.c dir1/src2.c
      )
      target_include_directories(exec common)

    • common/
      • src1.h
    • dir1/
      • src1.c
      • src2.c
Both are valid, the first is overkill for this simple example but illustrates a way of doing things that can be helpful with much more complex projects.  Either way, you also have a build directory completely detached and outside your source tree instead of a sub-directory.

 - Chuck

--

Powered by www.kitware.com

Please keep messages on-topic and check the CMake FAQ at: http://www.cmake.org/Wiki/CMake_FAQ

Kitware offers various services to support the CMake community. For more information on each offering, please visit:

CMake Support: http://cmake.org/cmake/help/support.html
CMake Consulting: http://cmake.org/cmake/help/consulting.html
CMake Training Courses: http://cmake.org/cmake/help/training.html

Visit other Kitware open-source projects at http://www.kitware.com/opensource/opensource.html

Follow this link to subscribe/unsubscribe:
https://cmake.org/mailman/listinfo/cmake