How to obtain library name at compile/preprocessor time?

B

babuyama

Hi,
Is there a way to obtain library name at compile/preprocessor time?

Assuming that the compilation unit, myfile.c is part of mylib.a, from
myfile.c code at compile/preprocessor time, I would like to know that
the library name is "mylib". I could not find an equivalent of
__FUNCTION__, __FILE__ defines for getting the library name. Is there
another way?
Thanks,
Babu
 
A

Alexei A. Frounze

babuyama said:
Hi,
Is there a way to obtain library name at compile/preprocessor time?

Assuming that the compilation unit, myfile.c is part of mylib.a, from
myfile.c code at compile/preprocessor time, I would like to know that
the library name is "mylib". I could not find an equivalent of
__FUNCTION__, __FILE__ defines for getting the library name. Is there
another way?

No, there's no standard way telling any linked componentes. It's not
specified by the C standard.
Alex
 
B

babuyama

Thanks for the replies. In general, I agree that if the compilation
step does not have the a-priori knowledge of the library it is going to

be part of - then it cann't know the library name.


But, in my case, during the build process, I know what source files
belong to what library. So, I was wondering if there is any compiler
flag that can be used to pass the library name on the compile line,
which will be available in the source file?
 
C

Charles Mills

Hi,
Hi,
Is there a way to obtain library name at compile/preprocessor time?

Assuming that the compilation unit, myfile.c is part of mylib.a, from
myfile.c code at compile/preprocessor time, I would like to know that
the library name is "mylib". I could not find an equivalent of
__FUNCTION__, __FILE__ defines for getting the library name. Is there
another way?
Thanks,
Babu

As mentioned there is no way to do this with in the C standard. But
chances are your compiler supports command line arguments like
'-DLIBRARY_NAME=mylib.a' or something similar. If your using make
files it should be possible (or easy for that matter) to automate this
using make rules. Perhaps check a unix or make related newsgroup.

Good luck,
Charlie
 
R

Roberto Waltman

babuyama said:
Is there a way to obtain library name at compile/preprocessor time?
myfile.c code at compile/preprocessor time, I would like to know that
the library name is "mylib". I could not find an equivalent of
__FUNCTION__, __FILE__ defines for getting the library name. Is there
another way?

No. You invoke the compiler and generate an object file. How can the
compiler know if that object file will be used as-is, of first
inserted into a library A, or not used at all, or emailed to somebody
who will then insert it into library B, or ...

It is possible that development environment in which the file is
compiled as part of a "project" could define symbols identifying the
ultimate target. Any such mechanism would be unique to that
environment and not usable in generic C.

Since you use the ".a" suffix for a library I assume you are using a
variant of Unix/Linux. On these environments you can always define a
variable in the environment or in a make file, and pass it to the
compiler using "-Dxxxxx"

Roberto Waltman

[ Please reply to the group, ]
[ return address is invalid. ]
 
K

Kenneth Brody

babuyama said:
Thanks for the replies. In general, I agree that if the compilation
step does not have the a-priori knowledge of the library it is going to
be part of - then it cann't know the library name.

But, in my case, during the build process, I know what source files
belong to what library. So, I was wondering if there is any compiler
flag that can be used to pass the library name on the compile line,
which will be available in the source file?

Perhaps something like "-DLIBRARY=mylib"? Check your compiler's docs
for how to define preprocessor values on the command line.

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

Keith Thompson

babuyama said:
Is there a way to obtain library name at compile/preprocessor time?

Assuming that the compilation unit, myfile.c is part of mylib.a, from
myfile.c code at compile/preprocessor time, I would like to know that
the library name is "mylib". I could not find an equivalent of
__FUNCTION__, __FILE__ defines for getting the library name. Is there
another way?

There's nothing portable (i.e., defined by the C standard). I don't
know of any system-specific solutions either. In typical
implementations, __FUNCTION__ and __FILE__ are expanded before the
code is put into the library; it would be difficult to have something
similar be expanded when the library is created.

<OT>
mylib.a, assuming a Unix-like system, is probably an archive of object
files. You can always extract the objects from the library and put
them into another library; this would make anything that refers to
"mylib.a" invalid.
</OT>
 
K

Keith Thompson

Ian said:
How can the compiler know where its output will end up??

It can't, unless you tell it (and promise not to move the output
somewhere else later).
 
A

Alexei A. Frounze

Alexei A. Frounze said:
No, there's no standard way telling any linked componentes. It's not
specified by the C standard.
Alex

However, there's one thing that you can do about it...
Implement a special list structure like this:
typedef tLibListNode {
const char* pLibName;
struct tLibListNode* pNext;
} tLibListNode;
Implement a function taking a string (for pLibName) and adding an item on
the list.
Then make in all your libraries a call to this function to register
themselves on this list. It's better to have initialization functions for
each of the libraries and put this call in this function. Then from main()
you call all the initialization functions of all the libraries and get their
information on the list...

Is that any better?

Alex
 
B

babuyama

If I use /D ( equivalent of -D) /DMYLIB_NAME=mylib.a, then MYLIB_NAME
is NOT being treated as a string - it is being treated as an integer. I
can do #ifdef MYLIB_NAME or #if etc.

But, what do I have to do to pass MYLIB_NAME to a function taking
(char *) as parameter? I tried with .NET c/c++ compiler.
 
G

Gordon Burditt

If I use /D ( equivalent of -D) /DMYLIB_NAME=mylib.a, then MYLIB_NAME
is NOT being treated as a string - it is being treated as an integer. I
can do #ifdef MYLIB_NAME or #if etc.

You need the equivalent of:
#define MYLIB_NAME "mylib.a"
(note the quotes). How you get that past your command line
interpreter depends on the command line interpreter.
In UNIX, you might pass:

"-DMYLIB_NAME=\"mylib.a\""
or '-DMYLIB_NAME="mylib.a"'
But, what do I have to do to pass MYLIB_NAME to a function taking
(char *) as parameter? I tried with .NET c/c++ compiler.

Gordon L. Burditt
 

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,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top