
Some nice and accurate CMake tips - Topolomancer
http://bastian.rieck.me/blog/posts/2018/cmake_tips/
======
callumprentice
I found CMake scripts really hard to grok but I think that was because I was
trying to learn by reading ones other people had made.

I found it helpful to build a simple app composed of a few C++ files, headers
and library dependencies and then write the bare minimum CMake script to build
it. After that, I just keep iterating, adding CMake features, turning on or
off C++ features, making it work on other platforms, compilers, bit-widths
etc.

Now, I can't imagine starting a C++ project without using it.

------
electricslpnsld
> INCLUDE_DIRECTORIES( ${FOO_INCLUDE_DIRS} )

This is the 'old' way of doing CMake -- the 'modern' transitive variants like
target_include_directories and target_link_libraries have many advantages
relative to the older usage.

~~~
Topolomancer
You are right; I only used this to illustrate the module, though. Will change
it at once, thanks!

------
flohofwoe
Little known cmake feature: instead of running "make" after running cmake, do
a 'cmake --build .':

$ mkdir build

$ cd build

$ cmake ../

$ cmake --build .

This has the advantage that it works for every generator on every platform,
since cmake knows how to invoke the right command line build tool for the
generator (make, ninja, msbuild, xcodebuild etc).

------
rekado
"Instead of fiddling around with autotools madness..." \--- in my years of
packaging software I have found autotools to be the least maddening of all
build tools. Autotools is weird for developers, but it's remarkably consistent
and easy to use for users (such as packagers).

------
cypher543
Note that the variables in the "Detecting an operating system" section are for
detecting the _target_ operating system, not the host (what CMake is running
on). If you use a toolchain file to cross-compile from Linux to Windows, for
example, WIN32 will be defined instead of UNIX.

------
captainbenises
You don't have to write cmake commands in UPPERCASE ANY MORE.

~~~
Topolomancer
I know and I never claimed that you would have to. It is just my personal
preference. Is there an equivalent of PEP-8 for CMake?

------
eslaught
Wouldn't it be preferable to set the C++ standard with:

    
    
        set_property(TARGET tgt PROPERTY CXX_STANDARD 11)
    

That way if you have multiple targets you're not setting it everywhere?

~~~
cypher543
The target property overrides the global setting, so you can set
CMAKE_CXX_STANDARD as the default and then set the target property if you
really need a different standard for some reason. And if you have a whole sub-
project that needs a specific version, you can set CMAKE_CXX_STANDARD before
calling add_subdirectory to have it apply to only the targets in that
directory.

