<div dir="ltr">Thinking about this some more, I suspect Petr's comments may be on the right track. What matters is the value of this variable in the scope of the <i>directory</i> being processed. You need the CMAKE_INCLUDE_CURRENT_DIR variable to be set in that directory scope, if I'm understanding the docs correctly. If you create an executable target inside the function, I would hazard a guess that it is that directory scope's variable that will be consulted to determine whether to include the current source and binary dirs in the include path.<div><br></div><div>To be honest, when I've used the automoc feature in the past, I've always set this variable directly in the CMakeLists.txt file where I want it to apply, never inside a function. This has always Just Worked for me. If you really want to get those two paths included in the search path without having to rely on CMAKE_INCLUDE_CURRENT_DIR being set in the directory scope, you can always just add them manually to your target inside the function. For example:</div><div><br></div><div><div style="font-size:12.800000190734863px">function(AddTest)</div><div style="font-size:12.800000190734863px">    ...</div><div style="font-size:12.800000190734863px">    add_executable(${FILE_RAW} ${TEST_FILE})<br></div></div><div style="font-size:12.800000190734863px">    target_include_directories(${FILE_RAW} PRIVATE</div><div style="font-size:12.800000190734863px">        "${CMAKE_CURRENT_BINARY_DIR}"</div><div><div style="font-size:12.800000190734863px">        "${CMAKE_CURRENT_SOURCE_DIR}"</div></div><div style="font-size:12.800000190734863px">    )</div><div>    ...</div><div>endfunction()</div><div><br></div><div>Pretty sure that would work and it avoids the whole question of where CMAKE_INCLUDE_CURRENT_DIR needs to be set. One could also argue that it makes your function more self-contained with fewer side effects.</div><div><br></div></div><div class="gmail_extra"><br><div class="gmail_quote">On Tue, Oct 25, 2016 at 4:20 PM, Petr Kmoch <span dir="ltr"><<a href="mailto:petr.kmoch@gmail.com" target="_blank">petr.kmoch@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr"><div><div><div><div><div><div><div><div>Hi Tiago.<br><br></div>Yes, Craig's original comment applies. Targets do not have scope, variables do. Because you're in a function, you'd need to set the variable using PARENT_SCOPE to have it apply outside the function:<br><br></div>function(AddTest)<br>  #...<br></div>  set(CMAKE_INCLUDE_CURRENT_DIR ON PARENT_SCOPE)<br>  #...<br></div>endfunction()<br><br></div>Note that this will only help if the funciton is called directly; if called from another function, it will fail again (since the variable would just be set in the calling function's scope and not at global level).<br><br></div>However, taking a step back, I believe setting the variable doesn't belong into the `AddTest` function at all. Looking at it, it seems to be concerned with creating and setting up one target. IMO, such a function should not also modify global state. Do not forget that CMAKE_INCLUDE_CURRENT_DIR is not target-specific in any way; it affects *all* targets in the current directory.<br><br></div>Therefore, my suggestion would be to move setting it out of the function altogether and perform it at CMakeList scope. Alternatively, put the function's declaration into a separate CMake file, along with the set() command. Then, whoever wants to use the function has to include() that file, which will also cause them to have CMAKE_INCLUDE_CURRENT_DIR set accordingly.<span class="HOEnZb"><font color="#888888"><br><br></font></span></div><span class="HOEnZb"><font color="#888888">Petr</font></span><div><div class="h5"><br><div class="gmail_extra"><br><div class="gmail_quote">On 25 October 2016 at 00:55, Tiago Macarios <span dir="ltr"><<a href="mailto:tiagomacarios@gmail.com" target="_blank">tiagomacarios@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr">Hi Craig,<div><br></div><div>Maybe my problem description was lacking. Below is the function I have. Both CMAKE_INCLUDE_CURRENT_DIR and the target are defined on the same function scope, but this does not seem to work. I need to define CMAKE_INCLUDE_CURRENT_DIR on the parent CMakeLists file.</div><div><br></div><div><div>function(AddTest)</div><div>    set(options)</div><div>    set(oneValueArgs FILE FOLDER)</div><div>    set(multiValueArgs LIBRARIES)</div><div>    cmake_parse_arguments(TEST</div><div>        "${options}"</div><div>        "${oneValueArgs}"</div><div>        "${multiValueArgs}"</div><div>        ${ARGN}</div><div>    )</div><div><br></div><div>    # THIS DOES NOT WORK HERE I NEED TO SET IT IN THE PARENT FOLDER</div><div>    set(CMAKE_INCLUDE_CURRENT_DIR ON)<br></div><div><br></div><div>    get_filename_component(FILE_RA<wbr>W ${TEST_FILE} NAME_WE)</div><div>    add_executable(${FILE_RAW} ${TEST_FILE})</div><div><br></div><div>    set_target_properties(${FILE_R<wbr>AW}</div><div>        PROPERTIES</div><div>        CXX_STANDARD 14</div><div>        CXX_EXTENSIONS OFF</div><div>        AUTOMOC ON</div><div>        AUTOUIC ON</div><div>        FOLDER ${TEST_FOLDER}</div><div>    )</div><div><br></div><div>    find_package(Qt5Test)</div><div>    target_link_libraries(${FILE_R<wbr>AW} ${TEST_LIBRARIES})</div><div><br></div><div>    add_test(NAME ${FILE_RAW} COMMAND ${FILE_RAW})</div><div>endfunction()</div></div><div><br></div><div><br></div><div><br></div><div><br></div><div><br></div><div><br></div><div><br></div><div><br></div><div><br></div><div><br></div></div><div class="m_-4151677493543721148HOEnZb"><div class="m_-4151677493543721148h5"><div class="gmail_extra"><br><div class="gmail_quote">On Mon, Oct 24, 2016 at 3:48 PM, Craig Scott <span dir="ltr"><<a href="mailto:craig.scott@crascit.com" target="_blank">craig.scott@crascit.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr">function() introduces a new scope, so if you want changes you make to variables inside the function to be visible outside the function, you need to use set(... PARENT_SCOPE). Alternatively, a macro() does not introduce a new scope, so replacing your function() with a macro() may also yield the behaviour you want (but changing to a macro has other effects, so make sure you read the docs before going down that path). Also note that setting it in one directory does not make it apply to subdirectories as well, in case that matters in your situation.<div><br></div><div><br></div></div><div class="gmail_extra"><br><div class="gmail_quote"><div><div class="m_-4151677493543721148m_1779837803095218789h5">On Tue, Oct 25, 2016 at 9:42 AM, Tiago Macarios <span dir="ltr"><<a href="mailto:tiagomacarios@gmail.com" target="_blank">tiagomacarios@gmail.com</a>></span> wrote:<br></div></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div><div class="m_-4151677493543721148m_1779837803095218789h5"><div dir="ltr">Hi,<div><br></div><div>Does CMAKE_INCLUDE_CURRENT_DIR need to be set outside of a function?<br></div><div><br></div><div>I have a function where I define an executable "add_executable". This executable uses moc'ed Qt clasees, so I need to set CMAKE_INCLUDE_CURRENT_DIR. It seems like I have to set it from the top level script calling the function. If I set it inside the function the compilation fails with a missing moc file.<br></div><div><br></div><div>Any ideas?</div><span class="m_-4151677493543721148m_1779837803095218789m_-2420463097592014405HOEnZb"><font color="#888888"><div><br></div><div>Tiago</div></font></span></div>
<br></div></div>--<br>
<br>
Powered by <a href="http://www.kitware.com" rel="noreferrer" target="_blank">www.kitware.com</a><br>
<br>
Please keep messages on-topic and check the CMake FAQ at: <a href="http://www.cmake.org/Wiki/CMake_FAQ" rel="noreferrer" target="_blank">http://www.cmake.org/Wiki/CMak<wbr>e_FAQ</a><br>
<br>
Kitware offers various services to support the CMake community. For more information on each offering, please visit:<br>
<br>
CMake Support: <a href="http://cmake.org/cmake/help/support.html" rel="noreferrer" target="_blank">http://cmake.org/cmake/help/su<wbr>pport.html</a><br>
CMake Consulting: <a href="http://cmake.org/cmake/help/consulting.html" rel="noreferrer" target="_blank">http://cmake.org/cmake/help/co<wbr>nsulting.html</a><br>
CMake Training Courses: <a href="http://cmake.org/cmake/help/training.html" rel="noreferrer" target="_blank">http://cmake.org/cmake/help/tr<wbr>aining.html</a><br>
<br>
Visit other Kitware open-source projects at <a href="http://www.kitware.com/opensource/opensource.html" rel="noreferrer" target="_blank">http://www.kitware.com/opensou<wbr>rce/opensource.html</a><br>
<br>
Follow this link to subscribe/unsubscribe:<br>
<a href="http://public.kitware.com/mailman/listinfo/cmake" rel="noreferrer" target="_blank">http://public.kitware.com/mail<wbr>man/listinfo/cmake</a><span class="m_-4151677493543721148m_1779837803095218789HOEnZb"><font color="#888888"><br></font></span></blockquote></div><span class="m_-4151677493543721148m_1779837803095218789HOEnZb"><font color="#888888"><br><br clear="all"><div><br></div>-- <br><div class="m_-4151677493543721148m_1779837803095218789m_-2420463097592014405gmail_signature" data-smartmail="gmail_signature"><div dir="ltr"><div><div dir="ltr"><div><div dir="ltr">Craig Scott<br><div>Melbourne, Australia</div><div><a href="https://crascit.com" target="_blank">https://crascit.com</a><br></div></div></div></div></div></div></div>
</font></span></div>
</blockquote></div><br></div>
</div></div><br>--<br>
<br>
Powered by <a href="http://www.kitware.com" rel="noreferrer" target="_blank">www.kitware.com</a><br>
<br>
Please keep messages on-topic and check the CMake FAQ at: <a href="http://www.cmake.org/Wiki/CMake_FAQ" rel="noreferrer" target="_blank">http://www.cmake.org/Wiki/CMak<wbr>e_FAQ</a><br>
<br>
Kitware offers various services to support the CMake community. For more information on each offering, please visit:<br>
<br>
CMake Support: <a href="http://cmake.org/cmake/help/support.html" rel="noreferrer" target="_blank">http://cmake.org/cmake/help/su<wbr>pport.html</a><br>
CMake Consulting: <a href="http://cmake.org/cmake/help/consulting.html" rel="noreferrer" target="_blank">http://cmake.org/cmake/help/co<wbr>nsulting.html</a><br>
CMake Training Courses: <a href="http://cmake.org/cmake/help/training.html" rel="noreferrer" target="_blank">http://cmake.org/cmake/help/tr<wbr>aining.html</a><br>
<br>
Visit other Kitware open-source projects at <a href="http://www.kitware.com/opensource/opensource.html" rel="noreferrer" target="_blank">http://www.kitware.com/opensou<wbr>rce/opensource.html</a><br>
<br>
Follow this link to subscribe/unsubscribe:<br>
<a href="http://public.kitware.com/mailman/listinfo/cmake" rel="noreferrer" target="_blank">http://public.kitware.com/mail<wbr>man/listinfo/cmake</a><br></blockquote></div><br></div></div></div></div>
</blockquote></div><br><br clear="all"><div><br></div>-- <br><div class="gmail_signature" data-smartmail="gmail_signature"><div dir="ltr"><div><div dir="ltr"><div><div dir="ltr">Craig Scott<br><div>Melbourne, Australia</div><div><a href="https://crascit.com" target="_blank">https://crascit.com</a><br></div></div></div></div></div></div></div>
</div>