I know this is probably a primitive topic for most, but I just got into coding in c++ because a simple project I am working on that uses esp8266 which can be programmed using c++. Before this I only had experiemce with python, javascript and typescript.

Now to my problem: I am trying to split my code that is getting longer into multiple files.

I already think that I understand right that each library has a header (.h) file and source (.cpp or .c in case of c) file. The first thing I already have problem with is that as you are defining your functions and classes in the header file and then implementing them in the source file you are repeating yourself with the declarations which is not something I would like. I presume that most IDEs will probably automatically help you with generating or editing the header file automatically as you change code in the source file and I guess I will need to learn to live with it.

Then there’s the thing with importing. It may happen that if you create a library it also has some dependencies that it needs to include. But as far as I understand one library shouldn’t be included multiple times. So from what I can see most libraries check whether a global variable with an ARBITRARY name that the library chooses itself is not defined and then if that’s true it defines that variable to indicate it has been included (the name of the variable is not compketely arbitrary and usually follows LIBRARY_NAME_H but the convention cannot be really relyed on). When the library includes other library it also needs to check whether the variable of that library that is defined on its import is defined. The main file should also probably check this for every library it includes because it can’t know what lins were already imported by libs it imported? Am I getting something wrong or is it sometimes ok for some libraries to be included multiple times. There’s the #pragma once to handle these situations?

You are viewing a single thread.
View all comments View context
3 points

Plus, if you eventually do need or want to learn C++ for some reason, I honestly think that knowing Rust first would be quite helpful.

permalink
report
parent
reply

Programming

!programming@programming.dev

Create post

Welcome to the main community in programming.dev! Feel free to post anything relating to programming here!

Cross posting is strongly encouraged in the instance. If you feel your post or another person’s post makes sense in another community cross post into it.

Hope you enjoy the instance!

Rules

Rules

  • Follow the programming.dev instance rules
  • Keep content related to programming in some way
  • If you’re posting long videos try to add in some form of tldr for those who don’t want to watch videos

Wormhole

Follow the wormhole through a path of communities !webdev@programming.dev



Community stats

  • 3.1K

    Monthly active users

  • 1.8K

    Posts

  • 30K

    Comments