0

First, some minimal code:

// file "t1.h":
int get1();

// file "t1.c":
int get1(){return 1;}

// file "t2.h":
int get2();

// file "t2.c":
int get1(){return 99;} // <- duplicate definition of get1(), I would like to hide it somehow
int get2(){return 2;}

// file "t3.c":
#include <stdio.h>
#include "t1.h"
#include "t2.h"

int main(int argc, char ** argv) {
    printf("%d\n", get1());
    printf("%d\n", get2());
}

Compilation command

gcc -c t1.c  &&  gcc -c t2.c  &&  gcc -c t3.c  &&  gcc t1.o t2.o t3.o -o t3`

Result is error:

t2.c:(.text+0x0): multiple definition of 'get1'.

Problem is clear, get1() is defined twice.

Why I still search for solution?

  • I do not need the 2nd definition of get1(), I would like to discard it hide it.
  • Deleting it from the file, marking it static or renaming it, does not work for me, because "t1.c" and "t2.c" are generated automatically.

Question:

Is there a way to somehow hide selected symbols without modifying .c file?

Or alternatively to show only chosen symbols.

Or rename them.

Something like: gcc -c t2.c --hide-symbol=get1

More details why I want this:

There is program flex which generates lexer .c file.

There is possibility to change some behavior by re-defining macros.

I need two distinct lexers to be generated, both used by same program.

Edit: actual solution

In flex it is possible to change prefix from "yy*" to "somethingelse*" as mentioned in comments.

http://westes.github.io/flex/manual/Code_002dLevel-And-API-Options.html#index-prefix

rici
  • 234,347
  • 28
  • 237
  • 341
dnsmkl
  • 792
  • 5
  • 17
  • How would `main` know which `get1` to call? – cadaniluk Nov 22 '15 at 17:18
  • My idea (wish) is, to some how delete `get1` from `t2.o`. This way for `main` only one `get1` would be available (only in `t1.o`), so there would be no ambiguity. – dnsmkl Nov 22 '15 at 17:24
  • 1
    Note that `flex` allows you to generate lexers with a chosen prefix (in place of the default `yy`) without needing any redefine shenanigans. `flex -P pfx` generates code using `pfx` instead of `yy` as the prefix. – Jonathan Leffler Nov 22 '15 at 17:40
  • 1
    The short answer to 'Is there a way to somehow hide selected symbols without modifying .c file?' is "Not in a portable, generic manner". If you give details of your specific platform, compiler etc, there might be a way to do it, but the objective is flawed. C expects you to decide which symbols you need, and any given symbol that is globally visible needs to be defined exactly once, in one object file (or object file in a library). If an object is forcibly defined twice, that is a linking error, as you are aware. Simply "don't do it"; it won't work. – Jonathan Leffler Nov 22 '15 at 17:44
  • @JonathanLeffler Thanks, somehow I missed that.. – dnsmkl Nov 23 '15 at 17:13
  • You're welcome. It was good that you included the snippet of context that allowed me to point you to the extra feature of `flex`. The Bison parser generator has a similar option for a similar reason. – Jonathan Leffler Nov 23 '15 at 17:22
  • related question: http://stackoverflow.com/questions/678254/what-should-i-do-if-two-libraries-provide-a-function-with-the-same-name-generati – dnsmkl Dec 06 '15 at 16:34

1 Answers1

0

The programmer can hide symbols by using the keyword static. Any symbol not declared static is visible in the global namespace of the object file.

Possibly there is an object file editor that allows you to rename exported symbols but Google showed no results. You can always edit the binary object file - make sure the names remain the same length so as not to confuse the linker later.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Paul Ogilvie
  • 25,048
  • 4
  • 23
  • 41