Help me: how to determine constant value at link time?

In ASM51, the following code is every useful:


-------- file1.asm ---------
public MAX
MAX EQU 10

-------- file2.asm ---------
extrn number(MAX)
MOV	A,#MAX

The point is that the constant value of MAX in file2.asm can be determined at link time.

I want the same effect in C51, that's to say, to define a constant as "extern". I have tried a lot of ways. One of them is:

-------- file2.C -----------
extern code MAX;
#define MAX_NUMBER ((unsigned char)&MAX)

C51 is trying to treat MAX_NUMBER as a constant, but the compiled result is much longer than the original one. Is there any better way???

Parents
  • I think you're looking at the wrong type of "constant" on the C side of that problem. C preprocessor macros are absolutely and strictly local to each source file. They're gone completely before the code even reaches the main compiler, let alone anything gets written into any object file. In this respect, C macros are completely different than what you're used to find in macro assemblers.

    C doesn't really have the concept of a symbolic constant, certainly none that could be linked in from another, separately compiled source file.

    OTOH: why insist on having all this work done by the linker? A simple shared #include file containing a line like

    #define MAX 10

    should suffice.

Reply
  • I think you're looking at the wrong type of "constant" on the C side of that problem. C preprocessor macros are absolutely and strictly local to each source file. They're gone completely before the code even reaches the main compiler, let alone anything gets written into any object file. In this respect, C macros are completely different than what you're used to find in macro assemblers.

    C doesn't really have the concept of a symbolic constant, certainly none that could be linked in from another, separately compiled source file.

    OTOH: why insist on having all this work done by the linker? A simple shared #include file containing a line like

    #define MAX 10

    should suffice.

Children
More questions in this forum