Solved

Programming Question Creating More .c Macros

Posted on 2004-10-14
7
218 Views
Last Modified: 2010-04-17
Hi, I need to add another Macro, .c file to my program, but I can't get it right.

I got

Main.c where I call most of my functions.
this file includes several name.h files.

I need to call some fucntions from main.c that are included in another macro or file
called Driver.c This Driver.c file will share one .h file with Main.c

How can I do this.
I thought that
#ifdef Driver
#define Driver
#include Driver.c
#endif
Would do it, but I think I have wrong written
Main.c is getting to long, that is the reason why I need to this.
Thanks!




0
Comment
Question by:caleno
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
  • 2
  • +1
7 Comments
 
LVL 1

Expert Comment

by:jaguarul
ID: 12312074
First, let me try to see if I understood correctly: you have a large "main.c" file with many functions. Now you decided to split it in 2, and created another file, Driver.c, where you moved some of your functions. Now you created a header file for Driver.c, and you include it in main.c. I fail to see the reason why you include Driver.c. Here is how I would do it (I am afraid I need your feedback):

have Driver.h in which I declare all the functions I need to call from driver.c. In main.c I will #include "Driver.h". Then, I will compile both files together (something like gcc main.c Driver.c).

The pattern:

#ifdef Driver
#define Driver
#include Driver.c
#endif

is possibly wrong. I think you should use in the first line "#ifndef Driver" (notice the extra "n" in ifndef). Try this first, and then switch to my "here is how I would do it" part. :)

cheers
0
 

Author Comment

by:caleno
ID: 12312473
jaguarul thanks for your response. this is what I did

I did include Driver.h in Main.c
In the Driver.c file I place

#ifdef Driver
#define Driver
#include Driver.c
#endif
#include "Driver.h"
====================== it did not work
Then I placed
#ifndef Driver
#define Driver
#include Driver.c
#endif
#include "Driver.h"
======================did not work either



0
 
LVL 1

Expert Comment

by:jaguarul
ID: 12312530
I think you can skip the #ifdef parts, you don't need that. What it means "it did not work"? What are the compilation errors?

I assume you have something like this:

*** Main.c ***

#include "Driver.h"

...
main() {
  call_driver();
..
}

*** Driver.h ***

...
int call_driver();
..

*** Driver.c ***

...
int call_driver() {
  printf("called");
}
...

0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Expert Comment

by:MattBeard
ID: 12316774
You should never (except in some very special cases) #include a .c file - only .h files.

You need to tell you compiler to compile main.c and driver.c then link the two results.

If you were using Visual Studio you would simply add driver.c as a new source file.  If you are using another compiler you will probably need to write a makefile.

What compiler are you using?
0
 

Author Comment

by:caleno
ID: 12318703
MattBeard, I am using microvision3 from keil software, for micro controllers.
I totally agree with this:
"You should never (except in some very special cases) #include a .c file - only .h files."

I have already two .c files in my program and did not have to anything special for that. One for main.c, one more .c and then I need a 3rd .c.


I was doing some reading last night and I think I need to inlcude all the code before I include the
#endif statement
Like this:

#ifdef Driver
#define Driver
 
#include "Driver.h"

Code.....
code....
code...

#endif

I will try this method.

Thanks


0
 

Expert Comment

by:MattBeard
ID: 12319402
caleno: I am assuming that the code in your last comment is intended to live in driver.c

If this is true there are a few comments I have.

Firstly there is no need to do the #ifdef ... #endif especially as Driver can't be defined if you are about to #define it (following line!)

I assume that the reason that you are wanting to define macro "Driver" is so that Driver.h can behave differently when it is included in Driver.c than it behaves in the other .c files (this is often done to allow an include file to either declare a variable as extern or to actually define it. Example:

>>Driver.h:
#ifdef Driver
   const int InitString[] = { 42, 12, 16, 3, 4, 1, 4 };
#else
   extern int InitString[];
#endif

>> Main.c
#include "Driver.h"      // Simply declares InitString as an extern

Send(InitString);

>>Driver.c:

#define Driver
#include "Driver.h"      // Defines InitString


Does this make sense for your application?
 
0
 

Accepted Solution

by:
Chiffa earned 50 total points
ID: 12323370
I suggest you to  do three things to change your project from one-file to multi-file:

1. Remove strings:
#ifdef Driver
#define Driver
#endif Driver
and leave only
#include "Driver.h" untouched in your main.c file

2. In file Driver.h you should place declarations of all functions you place into Driver.c It should looks like that:
(in Driver.c)
int SuperFunc( int a, int b )
{
 ... some code ..
}

(in Driver.h)

extern int SuperFunc( int a, int b);

You see - there are no body of function in Driver.h file, only name and parameters. And ";" after closing brace is mandatory!

One more thing about directives "#ifdef", "#ifndef" and so on. Usually they are written in header (*.h) files, for example in Driver.h you can write:
#ifndef _DRIVER_H_
#define _DRIVER_H_
... here should be all content of Driver.h
... and last string must be
#endif

Such syntax is used to avoid duplicate inclusion of header file into .c file

By the way, there should NOT be line
#include "Driver.h"
in Driver.c file.

3. Last thing you need is to make object files for all of your source .c files and link them all together. I don't know commandline parameters for your compiler (and they usually different for different compilers), but you should find it in help or manual of your compiler. When you make program from more than one source file, you have to do 2-phase compilation. In the first phase you'll produce object files from your source files (usually they have extansion .o or .obj) and in the second stage you link all object files into one program. So either your compiler must have two different commandline parameters for these phases or there should be compiler program to make object files and separate program - linker - to make executable code from object files.
To simplify this task, especially in case of large projects with many source files, programmers usually write so-called makefiles, and use "make" programm to process them.

By the way if you use multi-file projects you'll receive additional advantage - when you change only one of source files, you don't need to recompile full project - just make new object file of changed source and re-link all object files. In large projects it is very sizable time economy.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article will show, step by step, how to integrate R code into a R Sweave document
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.

734 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question