How to remove ld: duplicate symbol _ in xcode 4.2 ( c++ code ) - c++

I am trying to compile the c++ code in xcode and I am getting following error:
ld: duplicate symbol _selectedFields in Library/Developer/Xcode/DerivedData/ReadHeaderTBL-arftrodtnbtmucbjkejinzonhulu/Build/Intermediates/ReadHeaderTBL.build/Debug-iphonesimulator/ReadHeaderTBL.build/Objects-normal/i386/readingTBLCPP.o and /Library/Developer/Xcode/DerivedData/ReadHeaderTBL-arftrodtnbtmucbjkejinzonhulu/Build/Intermediates/ReadHeaderTBL.build/Debug-iphonesimulator/ReadHeaderTBL.build/Objects-normal/i386/ReadFile.o for architecture i386
The symbol " selectedFields " is declared in one class and invoked from other.
It is declared as:
std::string selectedFields;
I am not getting the cause for this error.

Usually when we get this problem it's because people have declared the variable in a header file. You should define it in a header file
extern std::string selectedFields; // definition
and declare it in one source file
std::string selectedFields; // declaration
If your problem is something else then post the code. It gets a bit dispiriting to have to guess what everyone's problem is because they don't bother to post code.

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.

Possible reasons for symbol multiply defined other than 'extern'

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.

Persistent undefined reference

I am having a persistent undefined symbol error in Eclipse on MAC OS X. I can't figure
out the source of the error.
The error occurs according to the compiler when I try to use GA_Operations and gaAlgorithm->run_algorithm..... below:
int Application::execute_Algorithm()
{
if (this->GA_On)
{
GA_Operations *gaAlgorithm = new GA_Operations();
gaAlgorithm->run_algorithm(blocksSequence, bins);
}
else
{
packingAlgorithm->run_algorithm(blocksSequence, bins); return 0;
} //
return 0;
}
The error showing is:
Undefined symbols for architecture x86_64:
"binproject::GA_Operations::run_algorithm(binproject::Blocks_Sequence&, binproject::BinContainer&)", referenced from:
binproject::Application::execute_Algorithm() in Application.o
"binproject::GA_Operations::GA_Operations()", referenced from:
binproject::Application::execute_Algorithm() in Application.o
And the declaration is:
class GA_Operations {
public:
GA_Operations();
~GA_Operations();
//call from main application to execute algorithm
void run_algorithm(Blocks_Sequence &b_seq, BinContainer &container);
...
};
It also throws a similar error anytime I try to define a declared function in the
implementation (CPP) file.
Any ideas? This only seems to happen with this class.
Also, I apologize if there is a problem with the code indenting, I'm
The error you are showing is a linker error. This means that the compiler thinks something exists, but the linker can't find where you've defined (not declared) it. Somewhere you need something like this:
GA_Operations::GA_Operations()
{
// construct
}
void GA_Operations::run_algorithm(Blocks_Sequence &b_seq, BinContainer &container)
{
// stuff
}
Do you have that anywhere? If so, is it in the same file that Application::execute_Algorithm is in?
If it isn't, where is it? How are you compiling your whole program together so the stuff in these different files ends up in the same executable?
I'm not at all sure how to make Eclipse do what you need. I know it's possible, but I'm for familiar with pure command-line tools. You need to tell Eclipse that the .cpp file that contains the above definitions is part of your project and should be compiled and linked into the executable you're created.
Turns out this was an issue with linker settings. I turned on generating makefiles automatically and the problem resolved itself.

Possible to hide cv-prefixed functions in OpenCV?

I'm using C++, and compiling & linking to OpenCV2 using "g++". One thing that bothers me is that all of the old cv-prefixed functions are still available and "pollute" my application.
Is it possible to make the OpenCV1 C cv-prefixed functions unavailable in the scope of my application and just keep the OpenCV2 cv:: namespaced ones?
Note: I haven't written C in a while, so please let me know if this is a silly question.
It depends on what do you really need. If you just want to make this code:
CvArr *arr;
cvAvg(arr);
cvAcc(arr, arr);
"not working" - you can just add this:
#define cvAvg nothing_interesting_cvAvg
#define cvAcc nothing_interesting_cvAcc
//you can change nothing_interesting_... to anything, but you can't use the same text more than once
//you include files
//...
//after your include files
#undef cvAvg
#undef cvAcc
before including any OpenCV file. If you now try to compile code you will see:
error C3861: 'cvAvg': identifier not found
If you change you code to use nothing_interesting_cvAvg(arr); instead of cvAvg(arr);, it will compile fine, but linker will fail, because:
: error LNK2019: unresolved external symbol _nothing_interesting_cvAvg referenced in function _main
Note that this will work only for this 2 functions, so you will have to find all functions which you want to "disable" and write similar code manually.
Functions which use "deactivated" functions will work fine, because they are already compiled, linked, etc - you will just call them from some files without changing anything in this files.

symbol lookup error

I am writing a shared library with C++ under Ubuntu. The source code contains two files: ClassA.h and ClassA.cpp. Bellow are parts of the contents:
ClassA.h:
namespace calss_a{
class ClassA{
public:
...
void foo(int nBlockIndex);
...
}
}
ClassA.h:
namespace calss_a{
...
void classA::foo(int nBlockIndex){printf("....");}
...
}
The compiled .so file from the above source codes is used in test.cpp as follows:
...
class_a::ClassA * ptr = new class_a::ClassA();
...
ptr->foo(0);
...
However, when the executable build from test.cpp runs, there is an error as follows:
symbol lookup error: /home/hzhu/test: undefined symbol: _ZN16class_a15ClassA16fooEj
But if I change in ClassA.cpp and ClassA.h the type of foo()'s argument "nBlockIndex" from "int" to "unsigned in", and recompile the .so file, then the problem disappears. In a further experiment, the argument "nBlockIndex"'s type is still defined to be "int", but in test.cpp I call "foo" this way:
ptr->foo(1); //pass 1 instead of 0
then the problem disappear as well.
Can anyone tell me what is going on here?
Thanks.
I am a bit new to this kind of thing myself, but I am also running into a symbol lookup error and so thought I would share what I have found out.
The problem with symbol lookup errors usually is that something is being defined two different ways in two different places. If you do
nm -u your-executable | grep undefined-symbol
then you will find out where the symbols being used in an object file or executable file are defined