r/cpp • • May 29 '20

I made a project template for modern C++ projects, using CMake, GoogleTest, Clang-Format with CI, templates for issues, PR and more!

https://github.com/filipdutescu/modern-cpp-template
148 Upvotes

77 comments sorted by

13

u/lanzaio May 30 '20

Why did you pick such a non-standard clang-format configuration for a general purpose project template? Just leave it as default.

3

u/[deleted] May 30 '20

I understand why one might not want to use that configuration, but it was one that I found suited my style. I am quite new to using clang-format, so I didn't really know it provided default one, but rather you needed to write one from scratch.

Out of curuiosity, is it a problem though? I mean you could easily just replace it with your own configuration. Or is there another reason for not using such a specific one?

2

u/lanzaio May 30 '20

It's perfectly reasonable to have your own clang-format preferences. But you propose this as a default template for other projects. So your personal opinion shouldn't really be a part of it. clang-format defaults to the llvm project's format and is perfectly reasonable for most projects.

1

u/[deleted] May 31 '20

I see your point. In this case, I might just change it to that, or Google's, which I fancy more.

9

u/andersfylling May 29 '20

Any reason to use this project compared to https://github.com/TheLartians/ModernCppStarter ?

4

u/[deleted] May 30 '20 edited Jun 03 '20

I would say the following reasons differentiate it:

  • More friendly and clearly organized CMake files (at least for me)

  • Ready to use templates for both PRs and issues

  • Solid templates for: README, Code of Conduct, Contributin Guidelines

  • Integrated compiler warnings, that Jason Turner also uses in his template

  • Properly configured installed targets, which allow for easier down-the-line integration, with the usage of CMake's find_package(...)

For me, those were the motivations behind creating this project, I tried to integrate all of the important pieces that an free open-source modern C++ project would need.

I do hope it provides some valid reasons for you to maybe use my template in some of you projects!

Edit: formatting

1

u/jonesmz Jun 03 '20

On old.reddit.com your bulleted list does not format properly.

1

u/[deleted] Jun 03 '20

I have no clue as to why. I myself do not use old reddit, so I do not know the markdown rules it has in place. If you know, do you mind telling me?

2

u/jonesmz Jun 03 '20

A large number of people on /r/cpp use old.reddit.com. Personally I very very very much do not enjoy the site redesign, and lots of people have a similar opinion.

This is how I've formatted bulleted lists in the past.

  • list 1

  • list 2

Where I've done

- list 1

- list 2

With only a single new-line between bullets.

1

u/[deleted] Jun 03 '20

Cheers, I used * instead, probably that is why it fails to format correctly.

3

u/[deleted] May 30 '20

file(GLOB_RECURSE headers CONFIGURE_DEPENDS "${CMAKE_CURRENT_SOURCE_DIR}/include/.h") file(GLOB_RECURSE sources CONFIGURE_DEPENDS "${CMAKE_CURRENT_SOURCE_DIR}/src/.cpp")

Isn't file glob is not recommended ?

All source and header files are included in the main cmake file. It won't work well if the repository has more than a few hundred files.

2

u/kalmoc May 30 '20 edited May 30 '20

Why should it not work well with more than a few hunderd files? That aside: Once your project grows to that size, there are probably a lot of things you want to customize anyway.

-1

u/[deleted] May 30 '20

I thought it's a template project which at least covers this part.

2

u/[deleted] May 30 '20

Well it is a template project and you are coverd for at least those hundred or more files. If you have a much larger project, you should actually edit the template to better suit your needs. You acutally probably need to do that, even with other templates.

1

u/kalmoc May 30 '20 edited May 30 '20

[Edit:What part and how is it covered?] Sorry, I haven't had my coffee yet. Can you elaborate?

1

u/[deleted] May 30 '20

No

1

u/[deleted] May 30 '20

It is not recommended, true, but I could not find another way to add all of the files "dynamically", rather than manually listing each. Do you know of any alternatives?

2

u/[deleted] May 30 '20

Usually adding everything in the cmake has advantages. It it show up in cmake version control difference. Since cmake file changed, cmake will be invoked when someone pulls the code.

As far as I know, adding "dynamically" won't have these things. It will cause problem. So, I never investigated this.

2

u/[deleted] May 30 '20

Those are some good arguments. I will look into this and choose the best alternative. Cheers for the feedback!

7

u/qizxo May 30 '20

Not bad. I'd recommend adding support for conan and/or vcpkg with options for pulling in boost and/or Qt.

1

u/[deleted] May 30 '20

I did look a lot into package managers, but I found them lacking some important libraries for me or them existing but not being upgraded since they were added to their repos (i.e. OpenCV and Armadillo).

I think I will do this though, as well as provided multiple options for unit testing, not just Gtest.

Thanks a lot for the advice!

3

u/Nelieru May 30 '20

Take a look at vcpkg. You can directly compile from github's latest HEAD (or other), and making your own packages from opensource libraries is extremely easy if they don't exist.

1

u/[deleted] May 30 '20

I will get to including Conan and VCPKG, maybe others as well, once I finish adding static analyzers, doxygen, CI caching and do a bit of refactoring. Thanks for the input!

2

u/qizxo May 30 '20

Yeah, the reason I suggested boost and Qt is because they are probably the two most commonly used.

2

u/[deleted] May 29 '20

Thanks for this. Seems like a great idea!

2

u/[deleted] May 29 '20

Glad you enjoy it and I hope it, or future versions of it, wil be of use to you and others!

5

u/ed_209_ May 29 '20

It still seems hard for cmake to have a "best practice" pattern if you want windows / linux given that external linkage defaults are the opposite.

Something the Unreal Engine build system does is enable to build everything with dynamic linkage when in development and then build everything statically linked when you want to build for retail. This seems like the best "default" for general C++ development.

For working on something like llvm/clang on windows being able to build all the libs as dlls would be incredibly helpful but it doesnt seem possible at all? cmake: BUILD_SHARED_LIBS

https://cmake.org/cmake/help/latest/variable/BUILD_SHARED_LIBS.html

never seems to work on windows and I presume requires manually exporting all the library symbols which is too much for cmake?

4

u/[deleted] May 29 '20

While making this template, I did actually stumble upon conflicting practices in regards to setting up CMake. I ended up taking all of the common things shared between CppCon talks, articles and other such templates on GH. I am unsure as to whether "my way" is the proper one and tbf I did not actually think too much of the lib vs dll problem, since I put this responsability on the dependencies, rather than on the template.

I will look into this though, maybe try to come up with a solution to it, but at least with GoogleTest I did not encounter any such issues, using the CI workflows presented.

If you have any more tips or advice, I would be glad and thankful to listen to them! And cheers for looking at my project and leaving feedback, I am very grateful for it!

8

u/mrexodia cmkr.build May 30 '20

I’m pretty sure that GLOB_RECURSE is a confirmed bad practice though.

3

u/ericonr May 30 '20

I think GLOB_RECURSE misses new files if you added them after creating the Makefile/Ninja file. At least that's what I saw as the reason for not using it.

4

u/flmng0 May 30 '20

The CONFIGURE_DEPENDS option was added to CMake's file(), to mitigate this problem; unfortunately it's listed as not working for some pretty major build systems though.

1

u/[deleted] May 30 '20

Did not know about this feature. I shall look into it and were it to prove reliable in most cases, I might use it. Should it not work for too many build systems, than I think I will look for something else

1

u/kalmoc May 30 '20

Which are?

3

u/flmng0 May 30 '20

https://cmake.org/cmake/help/latest/command/file.html#filesystem describes the issue. I've heard it doesn't work reliably for Visual Studio 14. I can't find a source for that, so I guess I'm either misinformed or it's been fixed.

1

u/[deleted] May 30 '20

Yeah, I know it is not recommended, but I couldn't find a different way to automatically get all files in the directories, so I decided to keep it for now at least.

1

u/twirky May 30 '20

Have some script to generate some, let's say Sources.cmake file and include that file from CMakeLists.txt (or whatever). GLOB_RECURSE gets quickly out of hand on larger projects. Generating sources list file has several advantages:

- You can implement any logic. You may have a lot of files like .mm .m .rc .qrc .rc .xib, etc. depending on the target.

- You can use a scripting language which makes sense rather than cmake's.

- You actually see what files end up in the project.

- If the source file structure is changed the cmake on other developer's machines will sense the changes and rerun cmake. It's very annoying when someone changes the file structure and after pull you start getting errors. It's also very annoying having to rerun cmake after every pull.

- You can use the very same script to generate for things like xcodegen as cmake is terrible with generating Xcode project files.

1

u/[deleted] May 30 '20

This is great advice, cheers! Will try to adapt my template.

5

u/electricCoder cmake | vtk May 29 '20

seems hard for cmake to have a "best practice" pattern if you want windows / linux given that external linkage defaults are the opposite.

Not at all

BUILD_SHARED_LIBS never seems to work on windows

If your project doesn't want to selectively export symbols ( which IMO it should be doing on non windows platforms ), you can get CMake to automatically export all symbols by setting:

https://cmake.org/cmake/help/latest/prop_tgt/WINDOWS_EXPORT_ALL_SYMBOLS.html

2

u/[deleted] May 29 '20

Not at all

Could you maybe elaborate on this a bit please? I did find some best practices, but nothing standardized.

Thanks for the clarification of the BUILD_SHARED_LIBS.

9

u/electricCoder cmake | vtk May 29 '20

If you want always export symbols use:

set(CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS True)

If you want to have always hidden and explicit symbol exporting use:

set(CMAKE_CXX_VISIBILITY_PRESET "hidden")

CMake provides the GenerateExportHeader module ( https://cmake.org/cmake/help/latest/module/GenerateExportHeader.html ) to help automate the generation of the correct export macros on all platforms.

1

u/[deleted] May 29 '20

Thank a lot for the clarification! Really insightful, since I did not encounter those issues thus far and did not realize CMake had those functionalities built in.

Would you also mind letting me know of how frequent, in your experience, those issues arise?

2

u/MarkOates May 30 '20

Not sure if I'd ever use the template, but I'm a huge fan of Google Test, so shout out to anybody who hasnt tried it out yet to give it a shot.

1

u/[deleted] May 30 '20

Well I do not plan to force my template to become standard, I just wanted something to help me out with my project, so no harm done in not using it. Maybe when I add more features you'll wanna check it out again and maybe even use it.

Thanks for taking a look anyways and I too am a fan of Gtest!

1

u/kalmoc May 30 '20
  • Why do you force static compilation of your library?

  • If you want to use unprefixed option names at all, wouldn't it make sense to use standard ones like BUILD_TESTS

  • I wouldn't turn on warnings as errors by default. Nothing more annoying than wanting to test out a 3rd party project, which is not compiling just because you have a newer compiler than the author. And then you have to find out what the project specific way to turn of that flag is.

  • Personal preference: For libraries I prefer to have warnings and similar flags in toolchain files.

1

u/[deleted] May 30 '20
  • Why do you force static compilation of your library?

That is only for demonstration purposes, intended to be changed by the one using the template. I might add an option to choose to build dll instead.

  • If you want to use unprefixed option names at all, wouldn't it make sense to use standard ones like BUILD_TESTS

I am quite new at CMake and do not yet know all the standard options, so that is why. I will try and check for any others like BUILD_TESTS and change accordingly, cheers for pointing it out!

  • I wouldn't turn on warnings as errors by default. Nothing more annoying than wanting to test out a 3rd party project, which is not compiling just because you have a newer compiler than the author. And then you have to find out what the project specific way to turn of that flag is.

This is a sensible thing to change, did not think of that when I added the warnings.

  • Personal preference: For libraries I prefer to have warnings and similar flags in toolchain files.

Could you elaborate a bit on this topic, please, as I am not familiar with toolchain files.

3

u/kalmoc May 30 '20

That is only for demonstration purposes, intended to be changed by the one using the template. I might add an option to choose to build dll instead.

You don't need to add an option. Just don't specify either shared or static. Cmake has the global "BUILD_SHARED_LIBS" option that will then change the default for all libs that aren't explicitly set one way or the other.

Could you elaborate a bit on this topic, please, as I am not familiar with toolchain files.

Not in a reddit comment sorry.

2

u/[deleted] May 30 '20 edited May 30 '20

Cheers for the CMake global option explanation! I am down to other options rather than reddit comments, if you are willing to explain.

Edit: I had a seizure typing the last sentence apparently, mb sorry

1

u/kalmoc May 30 '20

I am down to ither options rather than reddit comments?

Sorry, not quite able to parse that sentence.

1

u/[deleted] May 30 '20

Jeesus, I am sorry, I apparently was not able to compile my thoughts, fixed it:

I am down to other options rather than reddit comments, if you are willing to explain.

2

u/kalmoc May 30 '20

Ah, now. I have to admit that this comment was just my easy way out. Even with more space, I don't think I'd be able to explain what toolcahin files are and especially how to best use them because I myself am not an expert in that. I guess the internet can help you though.

Essentially the diea behind cusom toolchain files is that you have a file where you can put all the compiler flags and other settings that are specific to the toolchains (compiler + standard library) and configuration you want to use rather than a particular project (e.g. specify a certain compiler, change default optimization flags, have a separate tolchain that is preconfigured to use all the sanitizers etc.). Then you'd call cmake via cmake -DCMAKE_TOOLCHAIN_PATH=<path-to-toolchain-file>.

I think one of the main reasons to have toolchain files is for cross-compiling, but you also can use it to simply use a non-standard compiler or to customize your default build settings.

1

u/[deleted] May 30 '20

That's really interesting. Will check out more about those, surely. Also no worries, we all got our own stuff to do, didnt mean no pressure. But I thank you for your time and explanation, really awesome to learn about those files! Cheers!

1

u/ycshao May 29 '20

I suggest to make it as a Yoeman generator.

1

u/[deleted] May 30 '20

Haven't heard of it before, but will look into it.

-4

u/Scellow May 30 '20

this is just horrible...

people have nothing to do, so they decided they should bloat stuff even more

cmake is a mistake

2

u/[deleted] May 30 '20

Do you mean that my template is horrible or that CMake is horrible?

I used to say the same with CMake until I actually got into it and I think its prerty neat

-3

u/Scellow May 30 '20

cmake, it makes everything horrible

1

u/[deleted] May 30 '20

I can agree, premake is a bit more user firendly but less used

0

u/[deleted] May 30 '20

[deleted]

4

u/[deleted] May 30 '20

Those all seem like great suggestions! I did think of static analysis and doxygen, but I did not quite understand what they did and how, so I decided to leave them for the moment. But I think I shall focus on those things now.

Could you maybe give me an exampke of auto code format in this context? I tried to find a way to do it, but could not find anyrhing conclusive.

Did not look to much at Gtest' Mocks, but it seems to be of interest to people, so if I figure it out and seema like a good fit, why not.

Thank you very much for the suggestions, they are greatly appreciated!

2

u/ruxven May 30 '20

GoogleTest has a helpful script for generating mock code from header files: gmock_gen.py

Works best with pure virtual interfaces, though if the class has any virtual methods it may be useful to have a mock generated for it anyway.

2

u/[deleted] May 30 '20

Cheers for the explanation! I assume this can be automated on build, resulting in the automatic mock generator u/futureroboticist mentioned, right?

2

u/ruxven May 30 '20

That's right! I'm not an expert on cmake, but if it or a helper can detect virtual keywords I don't see any reason why it couldn't generate a MockClassName.h with a boilerplate header under test/mock/.

Well, OK, I can: there are some function signatures that throw off gmock_gen's ast builder and generate code that doesn't inherit the virtual method. They are not difficult to fix manually, but it does break the workflow goal of not having to manage them directly.

2

u/[deleted] May 30 '20

I guess I'll look into is some more and decide whether or not to include Mock by default or as an option, thanks!

2

u/Nelieru May 30 '20

For auto code format, CMake doesn't have built in clang-support but you can just run it as an external program with find_program or something. If you're working with git, it might be interesting to run formatting as a pre-commit hook however.

1

u/[deleted] May 30 '20

Which would you say I should fancy more, the git hooks or running clang-format from Cmake? I have little experience in this area. Cheers!

3

u/Nelieru May 30 '20

I'd say using git hooks is better because doing it during build could cause problems. Imagine if someone commits code with wrong formatting, everyone else is going to format that code which will cause plenty of changes. Just force formatting before any commit, or don't force it at all.

2

u/[deleted] May 30 '20

So it is not a bad idea not to auto format code either? More of a convenience thing?

2

u/Nelieru May 30 '20

I'm no expert, but I guess it depends. You also have to remember that .clang-format files have no fixed format and using version 10 might differ from version 6 formatting. I would add a .clang-format file in the repository, recommend using it but not force anything.

1

u/[deleted] May 30 '20

Well it is not mandatory to use my .clang-format, I just like that config, so I rolled with it. But anyone is free to swap it out with any other.

4

u/ngrodzitski May 30 '20

I totally agree with your concerns about changing code as a part of a build. But seems it can be a sort of cmake-fied using separate target: cmake --build . --target clang-format. Though pre-commit hooks are much better option.

0

u/[deleted] May 31 '20

[deleted]

2

u/[deleted] May 31 '20

Interestsing, cheers for letting me know!

0

u/[deleted] May 30 '20

[deleted]

3

u/[deleted] May 30 '20

Yep, I did actually take inspiration from his and TheLartians' template, when researching this topic!

2

u/andersfylling May 30 '20

Why make a new one? I'm starting to realise a lot of people are just making new ones now. But why not a PR to one you liked?

1

u/[deleted] May 30 '20

Allow me to quote myself an answer provided further in the comments, for the reasons behind my choice. Another important one is that it helped me learn how all of those things work together and fix the best practices found. Here is the quote:

I would say the following reasons differentiate it:

  • More friendly and clearly organized CMake files (at least for me)

  • Ready to use templates for both PRs and issues

  • Solid templates for: README, Code of Conduct, Contributin Guidelines

  • Integrated compiler warnings, that Jason Turner also uses in his template

  • Properly configured installed targets, which allow for easier down-the-line integration, with the usage of CMake's find_package(...) For me, those were the motivations behind creating this project, I tried to integrate all of the important pieces that an free open-source modern C++ project would need.

0

u/[deleted] May 30 '20

[deleted]

1

u/[deleted] May 30 '20

I ran into issues when compiling Release only on my machine and I added it as such in the CI workflow. I haven't had much time to change it, due to exams, but I plan on remaking those to benefit from caching as well, so I'll ensure no unnecessary builds are done.

0

u/[deleted] May 30 '20

[deleted]

2

u/[deleted] May 30 '20

Well I can always remove the C++ from it and have a really good starter for any FOSS project, since I have good README, CoC and Contributing templates, as well as for PR and issues.

0

u/[deleted] May 30 '20

[deleted]

1

u/[deleted] May 30 '20

I guess that is why people make templates. Honestly, after doing all of this I am so much more comfortable with each step of the process I do not mind it.

0

u/[deleted] May 30 '20

[deleted]

1

u/[deleted] May 30 '20

Could you maybe explain why not use the templates you made? As it seems like you started from scratch for each customer/project. That or I misread and if so I am sorry.