Library include paths with same header name

后端 未结 5 1526
慢半拍i
慢半拍i 2020-12-30 03:42

What is the best method to include a file that has same name in another folder from additional include directories?

Example:

lib1/include/foo.h
lib2/         


        
5条回答
  •  有刺的猬
    2020-12-30 04:32

    I would include the files from a unambiguous directory which is in the -I list (That is to say the path to the directory which contains lib1 and lib2):

    #include "lib1/include/foo.h"
    #include "lib2/include/foo.h"
    

    Therefore there will be no ambiguity because the precompiler will look at lib1/include/foo.h within its directory list and that don't happen from lib1/include/ or lib2/include/ but only from the parent directory.

    As said earlier: take care with the guards, even if for a lib header, the name should include the lib name to avoid such confusions.

提交回复
热议问题