I have a C++ app where I use some C functions. I have declared those C functions inside extern "C" {}; And it is compiling and working just fine.
I am using 'gcc' for compiling the C code, and using 'g++' for linking that object with C++ object.
GCOV fails by throwing me the following error:
hidden symbol `__gcov_init' in /usr/lib/gcc/x86_64-linux-gnu/5/libgcov.a(_gcov.o) is referenced by DSO
/usr/bin/ld: final link failed: Bad value
collect2: error: ld returned 1 exit status
I have no idea as this is new to me combining C and C++ code. Any one have idea why "gcov" failing with linking stage. If I disable those "C" functions in my app then it "gcov" is not giving me that error.
C Functions I am using just initialising a struct which has function pointers in it with some functions.
Any help is really appreciated.
Thanks
I was able to fix it by adding "-lgcov" flag while linking, but I don't know why it did not work even though "--coverage" flag should do the linking for this. Only have this problem when linking C object file with C++ object.
Here is the link for the answer: link
Related
I am getting this error in the Omnet++ project, based on the David Eckhoff Veins/Sumo Project.
The error is (maybe) because the version used in that omnet++ is 4.6 and the one I am using is the 5.0.
Nevertheless, I have been trying to find a logic solution to it without any success.
make MODE=debug all
src/TutorialAppl.cpp
In file included from src/TutorialAppl.cpp:3:
In file included from src/TutorialAppl.h:12:
In file included from C:/Users/konra/OneDrive/Escritorio/SUMO_VEINS_OMNET/veins-5.0/veins-veins-5.0/src\veins/modules/messages/WaveShortMessage_m.h:33:
C:/Users/konra/OneDrive/Escritorio/SUMO_VEINS_OMNET/veins-5.0/veins-veins-5.0/src\veins/base/utils/Coord.h:329:27: warning: 'veins::Coord::info' redeclared inline; 'dllimport' attribute ignored [-Wignored-attributes]
inline std::string Coord::info() const
^
1 warning generated.
Creating executable: out/clang-debug//VeinsTutorial_dbg.exe
out/clang-debug//src/TutorialAppl.o:(.rdata[_ZTIN5veins9BaseLayerE]+0x10): undefined reference to `typeinfo for veins::BatteryAccess'
out/clang-debug//src/TutorialAppl.o:(.rdata[_ZTIN5veins12BaseMobilityE]+0x10): undefined reference to `typeinfo for veins::BatteryAccess'
clang++.exe: error: linker command failed with exit code 1 (use -v to see invocation)
make: *** [Makefile:101: out/clang-debug//VeinsTutorial_dbg.exe] Error 1
"make MODE=debug all" terminated with exit code 2. Build might be incomplete.
Oh, this is a compiler bug (clang).
The clang compiler does NOT mark the typeinfo symbols as exportable on Windows. This usually goes unnoticed unless you are trying to use an abstract class from a base project (veins) in a project that depends on it. Abstract classes are used regularly to define interfaces in Veins, INET and OMNeT++, too.
There is a workaround for this in INET that can be implemented also in your own project (and in veins too), but it is extremely ugly and it requires the re-linking of the library each time so link time increases heavily.
Other more straightforward workarounds:
Add -Wl,--export-all-symbols to the linker command line in Veins to force the exporting of ALL symbols. (Veins is small enough not to cause issues with the 65K limit for the number of exported symbols on Windows)
Use GCC instead of clang on Windows (you should rebuild omnet++, too)
Use Linux/macOS
Use static linking
I am using gfortran compiler and ld linker in LINUX ubuntu 16.04.
When I am compiling some program written in Fortran 77, I have the following error message:
libdist.a(setup.F.o): In function `setup_':
setup.F:(.text+0x26c4): undefined reference to `mpi_send_'
setup.F:(.text+0x2b3c): undefined reference to `mpi_recv_'
setup.F:(.text+0x7984): undefined reference to `mpi_send_'
setup.F:(.text+0xb107): undefined reference to `mpi_recv_'
I guess it is about the position of error. However, it is difficult to me the find where is the error.
Can I have a better presentation of the position of error? Such as the c/cxx error: "setup.F:15:12: "
It is an address. And is is NOT a Fortran error. It is a linker error. You can get the very same error from a C or C++ code or any other compiled code linked by the same linker.
Generate debugging symbols (compiler option -g or -ggdb or similar - consult Debugging options in the GCC Fortran manual) to get something more meaningful. But if you do that you will realize that it only points you to some location where you do:
call mpi_send(...)
in some subroutine or function called setup().
So, it is not terribly useful in this case. The important thing is that you should link the appropriate MPI library. Normally that is done by calling mpif90 or mpifort or a similar wrapper which is called instead of gfortran. Consult the documentation of your MPI library implementation.
My problem is rather specific, but bear with me.
This in the end is kinda reverse engineering, but this problem in particular seems to fit more this board.
So, I have a shared object compiled for MIPS written in C++. I don't have the source code of the lib. The lib is compiled using GCC 4.3.3. I want to use functions present in this shared object in my amd64 computer running elementary OS. To do this, I used the sourcery cross compiler to cross compile some C++ code to MIPS, that would use this object.
So far I managed this except for this one compile error, which I cannot figure out. The lib is called libdvl.so, and uses as dependency libc.so.0 (and both are in the same folder as the cpp code).
mips-linux-gnu-g++ -g -L/path/to/lib -Wl,-rpath,/path/to/lib -o verifier verifier.cpp -ldvl
which gives me
(...)/mgc/embedded/codebench/bin/../lib/gcc/mips-linux-gnu/4.9.1/../../../../mips-linux-gnu/bin/ld: warning: libc.so.0, needed by /path/to/lib/libdvl.so, may conflict with libc.so.6
(...)/mgc/embedded/codebench/bin/../lib/gcc/mips-linux-gnu/4.9.1/../../../../mips-linux-gnu/bin/ld: errno##GLIBC_PRIVATE: TLS definition in (...)/mgc/embedded/codebench/bin/../mips-linux-gnu/libc/lib/libc.so.6 section .tbss mismatches non-TLS definition in /path/to/lib/libc.so.0 section .bss
/path/to/lib/libc.so.0: error adding symbols: Bad value
collect2: error: ld returned 1 exit status
So I added "-l:libc.so.0" and got this
(...)/mgc/embedded/codebench/bin/../lib/gcc/mips-linux-gnu/4.9.1/../../../../mips-linux-gnu/bin/ld: errno: TLS definition in (...)/mgc/embedded/codebench/bin/../mips-linux-gnu/libc/lib/libc.so.6 section .tbss mismatches non-TLS definition in libc.so.0 section .bss
(...)/mgc/embedded/codebench/bin/../mips-linux-gnu/libc/lib/libc.so.6: error adding symbols: Bad value
collect2: error: ld returned 1 exit status
Any idea how to solve this? I know I am using GCC 4.9.1, but I already downloaded the older code sourcery version which uses GCC 4.3.154 and got the exact same error.
EDIT 1: Exactly as Lol4t0 said, filtered using c++filt it gives an actual function name from stdc++. Using
mips-linux-gnu-g++ -g -L/path/to/lib -Wl,-rpath,/path/to/lib -I/path/to/lib -o verifier verifier.cpp -ldvl -l:libuClibc++.so.0 -l:libutil.so.0 -l:libc.so.0 -l:ld-uClibc.so.0 -nodefaultlibs
to give to libdvl its depencies (as I will not rewrite stdc++ :p), I get the following compile error:
(...)/mgc/embedded/codebench/bin/../lib/gcc/mips-linux-gnu/4.9.1/../../../../mips-linux-gnu/bin/ld: /tmp/cc66DLda.o: undefined reference to symbol '_Unwind_Resume##GCC_3.0'
(...)/mgc/embedded/codebench/bin/../mips-linux-gnu/libc/lib/libgcc_s.so.1: error adding symbols: DSO missing from command line
collect2: error: ld returned 1 exit status
I already confirmed lib dependencies and the order in which they appear.
Any thoughts on this?
Thanks for all the help.
Using -nodefaultlibs solves the first problem though.
You are linking against GLIBC (libc.so.6) and some other libc (libc.so.0).
That could never work: you have to have everything compiled and linked against a single, consistent libc.
Since your libdvl.so uses as dependency libc.so.0, and assuming you can't rebuild libdvl.so, you have to use crosscompiler that targets libc.so.0 (which is possibly dietlibc, or uClibc), and compile and link everything else using that toolchain. Your crosscompiler on the other hand appears to target GLIBC, and will not do you any good.
After a lot of trial and error, you may be able to link the final binary using inconsistent builds, and your binary may even get to main (that is very unlikely). But chances of such binary actually working correctly are minuscule.
as a project of my own, I'm writing a refactoring library (so it can by used by other applications) using libclang for code analysis.
The problem is when i try to compile my program to a static library everything is okey, but when i try to link my library (using some clang code) with some demo application (just main function calling my code) the linker is reporting:
./librefactor.a(renamer.o):(.data.rel.ro+0x60): undefined reference to 'clang::ASTConsumer::HandleInterestingDecl(clang::DeclGroupRef)'
./librefactor.a(renamer.o):(.data.rel.ro+0x80): undefined reference to `clang::ASTConsumer::HandleTopLevelDeclInObjCContainer(clang::DeclGroupRef)'
./librefactor.a(renamer.o):(.data.rel.ro+0x88): undefined reference to `clang::ASTConsumer::HandleImplicitImportDecl(clang::ImportDecl*)'
collect2: error: ld returned 1 exit status
Here is a makefile I build my library with: library makefile
Here is a makefile I build an example with: library example
I commented out some parts of the code to make the error as short as possible but they are some similiar errors (undefined reference) from other part of clang i use in my project.
Problem is I don't understand why linker can't find definitions of these functions/methods. They are not pure virtual in base class so I don't need to defined them. As I see it the linker is trying to find the definition of these in my part of the code istead in clang binaries. Could somebody please explain me where I'm wrong here and explain/repair it?
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.