Possible reasons for symbol multiply defined other than 'extern' - c++

Is there any reasons for 'symbol multiply defined' other than not having the declaration in .h, having it as 'extern', and have the implementation in .cpp?
I'm pretty sure that all my files follow the rule, but I'm getting an error message like this:
ld: lto: could not merge in /Users/zlw/Library/Developer/Xcode/DerivedData/Wireless -
amjmgyrircjezdhegioctszbcypz/Build/Intermediates/Wireless.build/Debug/Wireless.build/Objects
normal/x86_64/qam.o because 'Linking globals named '_Z12SNRFromSNRdBd': symbol multiply
defined!', using libLTO version 'LLVM version 3.3svn, from Apple Clang 5.0 (build
500.2.76)' for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
Is the message means that all the troubles have something to do with 'qam.h' or 'qam.cpp'?
Is there any reasons other that 'extern' or is there any ways to see what is wrong with my code in Xcode?
Thank you very much!

it says that when you compile qam.cpp, you use a symbol named _Z12SNRFromSNRdBd (corresponding to SNRFromSNRdB(double)) which is defined more than once.
You should search for that function and who is implementing it.
Note : to convert from "mangled name" to human readable, you can use c++filt
bruce#lorien:~$ c++filt _Z12SNRFromSNRdBd
SNRFromSNRdB(double)

I hope you can past your related code. That is clear.
I got the similar error I hope may help you.
That is A Function I declare in a.h and implement in a.c, then I invoke in b.c. It does work. If I change the a.c and b.c to a.cpp and b.cpp, it is wrong.
The reason is CPP will change your function name for polymorphic.

Related

Duplicate symbol error adding new library [duplicate]

This question already has an answer here:
Odd duplicate symbols error
(1 answer)
Closed 7 years ago.
working in the ns-3 environment, I have made a library where there are some methods called in my code. In order to compile the library I add in the wscript file the link to the library and I control if it is already define as follow:
#ifndef MY_LIBRARY_H_
#define MY_LIBRARY_H_
.. my methods
#endif
When I build the code this following error is generated:
duplicate symbol __Z8getValueiib in:
src/model/bs-phy.cc.1.o
src/model/ue-phy.cc.1.o
ld: 3 duplicate symbols for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
I think this is due to the fact that I use my methods in more then one class and maybe there are some error for multi-compiling. Any idea to solve the problems? (I'm not an expert and maybe I'm missing something!!!)
Thanks for the help!!
[EDIT]
uint32_t findOutSector()
{
uint32_t sector = 0;
return sector;
}
Is your function findOutSector written in the header directly? If yes, put the definition in a .c file (+ compile & link) and just the function declaration in the header. The function should just exist once in all compiled objects and the declaration serves as reference to it.

A C++ Implementation of Hidden Markov Model Dekang Lin install error

I have download the A C++ Implementation of Hidden Markov Model written by Dekang Lin, but I caught a error when I type make in src directory.
Thanks #Michael, this problem solved.
In tables.cpp, add #include <cstdlib>.
In genseq.cpp and trainhmm.cpp, add #include <stdlib.h>.
Error info:
g++ -c -o tables.o -g tables.cpp
tables.cpp: In member function 'bool OneDTable::rand(long unsigned int&)':
tables.cpp:155:24: error: '::rand' has not been declared
tables.cpp:155:34: error: 'RAND_MAX' was not declared in this scope
make: *** [tables.o] Error 1
You shouldn't be calling your compiler yourself. That project contains a Makefile, so run make and better fix what's broken in the Makefile and sources than try to individually build .os yourself. But:
Probably that table.cpp is missing the #include line where the header would have been included where OneDTable::rand(..) would have been declared. Also, missing ::rand indicates that there are missing standard library includes. This doesn't shine a great light on the integrity of that source code. Maybe, just maybe, as a C++ beginner, you don't want to start off with broken C++ that's 12 years old.
If you're not overly set on pure C++, you can use gHMM, which is much more mature (and written in C, so works flawlessly in C++, but isn't as much object-oriented). Also, HMMlib looks pretty nice.

Linker error when declaring a function just before calling it

I've got an update function in my game that contains the following code:
void DrawMiniFPSCounter();
DrawMiniFPSCounter();
The DrawMiniFPSCounter() function is declared in a file called miniFPSCounter.cpp, which is part of the build target (I'm using Xcode). When building, I get a linker error saying that the DrawMiniFPSCounter symbol cannot be found. I've tried removing the declaration above and just calling DrawMiniFPSCounter() but that results in a 'symbol not found' error during compilation. Why would the linker have trouble finding this symbol? Is it something to do with the order in which symbols are resolved in the project?
EDIT: I ran the command nm hrMiniFPSCounter.o | grep Draw in my build directory, and got the following output:
00000000 T __Z15DrawMiniCounteriiiii
0002d040 S __Z15DrawMiniCounteriiiii.eh
00000a00 T __Z18DrawMiniFPSCounterv
0002d148 S __Z18DrawMiniFPSCounterv.eh
00000560 t __ZL9DrawDigitiiib
0002d128 s __ZL9DrawDigitiiib.eh
is this normal? Why the extra characters on the end of the function names?
In my experience most common "errors":
Was the file (really) compiled?
Was it (really) linked correctly?
Did you give the function the name you thought you did?
new Namespace issues :)
Are you sure that the miniFPSCounter.cpp file is compiled (/have been incouded in the project in the right way)? I guess what you are experiencing could be caused by a few different things,but in lack of more information I would say: Try to make sure that the cpp file is being compiled (maybe introduce a few syntax errors which would give rise to a compilation error if it is indeed compiled) and when you are sure about that, you can start checking for other stuff (suchas that it is being linked correctly, etc)
Edit: Putting checklist on top.

Embedding Python in C++ libraries

I'm working on embedding Python in some C++ code, but I'm getting stuck compiling it.
For a header file, I have
#include <Python.h>
I would initial try,
$g++ EmbeddedPython.cpp
but would end up getting
EmbeddedPython.cpp:1:20: error: Python.h: No such file or directory
EmbeddedPython.cpp: In function ‘int main(int, char**)’:
EmbeddedPython.cpp:6: error: ‘Py_Initialize’ was not declared in this scope
....
I then tried
g++ EmbeddedPython.cpp -I/System/Library/Frameworks/Python.framework/Versions/2.5/include/python2.5
and that got rid of the first two errors, but I still ended up with
Undefined symbols:
"_Py_Initialize", referenced from:
_main in ccxJAUAB.o
I'm a bit of new to this, but I think I'm learning fast. I believe I need to 'Link' a library, right? But which one and how? Do I need a dynamic or a static one?
I am working on a MacBook Pro.
You need to link against libpython. UNIX programmers do this with "-lpython" in the link command (ie at the end of that "g++" command). On a Mac, I think it would be "-framework Python".

C/C++ linker CALL16 reloc at xxxxx not against global symbol

I'm getting these errors while linking, both messages have to do with the same object file.
CALL16 reloc at 0x5f8 not against global symbol
and
could not read symbols: Bad value
The 2nd message seems to be the reason I'm getting the CALL16 error, but the file compiles just fine.
Any tips on fixing this?
FYI, I'm cross compiling for a MIPS target and using gcc 4.1.2
EDIT: No luck so far:
Here are my flags used:
-fPIC,-Wl,-rpath,-Wl,-O1
I've also tried the following without success:
-mno-explicit-relocs
-mexplicit-relocs
-mlong-calls
-mno-long-calls
-mxgot
-mno-xgot
Meanwhile, I'll go back to the source at this point and investigate more.
Aha!
Thanks to a colleague of mine, we found the issue.
Here was the issue:
There was a forward declaration/prototype of a function.
void FooBarIsBest(void);
Later on in the file the function was defined.
static void FooBarIsBest(void)
{
// do the best
}
The issue here was that in the prototype the keyword static was left out. So it was like a whole new function was being defined.
The CALL16 reference is used by gcc for relocatable code. The assembly code of the file showed that CALL16 was being used on this function... Which is wrong, as this function is local.
Interestingly, this code used to compile & link just fine with an older version of gcc (3.2.2).
Another lessoned learned. :)
Try -mlong-calls flag to the compiler.
Also see the manual for more specific MIPS options.