Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
108 views
in Technique[技术] by (71.8m points)

c++ - Keep compile definitions when using CMake-built library that has custom target_compile_defintions

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...