Блог пользователя snorkel

Автор snorkel, история, 3 года назад, По-английски

How can I include a header file without writing #include "header.h" for example using cmakelists.txt?

For example without writing #define DEFINE I defined it from cmakelists with the command add_definitions(-DDEFINE)

Can I do the similar thing?

  • Проголосовать: нравится
  • +6
  • Проголосовать: не нравится

»
3 года назад, # |
Rev. 2   Проголосовать: нравится 0 Проголосовать: не нравится

Depends on the compiler. E.g. GCC has -include header.h key, see docs.

You can use it in CMakeLists.txt via add_compile_options, e.g. add_compile_options(-include header.h). Note that this will include header.h in each translation unit ("source file") for all targets.

  • »
    »
    3 года назад, # ^ |
      Проголосовать: нравится 0 Проголосовать: не нравится

    This does not work for me. I tried both Cygwin and MinGW.

    • »
      »
      »
      3 года назад, # ^ |
        Проголосовать: нравится 0 Проголосовать: не нравится

      Make sure add_compile_options is specified before add_executable, like this.

      Alternatively, use target_compile_options(main PRIVATE -include pre-main.inc.cpp) after add_executable(main main.cpp).

      • »
        »
        »
        »
        3 года назад, # ^ |
        Rev. 3   Проголосовать: нравится 0 Проголосовать: не нравится

        If I write it before add_executable it says header.h: No such file or directory. My Cmake looks like this:

        "untitled1" is the project name.

        cmake
»
3 года назад, # |
  Проголосовать: нравится +1 Проголосовать: не нравится

bump