2

I am attempting to use the generate_export_header function. My library source code is in the src folder, so CMake generates the export header at src/mylib_export.h. In order, to use this export header do I just copy it into my library's include folder to use in the implementation code? Here is a snippet of the CMake code I'm using:

ADD_LIBRARY(${PROJECT_NAME} ${LIB_TYPE} ${SOURCES})
GENERATE_EXPORT_HEADER(${PROJECT_NAME})

FILE(COPY 
    ${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}_export.h 
    DESTINATION 
    ${PROJECT_SOURCE_DIR}/include
)

Is there a more CMake way of doing this?

2
  • 2
    I add the binary folder to the include_directories and do not copy the export header at all.
    – drescherjm
    Commented Oct 5, 2012 at 2:09
  • 1
    Thanks! If you add this as an answer, I'll accept it.
    – mevatron
    Commented Oct 8, 2012 at 15:20

2 Answers 2

6

When I do this with CMake, I normally add the binary folder to the include_directories and do not copy the export header at all.

This way the source tree does not have any generated parts in it and this makes it simpler to use with source control and also I usually build my projects with more than 1 build tree per source tree mostly to allow building under more than 1 compiler or building with different settings or separate 32bit / 64 bit build trees with the same compiler.

1

I prefer copying export.h into source folder and adding it into .gitignore, same thing for application specific config.h.

generate_export_header (
  ...
  EXPORT_FILE_NAME "${CMAKE_CURRENT_SOURCE_DIR}/export.h"
)
configure_file ("config.h.in" "${CMAKE_CURRENT_SOURCE_DIR}/config.h")

The reason is clang code highlighting and formatting. It doesn't matter how export.h and config.h looks just now (these files will be updated before build), you need just your code properly highlighted and formatted in text editor with empty build folder.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.