I am using CMake to build different C++ libraries, the whole thing can be summed up like this :
I now need to create a lib c that depends on b. Do I need to link c only on b ? or on b and a because b depends on a ?
target_link_libraries(c b) or target_link_libraries(c b a) ?
Thanks
target_link_libraries is probably the most useful and confusing command in CMake. It takes a target ( another ) and adds a dependency if a target is given. If no target of that name ( one ) exists, then it adds a link to a library called one on your path (hence the name of the command).
There is negligible performance penalty for having two separate target_link_libraries calls.
Adds a library target called <name> to be built from the source files listed in the command invocation. The <name> corresponds to the logical target name and must be globally unique within a project. The actual file name of the library built is constructed based on conventions of the native platform (such as lib<name>.
In your code building library b, you should tell CMake that b depends on a:
target_link_libraries(b a)
Then, your library/application c can link to only what it uses and not have to worry about dependencies of dependencies:
target_link_libraries(c b)
Library a will be pulled in for you.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With