I I wrote a program. Can I use this program as an include?

Go To Last Post
9 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hey guys!

I have a question,

I have written a program, I can piece this program as an include file? Or is this not possible?

I like to hear it,

Greetings

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

After 87 posts you should know that "AVR Tutorials" is not the right place to ask this question. If you are talking about programming AVRs this belongs in "AVR Forum" and if you are talking about general C it belongs in either "Off Topic" or "General Electronics".

I'm going to assume you mean AVRs so I'll move this to AVR Forum.

Moderator

(rather ironically, while you were in Tutorial Forum it's a shame you missed the Managing Large Projects article which would have answered all your questions - the rule for the .c/.h split is only put data/code generating lines in .c files, leave .h for interface declarations (and function definitions that only generate code at the point of invocation - that is static inline)).

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Yes, that's possible.

For the ATtiny13 I used global optimization.
For quick-and-dirty tests, I include my other *.c files just above the main function. So I can still use all the defines and variables in the main *.c file. The compiler and linker need only to compile and link my main *.c file.

It is ofcourse better to add the *.c files to the compiler and linker and create *.h files for the functions.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:

It is ofcourse better to add the *.c files to the compiler and linker and create *.h files for the functions.

Indeed - it's very easy to cut/paste the function prototypes to make the .h so I'd always do it rather than getting into bad habits.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Yes, it s possible to do what you intend to.
I would suggest this tutorial from Dean (abcminiuser) for a very good explanation to achive your goal

http://deans-avr-tutorials.googlecode.com/svn/trunk/ProgrammingMethods/Output/ProgrammingMethods.pdf

Thanks,
Karmu

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I was under the impression that this was a very bad practice.

The function of the header is to put declarations and definitions that you want to expose to the public.

Also, IIRC you'll get linker errors if you define data in a header.

Smiley

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:

I was under the impression that this was a very bad practice.


Define "this".

There's a simple rule - don't put anything which would lead to the allocation of even a byte of program or data in a .h file.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 1

NB this post looks a lot like spam - it had three URLs embedded in it that were apparently trying to promote a commercial site. They have been removed. Cliff, Moderator

 

Yes you can create your own header file and include it in your program using #include pre processor directives. 

Here are the steps to create your own header file

 

  1. Open a text editor and type a function definition, like we define a new function in C program.
    int getSquare(int num){
       return num*num;
    }
    
  2. Save this file with .h extension. Lets assume we saved this file as myMath.h.
  3. Copy myMath.h header file to the same directory where other inbuilt header files are stored.
  4. Compile this file.
  5. To Include your new header file in a c program used #include preprocessor directive.
    #include "myMath.h"
    
  6. Now you can directly call any function define inside myMath.h header file.

 

C programming language (link removed)

Last Edited: Thu. Sep 22, 2016 - 12:44 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

akbarkhan wrote:
Open a text editor and type a function definition
Utter nonsense. If you are putting definitions not declarations in a .h then the function should be "static inline". It is very bad practice to actually instantiate either data or code from a header because if the header is included in more than one source it will be instantiated multiple times.

 

Given that your post appears to have been a veiled attempt to post links to a training course site I would not put much store in what you are offering if you think it is wise to advise beginners to instantiate code in a .h files!