How to properly add include directories?

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

How to properly add include directories?

David Aldrich-2
Hi again,

My CMakeLists.txt file for my shared library contains:

add_library(MyLib SHARED my_source.cpp etc.)
target_include_directories( MyLib PRIVATE ../MyHeaders)

The library builds ok, but there is no dependency on directory ../MyHeaders - touching a header file does not result in a re-compile of dependent source files.

There's a discussion here:

https://stackoverflow.com/questions/13703647/how-to-properly-add-include-directories-with-cmake  
Opinion seems divided over whether or not it is necessary to add the header files to the list of source files for the target, e.g.:

set(SOURCES file.cpp file2.cpp ${YOUR_DIRECTORY}/file1.h ${YOUR_DIRECTORY}/file2.h)
add_library(test ${SOURCES})

Please will you tell me what is the best practice?

--

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: [EXT] How to properly add include directories?

Bon, William

Hello,

Have you try to add the header files in the add_library command to explicitely add a dependency over it ?

add_library(MyLib SHARED my_source.cpp ../MyHeaders/header.hpp etc.)

On 10/21/19 4:16 PM, David Aldrich wrote:
WARNING: External email, please be mindful before clicking or replying.

Hi again,

My CMakeLists.txt file for my shared library contains:

add_library(MyLib SHARED my_source.cpp etc.)
target_include_directories( MyLib PRIVATE ../MyHeaders)

The library builds ok, but there is no dependency on directory ../MyHeaders - touching a header file does not result in a re-compile of dependent source files.

There's a discussion here:

https://stackoverflow.com/questions/13703647/how-to-properly-add-include-directories-with-cmake  
Opinion seems divided over whether or not it is necessary to add the header files to the list of source files for the target, e.g.:

set(SOURCES file.cpp file2.cpp ${YOUR_DIRECTORY}/file1.h ${YOUR_DIRECTORY}/file2.h)
add_library(test ${SOURCES})

Please will you tell me what is the best practice?


--

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: How to properly add include directories?

fdk17
In reply to this post by David Aldrich-2


On Mon, Oct 21, 2019, at 10:16 AM, David Aldrich wrote:
Hi again,

My CMakeLists.txt file for my shared library contains:

add_library(MyLib SHARED my_source.cpp etc.)
target_include_directories( MyLib PRIVATE ../MyHeaders)

The library builds ok, but there is no dependency on directory ../MyHeaders - touching a header file does not result in a re-compile of dependent source files.

There's a discussion here:

Opinion seems divided over whether or not it is necessary to add the header files to the list of source files for the target, e.g.:

set(SOURCES file.cpp file2.cpp ${YOUR_DIRECTORY}/file1.h ${YOUR_DIRECTORY}/file2.h)
add_library(test ${SOURCES})

Please will you tell me what is the best practice?
-- 


What generator are you using?
That thread seems to contain a lot of misunderstandings about the issue.

If you are using the Makefile generator then refer to https://stackoverflow.com/questions/7461000/handling-header-files-dependencies-with-cmake. After building the project for the first time inspect the depend.make files that are generated to verify if the headers are being picked up correctly as a dependency.

--
F

--

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: How to properly add include directories?

David Aldrich-2
>What generator are you using?

Ninja
 
That thread seems to contain a lot of misunderstandings about the issue.

If you are using the Makefile generator then refer to https://stackoverflow.com/questions/7461000/handling-header-files-dependencies-with-cmake. After building the project for the first time inspect the depend.make files that are generated to verify if the headers are being picked up correctly as a dependency.

Thanks, I made a mistake and was touching the wrong header file. Sorry about that. Dependencies are correctly generated and working. There seems to be no need to add the header files to the list of sources.

BTW It seems that dependencies for header files in the same path as the sources are generated implicitly. Is that expected?

Thanks for your help. 
 

--

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