0

I have a subfolder1/Submain.c

{
  //initialize statements
    CallFunction1();
}

subfolder2/Submain.c

{
  //initialize statements
    CallFunction2();
}

and so on.

In the MainFolder there is a make file to include all the subfolders.Like subfolder1 subfolder2 etc. The mainfolder also has this mainfile.h which has all the function definitions (like CallFunction1 and CallFunction2). I know by practice it is not a good idea to have function definitions i n .h file. So I want to create a new .h file with all the function declarations (like an interface) and include this .h in the subfolders. But I don't know how to do this. Can someone please help. So the main thing is I dont know how to link the new .h and the .C

Here is the make file in (BIGDIR/tests/Mainfolder/Subfolder1)


include $(BIGDIR)/tests/make/Makefile.defs

TEST_NAME := test1

LCINCS += -I$(BIGDIR)/../tests/Mainfolder

C_FILES := SubMain.c

include $(BIGDIR)/tests/make/Makefile.rules

Here is the SubMain.c

#include "Main.h"
{

    // do things
    CallFunction1();  //this function is defined in Main.h which is in Mainfolder

}

I want to modify it in such a way that in subfolder1/submain.c I can have

#include "NewMain.h"

such that NewMain.h has all the function declarations and

main.c still has all the function definitions (so old Main.h ==> NewMain.c).

Current contents of Makefile in Mainfolder is:

include $(BIGDIR)/tests/make/Makefile.defs
SUBDIRS += subfolder1
SUBDIRS += subfolder2
include $(BIGDIR)/tests/make/Makefile.rules

PS: I have no access to change the rules. But I can play with Main Makefile and Makefile in subfolders.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
user1357576
  • 389
  • 2
  • 3
  • 17
  • It would be a lot easier if you showed us your (minimal) makefile. Otherwise we have to guess what it does before we can tell you how to change it. – Beta Aug 23 '12 at 21:49
  • 2
    In no way. If you include one C file in another, that's far from elegant. –  Aug 23 '12 at 22:26
  • You run `make` in `Subfolder1/` and it builds the executable `test1`, is that right? And is it all right if we make some educated guesses about what's in `Makefile.defs`? – Beta Aug 23 '12 at 22:33
  • @Beta have added the make file. – user1357576 Aug 23 '12 at 22:34
  • @Beta Makefile.defs has global makefile logic for all builds – user1357576 Aug 23 '12 at 22:39
  • @Beta I run make only in the main folder and it generates the executable – user1357576 Aug 23 '12 at 22:43
  • 1
    The fragment of code you show as `subfolder1/Submain1.c` isn't compilable. Consequently, it is hard to know what you actually have and what you expect. I'm pretty sure this isn't the way the code is written for C; I've seen enough C code over the last quarter century to be confident that this is a novel scheme you're devising. That doesn't automatically make it wrong, but it needs _very_ careful explanation. It sounds a bit as if you're trying to avoid writing the signature of each function twice — where you'd normally write it once in a header and once when the function's defined. Right? – Jonathan Leffler Aug 23 '12 at 22:49
  • 1
    The main makefile you posted (before it was removed) had no rules, and could not generate anything. *Do you want me to guess what you're really doing?* Or do you want to tell us? – Beta Aug 23 '12 at 22:49
  • @Beta you are right. I avoided writing the rules on purpose because I do not have access to changing the rules. So whatever I have to play with has to be within these two folders. – user1357576 Aug 23 '12 at 22:56
  • @JonathanLeffler So I don't right now have a file that declares and defines seperately (that is the goal though). So my main.h file has the definitions of all files (Which ideally should have been in a .c file). So I am trying to make it better by writing a header file which can have all the declarations so that I can stop using the current Main.h (Which has definitions )and convert it to a .C (Which I don't have to include in subfolders.) Instead write a new .h file which can be included in the subfolders -(this wil have only funcn declarations) – user1357576 Aug 23 '12 at 22:59
  • So you are free to modify the makefiles in the subfolders, but not the main makefile. You might have mentioned that earlier. And before I can suggest a solution, I still have to see (or guess at) the contents of the main makefile, which I've asked you three times to show us. **I bow out.** – Beta Aug 23 '12 at 23:02
  • @Beta you asked me about the `Makefile.defs`. I can of course modify `MainFolder`. There is a `MakeFile` in this `MainFolder` (what it basically does is to call the makes in the subfolders so that seperate executables are generated). So I can play with this . MakeFile and the Makefile in Subfolders. (which generate the individual executables). I hope this is more clear. Sorry about the confusion though. I had posted the contents of the MainFile Make but it was deleted for some reason :| – user1357576 Aug 23 '12 at 23:08
  • 2
    I'm sorry, but I don't understand what you've got or where you're trying to get to. Please read up on [Short, Self-Contained, Correct (Compilable) Examples](http://sscce.org/). Consider whether you can create an SSCCE from your existing system. It sounds a bit as if you need to take your `main.h` with its function definitions and make two copies of it. Copy 1 will become a header; you replace the function bodies enclosed in `{ ... }` with a semi-colon. Copy 2 will be made into a source file or several source files, each of which will include the header created from Copy 1. – Jonathan Leffler Aug 23 '12 at 23:19
  • @JonathanLeffler Yeah. Bang On! So just to clarify Copy1 will have ONLY function declarations. While the Copy2 will be definition file which is .C. So this copy1 is more like an interface (in java language). – user1357576 Aug 23 '12 at 23:21
  • 1
    In green-field coding (no legacy code to work with), you should usually create pairs of files: a source file and a header. The header contains the information that the users of the functions (you wouldn't be so gauche as to use global variables, would you?) that are defined in the source file need. The header should not include anything that only the implementation needs. The header defines the interface for users of the source. The source file includes its own header (it should be the first header), and anything it needs to implement the services it provides, and the function definitions. – Jonathan Leffler Aug 23 '12 at 23:26
  • @JonathanLeffler Thanks for that. So the issue here is the initial implementation was quite straight forward. But now the situation is that the main file needs to be common at one place. and it needs to have submains. So I have to work around this – user1357576 Aug 23 '12 at 23:43

1 Answers1

0

Each file (compilation unit) is compiled separately (into .o files) and later they are linked together. Header files (new.h) are not linked, they are for prototype checking.

Makefile:

sources=subfolder1/main.c subfolder2/main.c
objects=$(sources:.c=.o)

all:
   gcc $(objects) -o final_binary

%.o: %.c
   gcc -c $< -o $@
perreal
  • 94,503
  • 21
  • 155
  • 181