-2

I have a function void MOTOR(int left, int back , int right);

This function runs another function: void PWMe(int left, int back, int right);

MOTOR uses this and some global variables.

I want to make a header file that includes MOTOR, but not PWMe (note that PWMe is a function in mainproj.c).

My first question is, how can I make a header file that includes MOTOR, uses PWMe and global variables from mainproj.c?

Second, I want to change MOTOR.c to assembly and MOTOR.lib.

  • 5
    As opposed to an _amateur_ C header file? – Grant Thomas Mar 22 '13 at 14:30
  • I'm not sure what you're asking to do... You want to make a header file with an `inline` version of your `MOTOR()` function, or you want the prototype to be placed in a header file? What issues have you run into while making the headerfile? As far as changing `MOTOR.c` to assembly... do you want to output of the compiler? Do you want to rewrite your functions in assembly? If so what is the target arch? – Mike Mar 22 '13 at 14:37
  • i want my answer only.... – Mohammad Javad Rahmani Mar 22 '13 at 14:37
  • no, i have some problem to make header file, i make this for header: #ifndef MOTOR_H_INCLUDED #define MOTOR_H_INCLUDED #pragma used+ void MOTOR (int left,int back, int right); #pragma used- #pragma library motor.lib extern int older,left_old,right_old,back_old,pr_old,pl_old,pb_old,mright,mleft,mback,min; #endif // MOTOR_H_INCLUDED – Mohammad Javad Rahmani Mar 22 '13 at 14:38
  • i have inline version and it`s work, but i want make it as an header file... that`s all – Mohammad Javad Rahmani Mar 22 '13 at 14:40
  • OK, and then what is the problem with the one you've made? – Mike Mar 22 '13 at 14:41
  • 1
    I think I've deciphered roughly what is wanted here. Have provided an answer. Adapt to suit your needs. Hope it points you in the right direction. – paddy Mar 22 '13 at 14:49
  • @mike, i have student and i want they can only use it, not change it. – Mohammad Javad Rahmani Mar 22 '13 at 14:55
  • and i use CodeVisionAvr compailer.... it have some special header file. – Mohammad Javad Rahmani Mar 22 '13 at 14:58
  • 2
    If you're teaching programming, I recommend that you master the basics first. If you can't explain clearly what you are trying to do, then you ought not to be teaching. – paddy Mar 22 '13 at 14:58
  • @paddy i have problem only with header file only... because to now, i don`t need to make a header file... – Mohammad Javad Rahmani Mar 22 '13 at 15:06

1 Answers1

5

The whole point of headers is to expose declarations. If you want your implementation of MOTOR(int,int,int) to be seen by any source file other than the one in which it is defined, you put it in a header.

Now, if you have another function PWMe(int,int,int) declared elsewhere then the same thing applies. For the MOTOR function to be able to call it, you need to expose it in a header. I guess it's defined in mainproj.c?

motor.h

#ifndef MOTOR_H__
#define MOTOR_H__

void MOTOR( int left, int back, int right );

#endif

mainproj.h

#ifndef MAINPROJ_H__
#define MAINPROJ_H__

extern const char const *one_hit_wonder;

void PWMe( int left, int back, int right );

#endif

Note that in mainproj.h I've declared a variable as well as a function. Hope you get the idea. Here are the implementations:

mainproj.c

#include "mainproj.h"

const char const *one_hit_wonder = "Yazz";

void PWMe( int left, int back, int right )
{
    printf( "The only way is up, baby\n" );
}

motor.c

#include "motor.h"
#include "mainproj.h"

void MOTOR( int left, int back, int right )
{
    PWMe( left, back, right );
    printf( "For you and me now\n" );
    printf( "\n - %s\n", one_hit_wonder );
}

Now, calling MOTOR with any arguments should produce a catchy chorus from an '80s hit single.

paddy
  • 60,864
  • 6
  • 61
  • 103
  • 2
    I need to +1 this for the answer and then +1 it again for the 80s ref. :-) – Vicky Mar 22 '13 at 14:50
  • ok, i define and use PWMe in my mainproj.c file, thanks, but how i can make motor.c to motor.lib and assembly language.? – Mohammad Javad Rahmani Mar 22 '13 at 14:51
  • and a new thing is i use some global variables in MOTOR; where i need to declare and how i can use that in "mainproj.c"? – Mohammad Javad Rahmani Mar 22 '13 at 14:53
  • @MohammadJavadRahmani See how I declared a global from `mainproj.c` in `mainproj.h` and then used it in `motor.c`? Do that in reverse. As for the 'assembly' comment, do you mean 'compile'? You really should ask that as a separate question and make sure you mention what compiler you are using. – paddy Mar 22 '13 at 14:55
  • my mean is not @COMPILE!!! i want write my operation in MOTOR in assembly. but i have some problem, how i can convert my operation to assembly, like this in math.lib: #pragma asm_function+ signed char cmax(signed char a,signed char b) { #asm ld r26,y+ ld r30,y+ cp r26,r30 brlt __cmax0 mov r30,r26 __cmax0: ret #endasm } i use gcc -s command, but i have some problem with this... – Mohammad Javad Rahmani Mar 22 '13 at 15:02
  • Can you explain why this needs to be assembly? You just want to make a `.lib` and provide a header for it, right? You don't have to share your C file, and you don't need to write it in assembly. That's the whole point of libs. Maybe [this link](http://stackoverflow.com/questions/2734719/howto-compile-a-static-library-in-linux) will help you. And [this one](http://stackoverflow.com/questions/8073108/must-c-libraries-have-lib-extension). – paddy Mar 22 '13 at 15:11
  • ok, i explain, i don`t want share my c source code, and if i share my lib file, i share my code... ok? i don`t want my student know how it work, they need only know it`s work! :D thank you @paddy – Mohammad Javad Rahmani Mar 22 '13 at 15:15
  • 1
    No, see, you're wrong. If you share your lib file, you share compiled object code, NOT source code. If your students are capable of disassembling that and making sense of the algorithm, then it's either exceedingly simple or they should be the ones teaching you! ;-) You can compile multiple C sources into a lib, and provide one header. It doesn't even have to use the same header - you just need to provide the declarations you want them to use. – paddy Mar 22 '13 at 15:27
  • what`s your mean? how i can make a lib file? – Mohammad Javad Rahmani Mar 22 '13 at 15:32
  • at the next your answer doesn`t work, i have error in code blocks: E:\Temp\Code\Header\main.c|4|undefined reference to `MOTOR'| – Mohammad Javad Rahmani Mar 22 '13 at 15:34
  • You need to include the header file that declares MOTOR. If you can't work it out then please post a new question, show the relevant bits of code, and try to give as much information (like that error message) as you can. That way more people can help. I would like to help but it's 4:30am here and I am going to bed now. Good luck. – paddy Mar 22 '13 at 15:36