Author Topic: [C] do i need a *.h file for every *.c file?  (Read 2797 times)

0 Members and 1 Guest are viewing this topic.

Offline rstofer

  • Super Contributor
  • ***
  • Posts: 6534
  • Country: us
Re: [C] do i need a *.h file for every *.c file?
« Reply #50 on: January 23, 2019, 01:30:14 am »
I have attached a makefile.  [...]

Cool! It's always interesting to see how others do things.

If you add "-MD" to your CFLAGS, GCC will generate the dependency info for you, producing a .d file for each object file (.o) it produces. The .d files are actually GNU Make include files, which you can bring in easily with:

Code: [Select]
-include $(OBJS:%.o=%.d)

Thanks for the tip!  I'll try it next time I create a makefile - if I don't lose the text I just copied from the reply.

My target 'make program' used the lpc21isp utility to program the flash.  All the parameters are on the command line.  This target depends on the .hex file which, in turn, causes all necessary compilations and linking.

Then create a keyboard alias like 'alias p=make program' and I have a one keystroke solution to build and program the flash.

When it comes to writing code, Linux is a very productive environment.
« Last Edit: January 23, 2019, 02:17:13 am by rstofer »
 

Offline Doctorandus_P

  • Frequent Contributor
  • **
  • Posts: 579
  • Country: nl
Re: [C] do i need a *.h file for every *.c file?
« Reply #51 on: January 26, 2019, 11:09:33 am »
It's all up to you.
Do you want to hack something quick together, or do you want to put in some effort to be able to read your own code 5 years into the future?
Do you want to re-use some "library" files such as for example the functions to use an encoder or serial port for other projects?

I find it much easier to give each .c file it's own .h file.
It avoids the fud of what is where.

If you use a decent IDE it is trivial anyway.
It just generates a .c and .h file from a template, which already has the include guards in the header file.

While browsing your project it also does not matter if you add 20 header files to the 20 source code files. If you follow convention and common sense you put the source in the source file and the miscellaneous info in the header files.
If you want to obfuscate, you put some the source in .h files, other source in .bin files, put the stuff that normally goes into a header file on lines between functions in a source code file where it does not belong and write a makefile to stich it all together.

Or you use the arduino way. Write a script to dump everything into a single file before you pass it to the compiler.
 

Online GeorgeOfTheJungle

  • Super Contributor
  • ***
  • Posts: 1948
  • Country: pl
Re: [C] do i need a *.h file for every *.c file?
« Reply #52 on: January 26, 2019, 02:28:44 pm »
Or you use the arduino way. Write a script to dump everything into a single file before you pass it to the compiler.

That's the first thing gcc does, when it (automatically) calls cpp.

https://gcc.gnu.org/onlinedocs/gcc-2.95.3/cpp_1.html
« Last Edit: January 26, 2019, 03:42:08 pm by GeorgeOfTheJungle »
Even when the experts all agree, they may well be mistaken.
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf