CMake: Splitting (independent) libraries into diff

2019-03-21 07:14发布

Say I have a target A that depends on the libraries B and C. But B and C have no mutual dependence. The linking with CMake could look like

target_link_libraries( A B C )

but

target_link_libraries( A B )
target_link_libraries( A C )

also seems to work (and might be more easy to maintain). Are there disadvantages of splitting the target_link_libraries command into multiple commands? Or should one always put it into a single command in case one eventually does encounter a mutual dependence of the libraries?

标签: linker cmake
2条回答
可以哭但决不认输i
2楼-- · 2019-03-21 07:36

Those are exactly equivalent. Both say that A depends on B and A depends on C. Neither says anything about any dependency between B and C, so there is none.

I'm not sure what you mean by "mutual dependence" -- when considering B and C, there are 4 possibilities: (1) neither depends on the other, (2) B depends on C, (3) C depends on B, or (4) they both depend on each other.

(1) is what you have. (2) and (3) would mean you should add another target_link_libraries command with either "B C" as the args or "C B" respectively. (4) means you have a circular dependency and they really shouldn't be separate libraries at all, but combined into a single logical entity. You should avoid (4) because it makes it impossible to load as shared libraries on some (all?) platforms.

There is negligible performance penalty for having two separate target_link_libraries calls. I doubt you could measure the performance and show any significant timing differences.

To clarify, this call:

target_link_libraries(A B C)

means target A requires libraries B and C.

If, instead, you consider case (2) above, where B depends on C, you would instead write:

target_link_libraries(B C)
target_link_libraries(A B)

which means target B requires library C, and target A requires library B (and CMake automatically transitively propagates B's dependencies to A so that you do not have to know about any A -> C dependence unless you have explicit code that calls functionality in library C).

You should always express the minimum dependency information necessary to link things together.

查看更多
够拽才男人
3楼-- · 2019-03-21 07:41

Both ways are valid and equivalent. Basically target_link_libraries command just appends specified libraries to the LINK_INTERFACE_LIBRARIES property of the passed target (A in your sample).

查看更多
登录 后发表回答