<div dir="ltr"><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr"><div id="gmail-m_-9103182773552894385divtagdefaultwrapper" style="font-size:12pt;color:rgb(0,0,0);font-family:Calibri,Helvetica,sans-serif" dir="ltr"><p>When I ran "cmake .." in bin, cmake complained that it could not find source file src1.h in add_executable. What is wrong here?</p></div></div></blockquote><div><br></div><div>Hi Lei,</div><div>Alex hit several of these points, but to wrap it up together:</div><div><br></div><div>A couple of things...<br></div><div><ul><li>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.</li><li>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.</li><li>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.</li><li>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.<br></li><li>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.</li></ul><div>Combining these things, you could have nested CMakeLists.txt in which the subdirectories are explicitly adding the sources:</div><div><ul><li>Top Level:<br></li><ul><li>CMakeLists.txt<br><span style="font-family:monospace,monospace">cmake_minimum_required(VERSION 3.8)<br>project(proj)<br><br>add_executable(exec)<br><br>add_subdirectory(common)<br>add_subdirectory(dir1)</span><br></li><li>common/</li><ul><li>src1.h</li><li>CMakeLists.txt<br><span style="font-family:monospace,monospace">target_include_directories(exec PRIVATE ${CMAKE_CURRENT_SOURCE_DIR})<br><span style="font-family:monospace,monospace">target_sources(exec PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/src1.h)</span></span></li></ul><li>dir1/</li><ul><li>src1.c</li><li>src2.c</li><li>CMakeLists.txt<span style="font-family:monospace,monospace"><br>target_sources(exec PRIVATE<br>  ${CMAKE_CURRENT_SOURCE_DIR}/src1.c<br>  ${CMAKE_CURRENT_SOURCE_DIR}/src2.c<br>)</span></li></ul></ul></ul><span style="font-family:monospace,monospace"><font face="arial,helvetica,sans-serif">Or a much simpler single file where no nested CMakeLists.txt are needed since they wouldn't be doing anything:</font></span></div><div><ul><li>Top Level: <br></li><ul><li>CMakeLists.txt<br><span style="font-family:monospace,monospace">cmake_minimum_required(VERSION 3.8)<br>project(proj)<br><br>add_executable(exec<br>  common/src1.h<br>  dir1/src1.c dir1/src2.c<br>)<br>target_include_directories(exec common)<br></span><br></li><li>common/</li><ul><li>src1.h</li></ul><li>dir1/</li><ul><li>src1.c</li><li>src2.c</li></ul></ul></ul></div></div>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.<br></div><div class="gmail_quote"><div><br></div><div> - Chuck<br></div></div></div>