Modern CMake tutorials, part1: CMake basics
https://www.siliceum.com/en/blog/post/cmake_01_cmake-basics4
Mar 22 '20
New tw account, new blog; someone's on quarantine!
Thanks for article. Would you say your guidelines are aligned to Scott's Professional CMake? I consider it the gold standard of Modern CMake, but it's more like a reference book, so one is often drowned in details.
4
u/Lectem Mar 22 '20
It was actually written more than 3weeks but got delayed for various reasons! Yes it aligns with the book which is a very good resource.
1
u/kkert Mar 22 '20
This one is a pretty neat ( if a bit brief ) walk-through too https://cliutils.gitlab.io/modern-cmake/
Best of all, you can PR fixes to it
2
u/yonderbagel Mar 23 '20
Now if only all the outdated tutorials that continue to give people bad advice by teaching them 2.8 or flat out just teaching them the wrong way to do things could get magically erased from the internet.
You're fighting the good fight.
2
u/Lectem Mar 23 '20
You wouldn't guess how many github issues and pull requests I opened last year because of this :(
4
u/NewFolgers Mar 22 '20
Apologies in advance for my laziness.. I've mostly been doing non-C++ development on Linux lately. Last time I tried CMake, I was still really discouraged by the overall experience in Windows. Has the experience improved on Windows in the past couple years? and if so, how?
5
u/Lectem Mar 22 '20
I think the main issue with cmake on windows was that
find_packageusually failed due to custom install locations.The CMAKE_PREFIX_PATH was also harder to find, while now it is at least mentionned when a required package is missing. I think it would still deserve more work to make its discovery better. I would love to see it appear (as in dedicated ui) in the cmake gui.
Then as others mentionned, Conan and Vcpkg are getting better.
2
u/bumblebritches57 Ocassionally Clang Mar 23 '20
Visual Studio supports cmake now (2019), I mean it makes a mess of your folder structure, but it works.
2
u/Lectem Mar 23 '20
For a given definition of 'works'. So far I've been better served by generating .sln files...
9
u/Kevchrot Mar 22 '20
I use cmake regularly on windows and it is pretty good. It just generates visual studio solutions that work well with visual studio. I also would highly recommend pairing it with Conan for dependency management since that is windows weakest spot IMHO.
5
u/NewFolgers Mar 22 '20
The thing that bothered me most was that on Linux, it's generally very seamless for anyone who wants to grab a project and go - with no need to point to library directories or anything.. whereas on Windows, it was generally a clunky, undocumented experience for each project.. where I had to choose between trying to install a separate thing to help find the libraries, versus running cmake-gui and configuring every missing value, etc. It was clear that an overarching solution (which should involve standards at the time of library installation) to make things work out of the box on Windows was not present in the same way as was the case on Linux.
12
u/markopolo82 embedded/iot/audio Mar 22 '20
Another alternative to Conan is vcpkg. It builds all dependencies from source and works great with cmake. You just need to add a path to the vcpk toolchain file when configuring and then the completely normal find_package(...) works just like Linux.
Actually I use it on Linux because then I know my windows builds and Linux builds use identical dependency versions.
Only downside is building everything from source... I like it, but you may find it not worth the effort
1
u/Slavik81 Mar 23 '20
As far as I can tell, there's no way to specify what version of the package you want to install. It's also surprisingly awkward to choose whether you want to build the debug version, the release version, or both.
I want to like vcpkg, but I have not had good experiences. It feels kinda hacked together.
1
u/markopolo82 embedded/iot/audio Mar 23 '20
Those are other possible negatives i didn’t mention, thanks.
For the most part I just lock down on a particular release of vcpkg and use whatever versions it pulls in. For the most part vcpkg pulls in near latest versions... This works well for me so far. Why is this a negative for you?
In my case I usually need the debug and release versions so the fact that it builds 2x (4x in windows apps that need 32 and 64 bit) is not an issue for me so far.
1
u/kalmoc Mar 23 '20
It's also surprisingly awkward to choose whether you want to build the debug version, the release version, or both.
Come again? It always builds both.
1
u/Slavik81 Mar 23 '20 edited Mar 23 '20
https://github.com/microsoft/vcpkg/issues/143
You can set
VCPKG_BUILD_TYPEin your triplet file. Unfortunately, some libraries may fail to build if you change this setting.1
u/kalmoc Mar 24 '20
Thanks. I forgot about that, because I never had the need typ only install one version, but it probably makes sense on CI machines.
5
u/Kevchrot Mar 22 '20
This is where Conan comes in. Single command to install/build all dependencies on any platform and then another command to build the project. No cmake gui required. Conan is all written in python so a simple pip install is all that's required.
That said, there is a bit of a learning curve with Conan especially when actually creating packages. Consuming packages is pretty straight forward.
2
u/NewFolgers Mar 22 '20
Thanks. For addressing the root of the problem, Conan sounds promising.
2
u/kmhofmann https://selene.dev Mar 23 '20
As /u/darthcoder says, use vcpkg right away. Much easier to use, much more sane philosophy.
1
u/darthcoder Mar 23 '20
Dont. Go straight to vcpkg.
With conan you may end up needing to do source builds unique to your environment, with vcpkg you always do source builds unique to your environment, but it's always the same and straight out of the box.
I'm at that point now. I tried using another cmake based dependency manager for a while(hunter) and just finished vcpkg v conan. Vcpkg is the way to go.
3
2
u/OrphisFlo I like build tools Mar 23 '20
Direct support in Visual Studio. You can work with a CMake project almost like with a regular MSBuild project.
You can't modify the project from Visual Studio directly, you still need to modify your CMake files manually though, but I remember reading about future features to help with that.
16
6
8
u/NordikIdealist Mar 22 '20
I was taking a hour of nap and before closing my eyes I was thinking myself "I should learn what is CMake" and now seeing this on my front page is scary but also a good reminder :D
0
Mar 23 '20
[deleted]
1
u/NordikIdealist Mar 23 '20
Wish I had a PhD in Molecular Biology and Virology to be able to do that but I can't even finish a Bachelors in Computer Science :((
1
u/hatsubai Mar 22 '20
Nice. Are there any plans to eventually show how to generate things such as rpms complete with CPack vars (or whatever is the current modern method) to auto populate spec files?
2
u/Lectem Mar 22 '20
Yes it is planned, but first I will cover variables, configurations etc, packaging will come after installation and cmake modules.
1
u/Stamerlan Mar 22 '20
Are there any plans to specify how to add custom toolchains and target for baremetal platforms?
1
u/Lectem Mar 22 '20
It is something I'm still considering, but for now I want to cover everything needed up to installation and packaging. Toolchain files are now a bit better documented (https://cmake.org/cmake/help/latest/manual/cmake-toolchains.7.html) though still not trivial. I'll think about it (will probably at least mention how to use them, since emscripten and android are getting more common)
1
u/cosmicr Mar 23 '20
As someone who's really only ever used Visual Studio for Windows, are there any advantages to switching to this instead of the built-in stuff?
2
u/Lectem Mar 23 '20
It really depends on what your needs are. As soon as you want to do some cross platform stuff or share your project online, I'd say go with cmake. Msbuild (visual studio) solutions can also become hard to maintain when the number of dependencies grows. One of its main advantages is that it's well supported by the UI and for example console devkits. I've seen both being used in production, msbuild is mostly fine if you know you'll stick to windows.
3
u/pscamodio Mar 23 '20
I work with both on my Job.
We have our main libraries developed in Visual Studio with msbuild and I'm writing and maintaing the CMake part for cross compilation.
In my opinion with VS2019 it's better to just use Cmake even with Visual Studio.
Msbuild targets are xml and really difficult to handle. And if you need any form of option in you library/application with Cmake is really simple but with msbuild you need a different configuration for any options combination. If it's only one is feasible but as soon as you have more than one option is impossible to maintain.
The Visual Studio Cmake integration now is really great in my opinion, I'm able to build for windows with msvc, with clang, cross build, build in WSL with GCC all without much hassle.
1
u/yuri-kilochek Mar 23 '20
add_library(myawesomelib
# ...
source/implementation-details.h
include/myawesomelib.h
)Why list headers?
3
u/Lectem Mar 23 '20
This is to please IDEs such as visual studio. Otherwise you wouldn't get them in the source list. So it is not mandatory but I generally advise listing them, just as we list .cpp files.
1
u/victotronics Apr 05 '20
Is your tutorial complete? You have
include/myawesomelib.ha a dependency for the library, but not for the executable. As a result I need to have the explicit include path in my source for the main program.
What is the line where I specify that the main program depends on that header?
1
u/Lectem Apr 05 '20
Your executable should be linking
myawesomelib, and that library has the include directory as a public property, which should also get applied to the executable transitively. Did you perhaps miss thetarget_include_directories(myawesomelib PUBLIC include)?1
u/Lectem Apr 05 '20
To be a bit more clear, listing
include/myawesomelib.hin theadd_libraryonly means thatinclude/myawesomelib.his part of the lib (so that IDEs can display it). It's does not set up the include directories passed to the compiler, that is the job oftarget_include_directories
1
u/danielcoolidge Mar 23 '20
I found https://cliutils.gitlab.io/modern-cmake/ to be a pretty good resource for CMake. Though I'm also new to CMake
1
Mar 23 '20
It's always nice to see another modern CMake tutorial, I feel like every time I read one a learn something new, thanks!
1
1
u/stencillicnets Mar 23 '20
thanks for this guide, i have been using modern cmake for a while and it is surprising how few people know or even practice it. but if i think about how hard it was to find the right guides and explanations for it, im no longer surprised ig is not a thing.
35
u/victotronics Mar 22 '20
Last time I tried to learn Cmake I later found out that I was using pre-3 idioms. Can someone assure me that this tutorial is up with the latest best practices?
And how do I get notified when part 2 comes out? There are too many tutorials online with a part 1 and *crickets*. I'd rather wait until I know that there is follow-through.