0

to be short and sweet, this is happening right now and has never happened to me before today:

  g++ assn1m.c segment.cpp
/tmp/cc2yUKRO.o: In function `bit_to_ascii(char const*, char*)':
segment.cpp:(.text+0x0): multiple definition of `bit_to_ascii(char const*, char*)'
/tmp/cc2Xgj7t.o:assn1m.c:(.text+0x0): first defined here
/usr/lib/gcc/i586-suse-linux/4.4/../../../crt1.o: In function `_start':
/usr/src/packages/BUILD/glibc-2.10.1/csu/../sysdeps/i386/elf/start.S:115: undefined reference to `main'
collect2: ld returned 1 exit status
Chimera
  • 5,884
  • 7
  • 49
  • 81
  • Seems like in files assn1m.c and segment.cpp function bit_to_ascii defined... – ForEveR Jul 13 '12 at 22:32
  • 2
    i suspect that its in a header that is not protected from multiple inclusions. It is of course completely impossible for us to diagnose tho without seeing any code – pm100 Jul 13 '12 at 22:33
  • @pm100: There's no way to prevent that sort of double inclusion from two different TUs. The correct action is to mark the function `inline` – Mooing Duck Jul 13 '12 at 22:58

1 Answers1

2

You have two definitions of the function bit_to_ascii(char const*, char*), one of which is in assn1m.c and the other of which is in segment.cpp. This is often caused by defining it in a header file without inline and including it in multiple source files.

Note that in C99, the inline specifier works slightly differently than in C++. You may have to define it as extern inline to get it to work properly.

You also didn't define the main function anywhere for your program's entry point, or you're not linking in the object file where it's defined, though that's a separate issue from the multiple definitions error.

Adam Rosenfield
  • 390,455
  • 97
  • 512
  • 589
  • What? In what case in C++ does one ever have to write `extern inline`? – Mooing Duck Jul 13 '12 at 22:59
  • Not in C++, but in C99: see [this question](http://stackoverflow.com/questions/216510/extern-inline). For a header that needs to be used in C and C++, it would be better to use a macro that expands to either `inline` or `extern inline` depending on whether the code is being compiled as C or C++. – Adam Rosenfield Jul 14 '12 at 15:24
  • ah, if it's a C compatable header... I get it – Mooing Duck Jul 14 '12 at 16:14