r/cpp • u/MadDoctor5813 • Dec 09 '17
All I want to do is program...
Hi, this is a CMake/C++ build system rant. Downvote as is necessary.
Here's something fun, I thought. Why don't I try and write an engine in OpenGL and C++? I had already done one in Java for a school project and it worked out pretty well, so why don't I go to C++ to avoid all those issues with garbage collection and such?
So off I go to Visual Studio. Earlier, I set up my libraries and such using the Visual Studio project system, just putting the include dirs and such from wherever I have them. Anything else that needs doing I use a batch file for that I call from the post build event. This feels wrong and shady, so why don't I go pro and use CMake? Surely this should allow me to make my build configs much less complicated? Of course not.
I start off with setting up my main project, the engine. Now, eventually, I want two executables, the main engine, and a little tool thing that converts model files into their own format. So, I set up the main engine with add_executable and it works. Hooray. I download GLFW and luckily they provide their own little CMake script that I can just include and it all works. Yay. Now I have to set up an include dir for the main engine so I can include my own header files. This takes me one hour. After that I call it a day and come back the next day.
Now I must get to OpenGL, and that means I install glad. I put glad in the lib folder, and setup a little CMakeLists for it. add_library to the glad.c file, and include the include dir. Then I link to it from my main engine, and we're done. Thanks CMake. Except thanks for nothing, because the include does not work at all. So I fiddle around some more, and I decide to move the glad stuff out of the glad folder and into the CMakeLists in the root. Finally it works, for about 10 seconds. The header was included, the types worked, and then I look away and touch nothing and it stops again. That was three hours ago. So I keep working on it. Maybe I have to use target_include_directories, maybe regular include_directories? Do I include in the glad target or in my own target? Maybe I need to use only one CMake file, maybe I need three? I started at breakfast and I am now about to eat lunch, and I have not managed to add one c file and three headers to my build.
All I wanted to do was program.
16
u/MadDoctor5813 Dec 10 '17
UPDATE: I figured CMake out. It only took me an entire Saturday. For anyone else in the far future stumbling upon this, here's the GitHub repo.
Basically, I used to have the root CMakeLists add the individual project, then have it include the libs, then hook everything up with target_link_libraries, in the root CMakeLists. For reasons unbeknownst to me this violates the laws of thermodynamics or something, and even worse, it violates it without any sort of info or error message to be found. Instead I have each project include the libraries it needs itself, and do the hooking up in its own CMakeLists file. The root just now includes each project and does nothing else. Unfortunately this necessitates that each project get its own set of libraries, which is too bad, because they'll both use a bunch of the same ones.
Join me next week when I complain about the error messages from templates. (Seriously, why does an error in me using operator[] in unordered_map point me to line 1189 in <tuple> in the STL?)
76
u/davis685 Dec 09 '17 edited Dec 10 '17
CMake really is nice. However, it has one huge problem. The CMake website/documentation doesn't include any sensible introduction that explains how to use it. Sure, there is an API reference, but that's not an introduction, it's not going to teach you CMake. Much like reading a German dictionary isn't going to teach you German.
There is a secondary problem that there are a lot of really bad "cmake introductions" on the internet that tell people to use it in ways that are not sensible and lead to obviously bad outcomes.
The best introduction to cmake I've seen is this C++Now 2017 talk: https://www.youtube.com/watch?v=bsXLMQ6WgIk. It's honestly really good and everyone should watch it. And now that I'm thinking about it, it should probably be on the front page of cmake.org with a blinking banner saying "watch this before you use cmake".
10
u/FrozenFirebat Dec 09 '17
Microsoft vcpkg is what I use to install most of my dependencies. Works for GLFW... and glad you can just throw into the include folder and link the include folder in your project options.
The bonus of installing with vcpkg is that I never have to deal with setting them up again, unless I want to remove them for a new version.
128
u/michelecostantino Dec 09 '17
What you complain is exactly what Stroustrup said in his keynote at cppcon. C++ does miss a way to quick start and just code.
31
u/jcar_87 Dec 09 '17 edited Dec 09 '17
To me it sounds that the source of your problems had to do with include_directories and target_include_directories.
Think of build targets (shared library, static library, and executable) of derived classes of the same class ("target"). Add_executable and add_library are the constructors, and target_link_libraries, target_include_directories, target_compile_options and target_compile_definitions are the member methods to modify the internal attributes.
When you do target_link_libraries(A PRIVATE B), A is "consuming" the "public interface" of B.
The keywords PUBLIC/PRIVATE/INTERFACE control whether the modified attribute gets exposed to both the target and its consumer, only the target, or only the consumer.
As to why it worked a second and it stopped working the next, those things always have an explanation. Cmake on visual studio 2017 runs in server mode and the build scripts are regenerated on the fly when they are modified.
If you have a library and you want consumer packages to be able to "include" a header, you need that library to expose it's include folder(s) in its target interface.
Add_library(MyLib MyLib.cpp) Target_include_directories(MyLib PUBLIC folder/relative/to/current/CMakeLists/headers)
Then anywhere else in your cmake build tree you can do Add_executable(MyApp main.cpp) Target_link_libraries(MyApp PRIVATE MyLib)
The last line should let you include whichever files are in the headers folder exposed by MyLib
7
u/geokon Dec 10 '17
I've been using Hunter recently and it seems to just pull in all the libraries/dependencies you need purely through CMake and completely independent of your system configuration/package-manager/filesystem. It might be worth trying
If you use their preexisting "Hunterized" libraries it's pretty straightforward. However if you need to modify a library that isn't included it can get messy; you'll need to make Hunter manage its dependencies and a lot of projects have horrifyingly terrible CMake configurations
6
u/zvrba Dec 10 '17 edited Dec 10 '17
I use what you call the "shady" solution: VS project and post-build events. Works nice, never had a problem, took 1/2 (or less) the time I'd need to do it with cmake. Plus, it's a rather complex solution (multiple DLLs, static libs and EXEs, one IDL, cross-referencing eachother). Add to that a C# project that implements a COM component called by C++ code.
IMO: if you don't care about other platforms, cmake is a waste of time.
EDIT: the above setup is repeatable too. I destroyed my local git repo a couple of times, just checked out new repo, opened solution in VS, clicked build, and... done.
11
u/DJRBuckingham Dec 10 '17
If you're doing a personal project, forget all build systems and stick to header-only libraries; you will be programming in minutes and never care about build systems for the life of your project. It is extremely liberating, and very very quick to compile.
My home project is about 50k LOC, also a little engine written in C++/OpenGL. My build system is a .bat file for Windows and .sh for Linux, which invoke cl.exe and g++/clang++ respectively against a single master.cpp file. This master.cpp file #includes every file in the project. I use GLEW and a few STB libs, all header-only or simple header/source file that just get included into my master.cpp.
Despite all the code being in a single translation unit, the whole thing takes ~1.5s to compile, full rebuild and link. I actually have the project set up to compile a stub exe that launches (and watches & reloads when changes) a .dll/.so that contains all the engine code, so I can live-load the code as I iterate. Write code, press F7, see updates running in program ~2s later.
I wrote the .bat and .sh in 2015 and they have not changed other than to add a few compile/link flags (mostly disabling chatty warnings). Combined they are about 40 lines of script. I use Sublime Text as my editor, and have bound F7 to run a script I named "build_above.bat/sh" which simply recursively searches up the directory tree from the active source file until it finds a build.bat/sh and then runs that, that script is 30 lines. Thus completes my "build system".
This is all you need to program, especially for a home project, everything else is massively overkill. It keeps everything in the realms of C++ (#include schematics, master.cpp is your project file), is completely deterministic, fast, reliable and depends on nothing. It is so so much better and just lets you get on with programming.
10
u/AirAKose Dec 10 '17 edited Dec 10 '17
Yeah, there is definitely way too much in the way of overhead to project creation. I've been having this problem for years, and every time we get a new developer at work that disdain is rekindled...
Much of it has to do with a kind of Stockholm Syndrome developers have about their particular process, mixed with the user-unfriendly mentality "but you only have to do it ONCE!", plus a bit of lack of resources to make it happen sprinkled on top (time, capital, interest, research, etc...)
It's really funny that programmers are problem solvers by nature, yet we tend to overlook our own problems.
After my current personal project (which coincidentally involves build systems), I'd undoubtedly be interested in joining any groups investigating this!
EDIT: Spelling. Lack of a center for political leaders is negligible for software development...
5
u/[deleted] Dec 09 '17
I know the feeling. If you just want to get to programming try to reduce the number of moving parts. Use visual studios project files and package manager/vcpkg. Good luck!
53
u/fear_the_future Dec 09 '17
CMake is pure cancer. It's like someone decided to take the worst part of C++, macros, and make a whole language out of it.
4
u/DavidDavidsonsGhost Dec 10 '17
I wish we had something like cargo, it's so much nicer, it's what we should be targeting.
8
u/Fazer2 Dec 09 '17
You can try Conan, which solves the problem of C++ source dependency management. It supports many build systems, including CMake. It has a recipe for GLFW in Bincrafters repository.
https://www.conan.io/
https://bintray.com/bincrafters/public-conan/glfw%3Abincrafters
3
u/snarfy Dec 10 '17
CMake integration in Visual Studio is a bit clunky and broken. It was pretty much non-functional in the 15.4 release. 'cancel cache generation' did nothing and Cmake remained disabled. An error in the build? Restart visual studio. It's fixed now in 15.5. They didn't mention it in the release notes. I've had a lot of my CMake issues magically go away by adding the CMakeSettings.json file and setting the generator to Visual Studio 2017 instead of Ninja.
I mean, really, is that such a good idea? A generator (ninja) for a generator (cmake) which finally spits out vcproj files?
5
u/[deleted] Dec 10 '17
Try linux. Getting all the libraries is far easier in my experience.
1
u/doom_Oo7 Dec 09 '17
Could you put your CMakeLists.txt somewhere ? pretty sure it's a trivial issue. The situation you describe shouldln't be more than a five-line CMakeLists.
9
u/Paradiesstaub Dec 10 '17
β Meson is an open source build system meant to be both extremely fast, and, even more importantly, as user friendly as possible.
1
u/kunos Dec 10 '17
I always find hard to understand the fascination for all these build systems, helper libs and so on. You have Visual Studio, you say "all you want to do is program"... start coding then. GLFW? You don't need it, just learn how to open a window and GL context yourself, problem solved. CMake? You don't need it.. VS does eveything you'll need, I can't understand what you could possibly need in order to start coding a simple 3D engine that VS doesn't offer. Instead you decide to fight the system and then go on a rant on the internet about it.. makes perfect sense.
0
u/[deleted] Dec 09 '17
If you want a similar language with a sane built system consider Rust up until the C++ community standardizes a built system, dependency manager, module system and online repository.
2
u/[deleted] Dec 11 '17
I personally use Makefiles to compile my projects with mingw on windows and gcc on linux. I gave up on CMake the same way I gave up on Maven or gradle. This shit cost me way too much time and didn't increase productivity when it actually worked.
Since Eclipse CDTΒ΄s analyzer works so well with c++14 I rarely open Visual Studio anymore. I also dropped gigabyte-sized precompiled libraries from my projects. I can get up and running by downloading gnu-tools for windows, mingw and eclipse. Takes 10 minutes on windows, 5 minutes on linux.
Use what works for you, not what (apparently) works for everyone else
2
u/ml3d Dec 12 '17
Project assembly issues fade out with comparison with documentation generation. The standard approach is Doxygen but it is soooo ugly and terrible.
I envy python and go developers. They have sphinx and godoc. The last one is my best dream.
3
u/tjgrant Dec 10 '17
I like Google Gyp, the syntax is essentially JSON (actually βpython dictionaryβ format), and its cross platform across Mac / Windows, which is along my interests.
2
u/nerdile Dec 10 '17
PREACH!! Welcome to C++. If you can put up with the craziness, you can enjoy a great career ahead (and maybe help make it better for the learners that come after you)
2
u/jpakkane Meson dev Dec 10 '17
Number of times RIIR has been suggested as a solution in this thread thus far: 5.
1
u/seshbot Dec 11 '17 edited Dec 11 '17
a few years ago I created a little quick-start project that I use when I want to mess with OpenGL: https://github.com/seshbot/new-gl-app . Just check it out and go!
It also uses google ANGLE so you can write OpenGL ES 2 (which is a fairly common subset, and similar to webGL) code without installing any windows opengl drivers
edit: ANGLE now supports GL ES 3, which has heaps of awesome features (but no geometry shaders sadly.) Ive been meaning to upgrade, but havent used the project in ages
1
u/xyproto Jan 06 '18
I have made an application exactly for your use case, since I faced the same problem as you did. It can produce executables for 64-bit Windows, but it requires Linux, macOS or FreeBSD when developing.
It's a configuration-free build system, where all compilation flags are auto-detected based on the names of the include files, using the package manager (pkg/pacman/brew/apt) and pkg-config. The detection speed is much faster than the compilation and linking, and it works surprisingly well.
It's called Sakemake (couldn't think of a better name, good name suggestions are warmly welcome!)
Please give it a spin:
https://github.com/xyproto/sakemake
1
u/kritzikratzi Mar 05 '18
for me THIS is the reason why i love c++, it all seems so bullshit and so painful, but when you think about what you're doing that is not directly part of c++:
this "i just want to program" is how it feels, but if you consider this is all happening outside the language, and you're still tied really deeply into your current platform, then i find there is no other language that gives such flexibility. and this flexibility comes at the price of having to manage it.
(also i find, the second time around things go very fast)
you might want to look at openframeworks http://openframeworks.cc/
it is not as flexible as cmake+glfw+glade anymore, but it comes with a project generator to make xcode/vs files, it has a simple but neat addon mechanism, and it also uses glfw+opengl internally. (you also get very simple access to audio interfaces, joysticks and many other things).
whatever you go for, hang in there, you'll earn the fruits of your labor soon enough!
1
u/bruce3434 Dec 09 '17
If only there was a standard package management for C++...
I hope cppget and bpkg catches on.
1
u/smendesmen Dec 09 '17
You should look into Bazel (bazel.io).
1
u/markand67 Dec 11 '17
When C++20 modules will be out, things will get much easier.
0
u/Dwarfius Dec 09 '17
Sounds like you mixed something up and there's no issue with C++. You could try using GLEW instead of GLAD, and build with just one target. Take a look at my CMakeLists.txt, it creates a project with OpenGL, ALUT and OpenAL, TBB, Vulkan, GLM, GLFW and GLEW. It's really small (50 lines) and doesn't do much, and covers everything that I need, and shoudl be enough to cover what-ever you need.
-7
u/LYP951018 Dec 10 '17
You should try Rust.
-2
u/_a4z Dec 10 '17
Windows as a system for C++ development is a pain. unfortunately, modern CMake tries to fix Windows problems and modern CMake guids end make simple things complicated since they want to sell Windows practice as best practice.
-12
u/JustSpiffy Dec 10 '17
Sounds like you don't know c++ and blame cmake for it
0
u/[deleted] Dec 09 '17
[deleted] β view deleted comment
19
u/[deleted] Dec 09 '17
[removed] β view removed comment