LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   compiling C code (https://www.linuxquestions.org/questions/programming-9/compiling-c-code-237118/)

vose 09-30-2004 11:32 AM

compiling C code
 
Suppose I have three C source files, foo.c, bar.c and baz.c.

Scenerio One:
Every function in baz.c is declared static.
Source foo.c includes baz.c (#include "baz.c").
Source bar.c includes baz.c (#include "baz.c").
I compile (gcc) foo.c with the -c flag to produce foo.o.
I compile (gcc) bar.c with the -c flag to produce bar.o.
I execute "gcc -o a.out foo.o bar.o" to produce a.out.
What is nice (among other things) is that the source baz.c
has two independent instanciations in the executable a.out.

Scenerio Two:
Suppose I want to achieve the "nice" property expressed
in the preceeding paragraph (independent instanciations),
but no function in baz.c is declared static. Assume further
that neither foo.c nor bar.c contains "#include "baz.c".
It seems to me that there must be some tool(s) (like ld, or
gcc, or ???) which if provided appropriate arguments
(and flags) would be able to produce an a.out as in senerio
one given foo.c, bar.c and baz.c as in scenerio two.
My question is: what are those tools, and how would I use
them to accomplish that objective?

:newbie:

itsme86 09-30-2004 12:02 PM

There's no way that I know of to do what you're looking for, but for the life of me I can't figure out an advantage you gain by doing it anyway. What you've managed to do is make the functions and variables in baz.c available to both foo.c and bar.c at the price of doubling your code. What are you gaining from it?

vose 09-30-2004 12:10 PM

The instanciations are independent; they have disjoint address spaces
(for example) and side effects (involving memory) of one instance do
not interfere with side effects of the other instance.

There must be some way to do this, or else the universe is a strange
place indeed... Of corse I could achieve analogous functionality by
defining C++ classes and then declaring multiple instances of the
class, but that (i.e. C++) is NOT what I want to do...

Hko 09-30-2004 12:19 PM

Quote:

Of corse I could achieve analogous functionality by
defining C++ classes and then declaring multiple instances of the
class, but that (i.e. C++) is NOT what I want to do...
It would certainly more efficient to use C++ classes, instead of doubling functions...

I can imagine the thought of having a double instance of a variable of the same name, but what is the point of wasting memory with double functions?

I would either use C++, or just don't...

vose 09-30-2004 12:34 PM

By use of the [-Dmacro[=defn]...] flag in gcc, not only are the
variables disjoint, but the independent instanciations can
be given independent semantics (as a result of the independent
compilations which allow the inclusion of different macro definitions).

But this current discussion is orthogonal to my point... I do not
wish to ask/explain/debate whether my intentions align with the
coding styles/preferences of the majority. I already know the
answer to that: my intentions rarely do!

Instead, does anyone know the answer to my original question?

WhiteChedda 10-01-2004 06:57 AM

Quote:

Originally posted by vose
By use of the [-Dmacro[=defn]...] flag in gcc, not only are the
variables disjoint, but the independent instanciations can
be given independent semantics (as a result of the independent
compilations which allow the inclusion of different macro definitions).

But this current discussion is orthogonal to my point... I do not
wish to ask/explain/debate whether my intentions align with the
coding styles/preferences of the majority. I already know the
answer to that: my intentions rarely do!

Instead, does anyone know the answer to my original question?

No but I would like to see an example of you you are talking about. I cannot for the life of me see defining a macro that needs to have different definitions depending on which source file called it and not passing a flag in the function call being the better design.

vose 10-01-2004 07:44 AM

The better design? Religion as to how one "should" write code?
In my religion, good = fast + correct. Passing a flag wastes time!

I do not intend to start (or participate) in such religious arguments.
So please, enough about that. Instead, if someone has any ideas
about my original question, I would be most greatful for help.

WhiteChedda 10-01-2004 09:01 AM

Quote:

Originally posted by vose
The better design? Religion as to how one "should" write code?
In my religion, good = fast + correct. Passing a flag wastes time!

I do not intend to start (or participate) in such religious arguments.
So please, enough about that. Instead, if someone has any ideas
about my original question, I would be most greatful for help.

I thought maybe I might learn something, but nevermind.

vose 10-05-2004 07:27 PM

I must have been unintentionally rude. Genuinely sorry.

Anyhow, a *single* source file can be multiply instanciated to give
independent instances of code returning values for random variables
whose parametric form (semantics) can be conviently set with macros.

The bottom line is ease of maintenence (if you are comfortable with
macros), virtually no overhead, and however many independent
random number generators as my application requires.

Yes there is a price, but trading space for (less) time is a good deal
in my case (all code fits in the instruction cache), and because this
happens in the innermost loop, the results are way faster than C++.

But I am off topic... any pointers to how I can find an answer to my
original question?

Stranger 10-05-2004 09:39 PM

Correct me if I'm wrong. As I recall, non-static C functions have external linkage. Thus, trying to compile multiple copies of the same non-static functions into the same module would cause conflicts.

If you really wanted multiple copies of non-static functions from the same source file-for instance, because you don't want to maintain two separate files of basically the same code-I think you would need to use template-like macros to generate functions with different names. Of course, if you wanted to have the option of changing your implementation without changing all of your code, you could simply use more macros to call your functions.

I thought you said that you were comfortable with macros. :rolleyes:

vose 10-06-2004 08:35 AM

Read my original post. No conflicts.

Stranger 10-06-2004 10:43 AM

Rereading your original post hasn't clarified anything for me.

In scenario two, neither of the other compilation units includes baz.c. Does it stand alone? Or do you want to use headers or external declarations to refer to non-static functions and data in baz.c from the other compilations units? Do you still want to parameterize your macros to give the non-static functions different semantics for each instantiation (part of your reasoning for having independent instantiations)? If so, then I do see conflicts.

If you merely want to spawn each instance as a separate process, as I understand it, each process will run in its own protected memory space and won't have the capability of interfering with the other.

A concrete example (with code) would help.

vose 10-06-2004 11:05 AM

I had hoped there might be something analogous to the
following "pretend" answer (except unlike the pretend
answer, the analogous real answer would actually work):

gcc -c foo.c
gcc -c bar.c
gcc -c baz.c
ld --output=f.o --relocateable --MAKE_STATIC_ALL_FUNCTIONS_IN_FILE=myfile foo.o baz.o
ld --output=b.o --relocateable --MAKE_STATIC_ALL_FUNCTIONS_IN_FILE=myfile bar.o baz.o
gcc -o a.out f.o b.o

Stranger 10-06-2004 11:58 AM

Deleted due to flawed reasoning on my part.

vose 10-06-2004 12:59 PM

I botched the example by not compiling baz.c twice...
Closer to my intent would be

gcc -c foo.c
gcc -c bar.c
gcc -Dmacro=def1 -o baz1.o -c baz.c
gcc -Dmacro=def2 -o baz2.o -c baz.c
ld --output=f.o --relocateable --MAKE_STATIC_ALL_FUNCTIONS_IN_FILE=myfile foo.o baz1.o
ld --output=b.o --relocateable --MAKE_STATIC_ALL_FUNCTIONS_IN_FILE=myfile bar.o baz2.o
gcc -o a.out f.o b.o


All times are GMT -5. The time now is 01:37 AM.