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
169 views
in Technique[技术] by (71.8m points)

CMake multiple calls to project()

What are the implications of / possible reasons for calling project() from subdirectories' CMakeLists?

i.e.

├── CMakeLists.txt     <-- top-level project() call
├── proj1
|  ├── CMakeLists.txt  <-- project()
├── proj2
|  └── CMakeLists.txt  <-- project()
└── proj3
   └── CMakeLists.txt  <-- project()

I have seen this in codebases before, usually after a refactor.

Is this ever useful? Couldn't you just define targets with add_library/add_executable instead?

Is this actually harmful, or innocuous?

The docs only really talk about the special case of the top-level project() call, but don't get into reasons for calling it in subdirectories.

question from:https://stackoverflow.com/questions/65945378/cmake-multiple-calls-to-project

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

1 Reply

0 votes
by (71.8m points)

The benefit of doing this is that you could use one of your subprojects on its own or use it as part of some other project that does not require its siblings.

There should be no issue with doing this without the parent dir. Afaik there is no issue with this. In fact the documentation of PROJECT_NAME states that there could be multiple project commands in use:

[...]

This is the name given to the most recently called project() command in the current directory scope or above. To obtain the name of the top level project, see the CMAKE_PROJECT_NAME variable.

I work on a software package that contains > 10 project commands and there never was an issue with that.

The visual studio generator creates one solution file per project() though but a unnecessary files in the build directory shouldn't be an issue.


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

...