jsoncpp icon indicating copy to clipboard operation
jsoncpp copied to clipboard

CMake target with namespace

Open xkszltl opened this issue 5 years ago • 4 comments

Is your feature request related to a problem? Please describe.

We found a case in our CMake script where jsoncpp, rather than jsoncpp_lib, is mistakenly used in target_link_libraries(). As a result, a system installed jsoncpp is linked by -ljsoncpp, not what we found with find_package().

Describe the solution you'd like

I think the more popular solution is to create namespaced targets like jsoncpp::shared and jsoncpp::static, and maybe also jsoncpp::jsoncpp pointing to the default.

Describe alternatives you've considered

Add a target jsoncpp equivalent to jsoncpp::jsoncpp mentioned above, to shadow the file version of jsoncpp.

xkszltl avatar Nov 02 '20 16:11 xkszltl

I don't know much about cmake namespace target, but I'll investigate it late. And for now, can you see if this PR https://github.com/open-source-parsers/jsoncpp/pull/1214 can solve your problem?

dota17 avatar Nov 04 '20 03:11 dota17

Not really. What I mean is more like this: https://github.com/google/glog/blob/c8f8135a5720aee7de8328b42e4c43f8aa2e60aa/CMakeLists.txt#L542 https://github.com/google/glog/blob/c8f8135a5720aee7de8328b42e4c43f8aa2e60aa/CMakeLists.txt#L850

xkszltl avatar Nov 04 '20 06:11 xkszltl

I mention namespacing because, when people see jsoncpp or jsoncpp_lib in target_link_libraries(), it's hard to tell whether it's a file or target. Although the intention is to use target, when editing around people can easily make mistakes and write it as jsoncpp, which can still "work" when linker found a file matching the name. We want to avoid this.

xkszltl avatar Nov 04 '20 06:11 xkszltl

See #1374

Andres6936 avatar Dec 27 '21 15:12 Andres6936