www.digitalmars.com Home | Search | C & C++ | D | DMDScript | News Groups | index | prev | next
Archives

D Programming
D
D.gnu
digitalmars.D
digitalmars.D.bugs
digitalmars.D.dtl
digitalmars.D.dwt
digitalmars.D.announce
digitalmars.D.learn
digitalmars.D.debugger

C/C++ Programming
c++
c++.announce
c++.atl
c++.beta
c++.chat
c++.command-line
c++.dos
c++.dos.16-bits
c++.dos.32-bits
c++.idde
c++.mfc
c++.rtl
c++.stl
c++.stl.hp
c++.stl.port
c++.stl.sgi
c++.stlsoft
c++.windows
c++.windows.16-bits
c++.windows.32-bits
c++.wxwindows

digitalmars.empire
digitalmars.DMDScript

c++ - newbie needs help with multiple source files

↑ ↓ ← 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
↑ ↓ 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
→ 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
→ 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