Unix Programming - Multi file program woes

This is Interesting: Free IT Magazines  
Home > Archive > Unix Programming > August 2004 > Multi file program woes





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author Multi file program woes
Andrew Falanga

2004-08-18, 5:57 pm

Hi,

Ok, I'm in the midst of writing a program that uses several object
files. The problem I'm running in to is that one of my object files
must use some functions defined in one of the other object files. So, I
compile the other object files first and then when make gets to the last
object file, that uses functions from other object files, I get the
following error:


g++: cannot specify -o with -c or -S and multiple compilations

How do I get the one object file to use functions in the other?

---------------------------------------------
Andrew R. Falanga (a non-HP employee)
Hewlett-Packard Company
11311 Chinden Blvd.
Boise, Idaho
---------------------------------------------
Please note: The e-mail address is purposely
mangled. I do not wish my account at HP to
become a spam haven.
Pascal Bourguignon

2004-08-18, 5:57 pm

Andrew Falanga <falandr@hp.com> writes:

> Hi,
>
> Ok, I'm in the midst of writing a program that uses several object
> files. The problem I'm running in to is that one of my object files
> must use some functions defined in one of the other object files. So,
> I compile the other object files first and then when make gets to the
> last object file, that uses functions from other object files, I get
> the following error:
>
>
> g++: cannot specify -o with -c or -S and multiple compilations
>
> How do I get the one object file to use functions in the other?


You don't need the other object files to compile a source using
function in these other source files!

What you need are declarations.

To ensure that all declarations are the same (those used in the
sources implementing these functions and those used in the client
code), the best is to put them in one place: the "header" file that is
included in both.


So you will have:

f1.h:
#ifndef f1_h
#define f1_h
extern void f1_f(int x)
#endif


f1.c:
#include <f1.h>

void f1_f(int x){
do_something_with(x);}

f2.c:
#include <f1.h>

void f2_f(char y){
f1_f(5550690);
do_something_else_with(y);}

Makefile:
all: pgm
pgm: f1.o f2.o
ld -o $@ $<
f1.o:f1.h f1.c
g++ -c -o $@ f1.c
f2.o:f1.h f2.c
g++ -c -o $@ f2.c

--
__Pascal Bourguignon__ http://www.informatimago.com/

Our enemies are innovative and resourceful, and so are we. They never
stop thinking about new ways to harm our country and our people, and
neither do we.
Jens.Toerring@physik.fu-berlin.de

2004-08-18, 8:48 pm

Andrew Falanga <falandr@hp.com> wrote:
> Ok, I'm in the midst of writing a program that uses several object
> files. The problem I'm running in to is that one of my object files
> must use some functions defined in one of the other object files. So, I
> compile the other object files first and then when make gets to the last
> object file, that uses functions from other object files, I get the
> following error:


> g++: cannot specify -o with -c or -S and multiple compilations


> How do I get the one object file to use functions in the other?


Having had a very similar problem (i.e. the same error message) just
a few days ago where the actual problem was just a space character in
something like

-D\"abcd \"

where it should have been just

-D\"abcd\"

(and I don't understand yet why) I guess that you should better post
the whole command line instead of looking for an explanation
involving the order you compile object files...

Regards, Jens
--
\ Jens Thoms Toerring ___ Jens.Toerring@physik.fu-berlin.de
\__________________________ http://www.toerring.de
Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com