How to know linked Files

R

rayuthar

Hi,

I have two files A.c and B.c , i can make executable with A.c alone or
addition to B.c. If i make executable with two files , how can i know
B.c is linked with A.c?

Thanks,
Rayuthar
 
S

suresh shenoy

Hi,

I have two files A.c and B.c , i can make executable with A.c alone or
addition to B.c. If i make executable with two files , how can i know
B.c is linked with A.c?

Thanks,
Rayuthar

You could may be enable trace on when you compile to see if the files
are compiled and linked.

Suresh M. Shenoy
 
R

rayuthar

What difference would it make? Since A uses nothing that
is defined in B (if it did, you could not make an executable
from A alone), anything B contributes is inaccessible.

What is the larger problem you are trying to solve?

I want to check if A is linked with B or not, if linked i will use the
function from B else i will go for any other way.
 
R

rayuthar

What difference would it make? Since A uses nothing that
is defined in B (if it did, you could not make an executable
from A alone), anything B contributes is inaccessible.

What is the larger problem you are trying to solve?

sorry if posted twice!

If B is linked, then i will use the function from B else i will go for
my own function.

Thanks,
 
L

lithiumcat

If B is linked, then i will use the function from B else i will go for
my own function.

I was in the same situation a while back, and I haven't found anything
better than checking whether a symbol from B is present or not in the
program using dlopen/dlsym with a NULL filename.

It's not standard C (it's POSIX), and it seems to be a very heavy tool
for that purpose, so I would be glad to hear of a better solution (if
such a solution exists).
 
K

Kenneth Brody

I want to check if A is linked with B or not, if linked i will use the
function from B else i will go for any other way.

Basically, you want to determine, at runtime, if a function of a
given name existed at link time?

As others have pointed out, you can't do this in anything resembling
portable code. There may be a system-specific way of doing this,
but you would have to check in a system-specific group for such info.

What I would recommend is a slight variation on your request. Rather
than linking with B or nothing, link with B or "alternate B", and
have both include a _pointer_ to the function, with "alternate B"
having a NULL pointer.[*] Have your A code check for a non-NULL
pointer, and call the function indirectly. As an alternative to
this method of explicitly linking with "alternate B", you may be
able to supply a library which is always linked with the code. If
you link with B, the pointer symbol will be resolved, and your
"alternate B" module from the library shouldn't be included. If
you link without B, your library module with the NULL pointer
should be linked it.


[*] Technically, I don't think a code pointer can be NULL, as a
code pointer may not be represented the same as a data pointer.
(Though I have to admit that I use such a construct in my code.)
You could always make your own definition for a null code
pointer, such as:

typedef int (*IntFunc)();
#define NULL_INT_FUNC ((IntFunc)0)

--
+-------------------------+--------------------+-----------------------+
| Kenneth J. Brody | www.hvcomputer.com | #include |
| kenbrody/at\spamcop.net | www.fptech.com | <std_disclaimer.h> |
+-------------------------+--------------------+-----------------------+
Don't e-mail me at: <mailto:[email protected]>
 
B

Ben Bacarisse

Kenneth Brody said:
[*] Technically, I don't think a code pointer can be NULL, as a
code pointer may not be represented the same as a data pointer.
(Though I have to admit that I use such a construct in my code.)

You are in the clear. Function pointers can be assigned NULL and can
be compared against NULL (specifically against any null pointer
constant).
 
S

soscpd

Hi List

Can't that work like:

#ifdef whatever_in_b.c

/*call here the function/variable in b.c, set the return value to
local scope variable*/

#elif

/*use a standard value*/

#endif

Still can't see the point in do that (since it's very easy to know, on
compile/link time, if b.c is there or not), but that can work as far
as I can see, anyway. Some headers can be necessary to work with that
"solution".

Regards
Rafael
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,819
Latest member
masterdaster

Latest Threads

Top