[CMake] cmake cannot find source file (new to cmake)
Chuck Atkins
chuck.atkins at kitware.com
Thu Dec 27 17:04:35 EST 2018
>
> 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
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://cmake.org/pipermail/cmake/attachments/20181227/3607ddf1/attachment.html>
More information about the CMake
mailing list