Duplicate symbol error adding new library [duplicate] - c++

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.

Related

Undefined reference to linker error when using namespaces in headers in c++ [duplicate]

This question already has answers here:
What is an undefined reference/unresolved external symbol error and how do I fix it?
(39 answers)
Closed 1 year ago.
I've read through all similar "Undefined reference to" threads I could find but couldn't find a solution. Most of the other threads also involved classes which I don't plan to use for this. The program compiles executes normally if I define the function within the header instead of using an external .cc file. I feel like I'm missing something simple here.
This is the simplest test I could put together that recreates the issue I'm having.
Compiler: g++ (Debian 8.3.0-6) 8.3.0
hntest.h
namespace hntest
{
void pewpew();
}
hntest.cc
#include <iostream>
#include "hntest.h"
namespace hntest
{
void pewpew()
{
std::cout << "pew pew pew!!!" << std::endl;
}
}
hntestmain.cc
#include "hntest.h"
int main(int argc, char* argv[])
{
hntest::pewpew();
}
I'm attempting to compile with:
g++ -lstdc++ hntestmain.cc -o hntestmain
And I get the following linker error:
hntestmain.cc:(.text+0x10): undefined reference to `hntest::pewpew()'
collect2: error: ld returned 1 exit status
I have tried reading through the code of a couple popular C++ libraries as well as some of my own older C (not ++) code and makefiles but haven't been able to find my error. I'm admittedly both and amateur an a bit rusty.
What am I missing?
You are not actually compiling the cpp file that has the definition of pewpew.
Try:
g++ -lstdc++ hntestmain.cc hntest.cc -o hntestmain
The compiler needs to know about all the source files. The header file is dealt with during pre-process and knows to look in the same folder. You can imagine that if you have more files, say 10, 100 or 10000, this would become impossible to manage by using command line. That is why people created build systems like make, cmake and bazel.
For much greater detail see the answer here which is specific for your case of linker error.

g++ 'undefined reference' error when function is already defined [duplicate]

This question already has answers here:
What is an undefined reference/unresolved external symbol error and how do I fix it?
(39 answers)
Closed 2 years ago.
I have a static library, libmylib.a, which contains lots of pre-compiled objects. All the header scripts for these object are stored in /path/to/includes/. I am compiling my main script, BACnetSearch.cpp using g++ with the line below:
g++ BACnetSearch.cpp -I/path/to/includes/ -L/path/to/libraries/ -lmylib
All functions used from the library work except one, which gives the undefined reference to 'function_name'. I have checked the function has been instantiated in the appropriate header file, exists within an object in the library, and I have included it at the top of my script. The library is BACnet, so assuming the release has no bugs, where do I start looking to fix this.
Any other info you need just ask I will try to add. Thanks :)
EDIT: Error message received:
/tmp/ccDIISDz.o: In function `main':
BACnetSearch.cpp:(.text+0x67e): undefined reference to `bvlc_receive'
collect2: error: ld returned 1 exit status
EDIT 2: Auto-marked as answered elsewhere, then linked to a generic question with too many possible problems. Only possibility is that when the library is compiled, the order scripts are compiled in causes this error based from scripts dependencies on each other.
You can always start by checking that the function has been implemented. It is a common mistake to defined a prototype like so:
int isLarger();
And never implement the function, and in that case you get isLarger is not defined errors....
Looking at their site perhaps you should compile the code with a make file and like so:
make BACDL_DEFINE=-DBACDL_MSTP=1 clean all

Correct workflow in Xcode to avoid error: duplicate symbols for Architecture x86_64

Background
Hi, I am learning C++ and new to Xcode. I keep running into the error said in the title and not sure how to fix it. I wonder if learning the correct workflow in Xcode will help me avoid the problem.
Here is my directory:
What I have here is:
Under project signal, I firstly created a group named
signalMean, which has some files and it compiles just fine. Then I simply created another group named signalVariance under signal, copied the exact same files from signalMean to this new group. After I added new functions to the main.cpp file and renamed it signalVariance.cpp, I kept getting duplicated symbols error when I tried to compile.
Code
// waveforms.h
extern double InputSignal_f32_1kHz_15kHz[320];
// waveforms.cpp
#include "waveforms.h"
double InputSignal_f32_1kHz_15kHz[320] =
{+0.0000000000f, +0.5924659585f, -0.0947343455f,...}
// signalMean.cpp
#include <iostream>
#include "waveforms.h"
#define SIG_LENGTH 320
double calc_signal_mean(double *sig_src_arr, int sig_length);
double signal_mean;
int main() {
//code
}
double calc_signal_mean(double *sig_src_arr, int sig_length) {
//code
}
Error
duplicate symbol _InputSignal_f32_1kHz_15kHz in:
.../x86_64/waveforms-c8eb105c3a214768b4cffcf6a99ba09ec9961410b041f2905a2df7e9fe06655e.o
.../x86_64/waveforms-c8eb105c3a214768b4cffcf6a99ba09e2eac5ae7a592aee13a84d551d65ef983.o
duplicate symbol _main in:
.../x86_64/signalVariance.o
.../x86_64/signalMean.o
duplicate symbol _signal_mean in:
.../x86_64/signalVariance.o
.../x86_64/signalMean.o
duplicate symbol __Z16calc_signal_meanPdi in:
.../x86_64/signalVariance.o
.../x86_64/signalMean.o
ld: 4 duplicate symbols for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
Question
What should be the correct workflow in Xcode to avoid this issus if I want to keep building up the project from the previously written code like this? Each group will represent a different topic/function but they share code from each other and it makes sense to put them under the same project, e.g. signal.
I am sorry that this is a lengthy question. I'd really appreciate it if someone can point me to the right direction.
I think you are confusing Xcode groups and Xcode targets.
Groups are a method of organizing source code within your project (they also create folders in the filesystem), however, crucially to your issue, all the source code is compiled into the same target executable.
I think what you want to do is create multiple targets within the project and this allows you to define which of the source code files belongs to which of the targets. See this Apple Guide on working with targets.
If you didn't realize, you cannot have multiple copies of the same symbol (method or variable) within a single executable unit (executable or dylib).

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.

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

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.