CMake和C++项目中的“命名空间”包含路径在将项目集成在一起时有好处吗?[已关闭]

cidc1ykv  于 2022-12-15  发布在  其他
关注(0)|答案(2)|浏览(114)

Closed. This question is opinion-based . It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post .

Closed 6 years ago.
This post was edited and submitted for review 46 mins ago.
Improve this question
While orienting myself to one of the open source C++ project I found a line of code in the root CMakeLists.txt file:

include_directories(${PROJECT_SOURCE_DIR}/../include)

And then in one of the source files there is this line:

#include "someFolder/someFile.h"

someFolder is found in include folder.
I have seen a different approach in another project, in which the CMakeLists.txt has something like this:

include_directories(${PROJECT_SOURCE_DIR}/../include/someFolder)

then in the source file:

#include "someFile.h"

The first approach typically "namespaces" the include path by the name of the project the header belongs to. Are there common benefits to this when integrating multiple projects together? If so, what are those common benefits?

czq61nw1

czq61nw11#

I prefer subdirectories for include files.
The main reason for this is to avoid file name conflicts. If dependency A has a file called someFile.h , and dependency B also has a file called someFile.h , you got a problem, because the compiler doesn't know which one to include.
So for the same reason you should use namespaces, you should also use subdirectories for include files when possible.

bmp9r5qi

bmp9r5qi2#

Well, this is very opinion based, in my opinion...
I prefer the former approach, especially for larger libraries. It exhibits the logical structure of the library as intended for by the authors.

相关问题