473,320 Members | 1,914 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Compiling with One Header File.

2
Hello,
I am trying to compile two programs using the same header
file and definitions. I need help on the makefile part to make this work.

================================
My headerfile looks like this:

#ifdef ONE
void hello(int s);

#else
int pin;
int verification(char* name);

#endif
=============================

My makefile

SRCONE = hello.c input.c
OBJONE = hello.o input.o

SRCTWO = name.c account.c
OBJTWO = name.o account.o

INCLUDEPATH = ../include/
CFLAGS += -I$(INCLUDEPATH)

all: one two

one: $(OBJONE)

two: $(OBJTWO)

===============================

How can I include the flag -DONE in my makefile so that
it includes void hello(int s); for "one" and
int pin;
int verification(char* name);
for "two"
Oct 21 '14 #1
2 1333
weaknessforcats
9,208 Expert Mod 8TB
The first thing about header files is that they cannot contain any code. Like defining an int. Or anything else that occupies memory or generates instructions.

When this header is used in multiple source files you get an int in every object file. When the object files are linked, the linker can fail on multiple definitions.

So if you share an int in multiple source files, you put the int in it's own source file and add this file to your build. Access from the other source files is accomplished by using extern.

The #ifdef (or #ifndef) only protects against multiple declarations if the include file is included more than in the same source file.

You should have:

Expand|Select|Wrap|Line Numbers
  1. Header one:
  2.  
  3.  void hello(int s); 
  4.  
  5.  
Expand|Select|Wrap|Line Numbers
  1. Header two:
  2.  
  3. int verification(char* name);
  4.  
  5.  
Expand|Select|Wrap|Line Numbers
  1. Source file for global variables:
  2.  
  3. int pin;
  4.  
Expand|Select|Wrap|Line Numbers
  1. Access the global variable from another source file:
  2.  
  3. extern int pin;
Oct 22 '14 #2
sakebu
2
It actually works the way I have it.
My solution was that for each .o file in my ONE program I add to CFLAGS like this:

hello.o: CFLAGS+= -DONE
input.o: CFLAGS+= -DONE

This will automatically attach -DONE during compilation and select the correct definitions in the header file for program ONE.

And for program TWO I do not need to do anything since it will go to the else portion of the header file.
Oct 22 '14 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

6
by: Suzanne | last post by:
Hi++, I get a link error when I try to call a template function that is declared in a header file and defined in a non-header file. I do *not* get this link error if I define the template...
2
by: A | last post by:
Hi, Can you actually compile header files? I'm writing a header file named "test.h" using Visual C++ 6.0 and I get the following error message when I try to compile it: "no compile tool...
12
by: Steven T. Hatton | last post by:
I recently came across the suggestion that it might be beneficial to create a header file containing all the Standard Headers, and include that in all the places where declarations from the...
6
by: alan | last post by:
Dear all, I have written my own function by C. And my development platform is W2k with VC6.0. Then I also defined a header file to extern declare this function. After that, I include this...
3
by: Sujan Datta | last post by:
What are the possible effects of modifying an existing header file, which includes bunch of defines, function prototypes and some struct definitions. The structure of the header file looks...
18
by: Al | last post by:
I'm still trying to do this but it never worked! In a .cpp file, I write the code, and at the beginning, I write: #ifndef MYLIST_H #define MYLIST_H ....to end: #endif What's wrong with it for...
7
by: The Cool Giraffe | last post by:
Please note that i do intend to use a header file. However, i'm not sure if it's really needed or just a convention. Suppose we have the following two files. // Something.h class Something {...
6
by: Gaijinco | last post by:
I'm having a weird error compiling a multiple file project: I have three files: tortuga.h where I have declared 5 global variables and prototypes for some functions. tortuga.cpp where I...
2
by: toonces | last post by:
I am doing cross compiling between C++ and C compiler for header files and desperately need help on figuring out namespace issue error generated by C++ compiler. Here are two header files:abc1.h,...
7
rampraveen
by: rampraveen | last post by:
hai guys..just now before i download one header file for c.but i do't know how to use that,..tat header file is needed for another compiling of program....any body know pls tell.(in windows XP)
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.