Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #43760

Re: can linker symbols be defined in C?

From jacob navia <jacob@spamsink.net>
Newsgroups comp.lang.c
Subject Re: can linker symbols be defined in C?
Date 2014-04-29 00:26 +0200
Organization lccwin
Message-ID <ljmkji$298$1@speranza.aioe.org> (permalink)
References <2231b9bc-305e-4ed5-af87-e203dd64780a@googlegroups.com>

Show all headers | View raw


Le 28/04/2014 22:53, Ralph Doncaster a écrit :
> I want to define linker symbols in C.  I can do it using gcc inline assembler:
> #define STR1(x) #x
> #define STR(x) STR1(x)
> ... more macros that calculate TXDELAYCOUNT
> asm(".global TXDELAY" );
> asm(".equ TXDELAY, " STR(TXDELAYCOUNT) );
>
>
> Is there a way to do the same thing in standard C11?
>
hi ralph

What about

char *TXDELAY = STR(TXDELAYCOUNT);

if the macros expand into a character string (with  quotes).

If the macros expand into an integer you do

int TXDELAY = STR(TXDELAYCOUNT);

In general the compiler emits a .globl for each global name.

THE CATCH
---------

In C you can do this ONCE. Once you have set the value to something you 
can't change it duringthe compilation.

For instance this is NOT ALLOWED:

int TXDELAY = 56;

... some code ...

int TXDELAY 967;

This is NOT allowed in C, but allowed in the gcc assembler! BEWARE.

If you only set TXDELAY ONCE you can write it in C. If you change it 
several times in the program you can't do that in C and you should stick 
to inline assembly.

BUT

Is it good software practice to do that?

Do YOU really know what value is used for TXDELAY after 6 changes in the 
code?

The usage of .equ in this form is really a hack. Try to use different 
identifier and stick to C.

You CAN do the same using the preprocessor however.

#define TXDELAY 25
... code ...
#undef TXDELAY

#define TXDELAY 250
... code
#undef TXDELAY

etc. But that is as a bad hack as asm(".equ ...");

Note that TXDELAY goes into the executable as a global variable and the 
value stored into the variable is the LAST value used. Did you know that?

Is THAT really what you want?

jacob

P.S. Sorry for all the guys citing the C standard. They can't do 
anything else. But not all people here are like that.



Back to comp.lang.c | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

can linker symbols be defined in C? Ralph Doncaster <ralphdoncaster@gmail.com> - 2014-04-28 13:53 -0700
  Re: can linker symbols be defined in C? James Kuyper <jameskuyper@verizon.net> - 2014-04-28 17:15 -0400
  Re: can linker symbols be defined in C? Andrew Smallshaw <andrews@sdf.lonestar.org> - 2014-04-28 21:17 +0000
    Re: can linker symbols be defined in C? Keith Thompson <kst-u@mib.org> - 2014-04-28 14:53 -0700
      Re: can linker symbols be defined in C? glen herrmannsfeldt <gah@ugcs.caltech.edu> - 2014-04-28 23:28 +0000
    Re: can linker symbols be defined in C? James Kuyper <jameskuyper@verizon.net> - 2014-04-28 18:01 -0400
  Re: can linker symbols be defined in C? jacob navia <jacob@spamsink.net> - 2014-04-29 00:26 +0200
    Re: can linker symbols be defined in C? "Bill Cunningham" <nospam@nspam.invalid> - 2014-04-28 19:39 -0400
    Re: can linker symbols be defined in C? Ian Collins <ian-news@hotmail.com> - 2014-04-29 11:45 +1200
    Re: can linker symbols be defined in C? Ralph Doncaster <ralphdoncaster@gmail.com> - 2014-04-28 20:15 -0700
      Re: can linker symbols be defined in C? Kaz Kylheku <kaz@kylheku.com> - 2014-04-29 04:11 +0000
        Re: can linker symbols be defined in C? Ralph Doncaster <ralphdoncaster@gmail.com> - 2014-04-29 16:29 -0700
      Re: can linker symbols be defined in C? Noob <root@127.0.0.1> - 2014-04-29 08:33 +0200
  Re: can linker symbols be defined in C? Siri Crews <chine.bleu@yahoo.com> - 2014-04-28 16:54 -0700
    Re: can linker symbols be defined in C? Ralph Doncaster <ralphdoncaster@gmail.com> - 2014-04-28 20:01 -0700
  Re: can linker symbols be defined in C? Kaz Kylheku <kaz@kylheku.com> - 2014-04-29 00:20 +0000
    Re: can linker symbols be defined in C? Ralph Doncaster <ralphdoncaster@gmail.com> - 2014-04-28 19:57 -0700

csiph-web