www.digitalmars.com         C & C++   DMDScript  

c++ - newbie needs help with multiple source files

reply Kathryn <Kathryn_member pathlink.com> writes:
I have found myself copying and pasting certain functions into all of the
programs I've written and decided I should maybe put them in their own source
files. My programs are in a separate folder from the digital mars compiler. If
the function source files are in the same folder as the main program, I have no
problem compiling. But I really think that the source files with commonly used
functions should be in one central location (ie, the digital mars folder). How
can I compile the programs with the other source files in a different location?
I tried to figure out how to write a "myfunctions.h" but that didn't get
anywhere, either.

-kathryn
Feb 20 2006
parent reply Bertel Brander <bertel post4.tele.dk> writes:
Kathryn wrote:
 I have found myself copying and pasting certain functions into all of the
 programs I've written and decided I should maybe put them in their own source
 files. My programs are in a separate folder from the digital mars compiler. If
 the function source files are in the same folder as the main program, I have no
 problem compiling. But I really think that the source files with commonly used
 functions should be in one central location (ie, the digital mars folder). How
 can I compile the programs with the other source files in a different location?
 I tried to figure out how to write a "myfunctions.h" but that didn't get
 anywhere, either.
A quick example. First a header file, called mine.h, stored in folder1: #ifndef MINE_H_INC #define MINE_H_INC void Func(int i); extern int y; #endif Then the source for the .h, to be put into mine.c in folder1: #include "mine.h" #include <stdio.h> int y; void Func(int i) { printf("Old: %d, new %d\n", y, i); y = i; } Then an application that uses the header file and its function, to be put into app1.c, in some other folder: #include "mine.h" #include <stdio.h> int main() { y = 12; Func(34); printf("Y: %d\n", y); return 0; } Now, assuming that you compile from the commandline: dmc app1.c ..\folder1\mine.c -I..\folder1 You have to adjust the path to mine.c and mine.h I don't think you should put your own code into the compilers folder, but that up to you. -- Absolutely not the best homepage on the net: http://home20.inet.tele.dk/midgaard But it's mine - Bertel
Feb 20 2006
next sibling parent Kathryn <Kathryn_member pathlink.com> writes:
Thanks for the quick and clear reply...it's working fine, now. I'll take your
advice on not putting my functions in the compiler file.

-kathryn

In article <dtdq12$2rr$1 digitaldaemon.com>, Bertel Brander says...
A quick example.

First a header file, called mine.h, stored in folder1:

#ifndef MINE_H_INC
#define MINE_H_INC

void Func(int i);
extern int y;

#endif

Then the source for the .h, to be put into mine.c in folder1:

#include "mine.h"
#include <stdio.h>

int y;

void Func(int i)
{
    printf("Old: %d, new %d\n", y, i);
    y = i;
}

Then an application that uses the header file and its function,
to be put into app1.c, in some other folder:

#include "mine.h"
#include <stdio.h>

int main()
{
    y = 12;
    Func(34);
    printf("Y: %d\n", y);
    return 0;
}

Now, assuming that you compile from the commandline:
dmc app1.c ..\folder1\mine.c -I..\folder1

You have to adjust the path to mine.c and mine.h

I don't think you should put your own code into the compilers
folder, but that up to you.

-- 
Absolutely not the best homepage on the net:
http://home20.inet.tele.dk/midgaard
But it's mine - Bertel
Feb 20 2006
prev sibling parent Kathryn <Kathryn_member pathlink.com> writes:
Thanks for the quick and clear reply...it's working fine, now. I'll take your
advice on not putting my functions in the compiler file.

-kathryn

In article <dtdq12$2rr$1 digitaldaemon.com>, Bertel Brander says...
A quick example.

First a header file, called mine.h, stored in folder1:

#ifndef MINE_H_INC
#define MINE_H_INC

void Func(int i);
extern int y;

#endif

Then the source for the .h, to be put into mine.c in folder1:

#include "mine.h"
#include <stdio.h>

int y;

void Func(int i)
{
    printf("Old: %d, new %d\n", y, i);
    y = i;
}

Then an application that uses the header file and its function,
to be put into app1.c, in some other folder:

#include "mine.h"
#include <stdio.h>

int main()
{
    y = 12;
    Func(34);
    printf("Y: %d\n", y);
    return 0;
}

Now, assuming that you compile from the commandline:
dmc app1.c ..\folder1\mine.c -I..\folder1

You have to adjust the path to mine.c and mine.h

I don't think you should put your own code into the compilers
folder, but that up to you.

-- 
Absolutely not the best homepage on the net:
http://home20.inet.tele.dk/midgaard
But it's mine - Bertel
Feb 20 2006