Situation:
I am developing a C++ library managed by CMake that I intend to use in multiple projects. The library has some compile-time configuration that propagates to the defintions of custom "defines" using (e.g.) target_compile_definitions(my_library PUBLIC "CUSTOM_DEF_OPTION")
. These definitions affect the generated code both in cpp files and headers.
When I am linking this library to another library or executable as part of the same CMake project (using target_link_libraries(my_executable my_library)
, the PUBLIC defintions are properly carried over to the compilation of the executable.
Problem:
However, this does not work in case the library is used in some external project, managed e.g. by Visual Studio or Eclipse, because the compile definitions are not carried over. This results in discrepancies between the effective header code and already-built library.
Therefore, I need to find a way how to easily apply the PUBLIC compile definitions in the projects using this library.
Question:
I was thinking about using CMake's configure_file(...)
to generate a special header file that would contain the definitions and will be included in the library project instead of using target_compile_definitions
. It seems I need to manually craft the template file and logic, which duplicates already existing logic for adding target compile defintions.
Is this a correct approach? Is there a way to generate such file automatically? Is there some other more-elegant approach that would work?
question from:
https://stackoverflow.com/questions/66046003/keep-compile-definitions-when-using-cmake-built-library-that-has-custom-target-c 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…