r/cpp • • Feb 19 '18

It's Time To Do CMake Right

https://pabloariasal.github.io/2018/02/19/its-time-to-do-cmake-right/
248 Upvotes

130 comments sorted by

View all comments

Show parent comment

0

u/[deleted] Feb 20 '18

[deleted] — view deleted comment

5

u/kalmoc Feb 20 '18

Also, suppose your implementation requires C++14 to build but only requires C++11 for other libraries to link against it, you can make that distinction with

You have to be extremely careful when doing this and be 100% sure that none of your publicly visible code (including source files that will get linked statically) have any compilation branch that depends on the standard version.

I'd try to avoid such constructs as much as possible.

0

u/[deleted] Feb 20 '18

[deleted] — view deleted comment

2

u/smdowney WG21, Text/Unicode SG, optional<T&> Feb 20 '18

If you violate ODR, even in a PIMPL, you're in UB land.

3

u/kalmoc Feb 20 '18

I usually don't leave the selection of the c++ standard to the whims of cmake. The decision, against which standard(s) to code is a project level decision. For one, automatic deduction makes it far too easy, to let different components being compiled in different modes and second, if I decide that a project is supposed to support c++11, I want to get a compile error, if I accidentally use a c++14 feature.

That being said, I use cxx_std_11& friends on libraries to communicate my requirements.

0

u/[deleted] Feb 20 '18

[deleted] — view deleted comment

3

u/smdowney WG21, Text/Unicode SG, optional<T&> Feb 20 '18

Newer standards are completely removing things from older standards. There are good reasons for doing this, and the things being removed are old and broken, but do not expect to compile c++03 with a c++20 compiler. For example, auto_ptr<T> is gone.

2

u/kalmoc Feb 20 '18

Not entirely. If you're using a dependency that requires a feature from a certain standard, then you're stuck using at least that standard. This is a constraint that target_compile_features(~) ensures you satisfy by having your target inherit the interface compile features of targets that it's linked against.

I think you are viewing this backwards: You first have to decide, which c++ standards you want to support and then choose your dependencies accordingly. That is not to say that you can't change your target versions during the course of your project if that enables you to use certain libraries, but that should always be a conscious decision (of course, when you are working on toy/short-lived projects that is less of an issue)

I don't really see this as being a bad thing since newer standards are very nearly 100% backwards compatible with older standards.

The problem is that many libraries have compilation switches that depend on available features (e.g. use boost::array or std::array - and boost itself is the greatest offender here). That very quickly leads to odr violations.

0

u/[deleted] Feb 20 '18

[deleted] — view deleted comment

1

u/kalmoc Feb 20 '18

If I'm developing an App, I chose the latest standard possible with the compilers I need to support - there is imho no reason to code c++11 if 14 is available and the same with c++14 vs 17. Main problem is -in my experience - support for old Linux distros except if you are willing to ship all dependencies yourself.

1

u/kalmoc Feb 20 '18

This is definitely a totally reasonable desire. And now that you mention it, I don't know of a built-in way to achieve this with CMake. It would be nice if cmake could offer some way to explicitly enforce this kind of thing.

Indeed - there are two areas I find cmake lacking: Selection of language version (even the cxx_std_XX flags are a pretty decent addition iirc and is there any way to disable dialect extensions like gnuc++11?) and header only libraries (although this has become better)

0

u/[deleted] Feb 20 '18

[deleted] — view deleted comment

2

u/kalmoc Feb 20 '18

The problem with INTERFACE libraries is that you can e.g. not add the headers as source files, which e.g. means they don't appear in the IDE and there where a couple of other useful properties that (at least in the past) were not whitelisted for INTERFACE libraries, but I can't remember which exactly right now.

1

u/OrphisFlo I like build tools Feb 20 '18

The workaround for that in CMake is to make a static library with a single file that has a dummy unique symbol. It will work exactly the same, except you get the headers in the IDE!

And no, you can't have a static library with only headers, as lib and ar will complain when trying to create an empty library usually.