0

This is my project tree-like format :

├── cmake       
│  └── CopyHeaderFiles.cmake    
├── CMakeLists.txt   
├── common   
│   ├── CMakeLists.txt   
│   ├── Headers   
│   │   ├── CMakeLists.txt       
│   │   ├── criticalmessage.h        
│   └── Src    
│       ├── CMakeLists.txt   
│       ├── criticalmessage.cpp     
├── includes    
│   └── CMakeLists.txt    
├── main.cpp    

As you see i have a module (common) in my project, my goal is copy all headers file from PROJECT_SOURCE_DIR/common/Headers to PROJECT_BINARY_DIR/includes with add_custom_command.
this is common/CMakeLists.txt

    cmake_minimum_required (VERSION 3.0)
    set(MODULE_NAME common)
    add_subdirectory(Headers)
    add_subdirectory(Src)
    include(${PROJECT_SOURCE_DIR}/cmake/CopyHeaderFiles.cmake) 
    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -lpthread")
    include_directories("${CMAKE_SOURCE_DIR}/common/Headers" )
    add_library(${MODULE_NAME} STATIC ${SRC_LIST} ${INCLUDE_LIST})
    set(LIBRARY_OUTPUT_PATH ${PROJECT_BINARY_DIR}/lib)

And content of cmake/HeaderFiles.cmake:

foreach( headerfile ${INCLUDE_LIST})

    add_custom_command(
        OUTPUT ${headerfile}
        COMMAND ${CMAKE_COMMAND} -E copy    
        "${PROJECT_SOURCE_DIR}/${MODULE_NAME}/Headers/${headerfile}"        
        "${PROJECT_BINARY_DIR}/includes"    
         )

endforeach( headerfile )

But no files exist in include directory and always be empty after build my project.
Where am i wrong?

einpoklum
  • 118,144
  • 57
  • 340
  • 684
HP_perfect
  • 95
  • 1
  • 11
  • Documentation of add_custom_command says the following: "A target created in the same directory (CMakeLists.txt file) that specifies any output of the custom command as a source file is given a rule to generate the file using the command at build time." (https://cmake.org/cmake/help/v3.10/command/add_custom_command.html) Does any of your targets depend on add_custom_command's outputs? Another question is whether this is the thing you really need? Maybe using file(COPY) or install commands would work better? – joe_chip May 26 '18 at 06:28
  • You're right .My project have 4 modules(common, dataaccess, dataprocess, signalprocess) and all of them create static library. I want to add common headers in dataaccess module with this way `#include "common/criticalemssage.h"` but i cant. and must use `#include "../../common/headers/criticalmessage.h"` . what is the best way for this? – HP_perfect May 26 '18 at 06:43
  • 1
    You should use `target_include_directories` in that case. You can do: `target_include_directories(common INTERFACE ${CMAKE_CURRENT_SOURCE_DIR}/dir_with_headers/)` and then `target_link_libraries(dataaccess PRIVATE common)`. `target_link_libraries` will also add `INTERFACE` (and `PUBLIC`) include dirs from `common` to `dataaccess`. – joe_chip May 26 '18 at 06:51
  • By the way, you might consider editing your question. Proper answer would be more readable than comments. – joe_chip May 26 '18 at 06:53
  • I do your solution with target_include_directory. but not working and just `#include "ctiticalmessage.h"` shows up, instead of `common\criticalmessage.h` – HP_perfect May 26 '18 at 07:18
  • In that case, path passed to `target_include_directories` must be path to parent of `common` directory – joe_chip May 26 '18 at 15:44
  • tnx, works ... but still have some problem : 1. It shows all directory and files in common module. 2. I should add `common/Headers/file.h` but i want to add `common/file.h` how can solve these? – HP_perfect May 27 '18 at 05:15
  • 1
    Honestly, I would just put headers in `common/Headers/common/` instead of `common/Headers/`. Copying headers to another directory when building seems like an easy way to make mistakes - e. g. you might accidentally edit a header in your build dir, and then loose those changes after rebuild. – joe_chip May 27 '18 at 07:34
  • Wow, you're right, Thank you again – HP_perfect May 27 '18 at 10:07

0 Answers0