Hello! I noticed that i can write all code just in .h file, whole functions, variables, etc., without even using .cpp file and it still works. So i wonder what is the difference, can i use only .h files, will it not lead to errors? When i checked that, it worked even in normal programming for Windows (not just Arduino). So it made me curious.
(deleted)
i wonder what is the difference, can i use only .h files, will it not lead to errors?
.h files are typically included in multiple .c/cpp files. A .h file typically contains typedefs, constant and function declarations needed to access those functions from separate files.
Any code in such a file will result in multiple redefinition errors.
See my reply #3 to this thread for a description of the conventional use of .h and .cpp files, especially by engineers working in industry who write software for a living.
I put all my code in the .h file and have no problems. But I don't claim to be an expert or professional programmer.
...R
there are some unique ways to used .h files
But I don't claim to be an expert or professional programmer.
i wonder if a "professional programmer" is someone who has had to debug someone else's code
holdingpattern:
All it means is they grokked the interview.
not sure that's a proper use of grok
i actually had a supervisor say I just had to "grok" it, when discussing how some code worked.
If you define functions or global variables in a .h file you can only use that .h in one source file.
Let's say you had two I2C/TWI/Wire devices like an LCD backpack and a Real-Time Clock (RTC). Each has a driver library that uses the Wire library to talk to the device so each includes Wire.h in one or more of the driver library .cpp files.
Now if Wire::begin() was defined in Wire.h instead of Wire.cpp you would not be able to use both devices. The linker would find that the global name Wire::begin had two different values ("doubly defined") and the build would fail. By having the Wire::begin() function only in the Wire.cpp file the linker would find only one definition of Wire::begin() and everyone would be happy.
johnwasser:
If you define functions or global variables in a .h file you can only use that .h in one source file.
Perhaps it is by happy accident but that is all I have needed to do - in the .ino file
...R
of course you could define the functions as static and there would be no problem, just redundant code
gcjr:
of course you could define the functions as static and there would be no problem, just redundant code
... and redundant definitions of global variables.
Given that there are well-established conventions and best practices, I don't see a compelling reason to invent new and inferior ways of implementing modular, maintainable code.
Given that there are well-established conventions and best practices, I don't see a compelling reason to invent new and inferior ways of implementing modular, maintainable code.
i assume we've all had to straighten out or debug existing code written by someone else. Best practice (or even a consistent practice) are not necessarily followed by all developers or all organizations.
i used a .h file to capture the differences for ~6 radio boards with constants and a small amount of code probably written into flash. A unique executable was built for each board and run once on each board.
gfvalvo:
Given that there are well-established conventions and best practices, I don't see a compelling reason to invent new and inferior ways of implementing modular, maintainable code.
Some aspects of the well-established conventions and best practices are overkill for many (most?) Arduino projects that are developed and maintained by a single person.
And there are certainly aspects of the C/C++ programming language that are well established but are by no means "good".
...R
And there are certainly aspects of the C/C++ programming language that are well established but are by no means "good".
what aspects of C do you dislike?
Ken Thompson described C++: "It does a lot of things half well and it's just a garbage heap of ideas that are mutually exclusive."
In an interview Stroustrup said he felt it takes 10 years to learn to use C++ efficiently.
gcjr:
what aspects of C do you dislike?
c++ is a decent language, but the OOP was grafted on, long after c was established. As a result, it is not a very "clean" implementation. If you spend time with more modern c-like languages that were designed to be OOP from the start (Java and c# come to mind), you can see how much better they are, in many/most respects. If I had my way, I'd do everything in c#. In reality, I do almost everything in c/c++.
Regards,
Ray L.
along those lines, Ken Thompson and Rob Pike moved from Bell Labs to Google and developed the Go programming language
but i thought C was pretty straight forward and clean.
gcjr:
what aspects of C do you dislike?
One thing that I find particularly irritating is that it seems the authors put a premium on minimising the amount of characters that needed to be typed. As a result there is the stupid business that the * is used for multiply and for pointers. And together with the & it makes the simple concept of pointers very difficult for newcomers, or those (like me) who use them infrequently. There is no reason why they could not have used addressOf and valueAt or similar.
...R
Different times, different hardware, different priorities. I’d say the longevity of the language, was well as the BILLIONS of Lines of Code that have been written, speaks for itself. It’s still the language of choice for small, embedded processors, in real-world industrial applications, that need to twiddle bits at the hardware level.
Robin2:
As a result there is the stupid business that the * is used for multiply and for pointers. And together with the & it makes the simple concept of pointers very difficult for newcomers, or those (like me) who use them infrequently.
that's an interesting and understandable perspective. thanks
the ANSI K&R book (5.12) has a coding example that parses complex declaration and describes them in english (see pg 102).
after learning pascal in college, i recuperated from an accident learning to program microprocessors, accessing and configuring hardware registers.
when i read about pointers in C my draw dropped, realizing i could set a pointer to a memory mapped register address to access it using a higher level language. I think assembler programmers controlling hardware have an easier time appreciating C.
Robin2:
There is no reason why they could not have used addressOf and valueAt or similar.
Sounds like you are not remotely familiar with the "resources" provided by the the DEC PDP7 computer on which the c language, and the unix operating system itself, were first developed. The PDP7 Kernighan and Ritchie used had a whopping 8K 18-bit words of RAM - that is for all code and data. And it had only punched tape for I/O and storage. So, verbose keywords, like "addressOf" were pretty much out of the question.
Regards,
Ray L.