referenced in section `.rodata' of - c++

Hey friends
I am stuck at a serious linking error, I am getting an error like this referenced in section .rodata' of xxx defined in discarded section .text._xxx.
This is how I am compiling my program
g++ main.o CPQENReportBB1.o CPQENReportBB2.o CPQ10minRecord.o PQ10minconfiguration.o ENReportConfig.o CPQFreeRecording.o CPQFreeHistogramReport.o
I have 8 files, it is compiling correctly but while linking I get the previously mentioned error. (i am using g++ version 3.4.3)
In two files I am getting this error message; I tried to shuffle the files in linking but then it is giving error in another two files.
Please give any suggestion to solve this. I am using c++ templates

2 possible reasons:
the file format and the machine architecture.
the use of __attribute__((section))

Related

How to build library function? [duplicate]

This question already has answers here:
Unresolved external symbol in object files
(31 answers)
Closed 1 year ago.
I want to build a small reusable library.
I have 2 projects, project A to build a library function (must dynamic library), and project B to run tests on it.
My problem encountered the following error:
In project B I used the following code:
#include "StdAfx.h"
#include "C:\......\projectA\Ent extension.h" (same folder with "Ent extension.cpp")
void Call_plot()
{
...=fent_select(..);
}
however when I build the project I always get an error:
Error LNK2019 unresolved external symbol "class Ent __cdecl fent_select(wchar_t const ,enum Mode)" (?fent_select##YAPEAVEnt##PEB_WW4Mode#Db###Z) referenced in function "void __cdecl" Call_plot (?Call_plot##YAXXZ)
How i can fix this problem?
Thanks you!
You need to link the object codes from both source code files together (and when you compile source code, it often turns into an object code). How to do this is somewhat dependent on your operating system and compiler. But, for example using GNU's gcc compiler, it would take the following compilation lines.
Step 1, compile the base code you want include:
gcc -g -o Ent_extension.o Ent_extension.cpp
Step 2, compile your code that wants to use the extension, telling it where the header file is for the base projectA using -I (include path):
gcc -I C:\......\projectA\ -g -o project_B.o project_B.cpp
But neither of these files is executable yet until you link them together, which takes the last step:
Link them together into an executable:
gcc -o myprogram project_B.o C:\......\projectA\Ent_extension.o
Which will create myprogram out of both your code bases.
Note: the even more advanced way is to turn the projectA code into a library and link that in, but that should be a future task.

Symbol lookup error when using -Wl,--defsym GCC option

I've got a question regarding using one of the GCC linker options: -Wl,--defsym.
Some time ago I decided to rewrite one of my projects in C++ but without using its standard library and without even linking to it (I compile .cpp source files to object files using C++ compiler but I link them using C compiler).
For that I used following compiler flags:
-fno-exceptions -fno-rtti -nostdlib -nodefaultlibs
And following linker options:
-Wl,--defsym -Wl,__cxa_pure_virtual=0
Using those flags I got my shared library compiling and linking fine.
But after I try to use my shared library in some simple program (also compiled and linked using above flags) I get following error while running it:
examples/bin/blink: symbol lookup error: examples/bin/libblink.so: undefined symbol: __cxa_pure_virtual
where blink is the name of the executable and libblink.so is the name of my shared library.
I tried to fix it and it looks like replacing --Wl,--defsym linker flag (for both executable and library) with this function:
extern "C" void __cxa_pure_virtual
{
while (true);
}
does the job. Why is the --Wl,--defsym not working in this case?
I'd also like to mention that I tested this under Windows and it works fine there.
I think that I've found an answer to my question.
Changing the symbol address from 0 to any other value fixes my issue.
So instead of having:
--Wl,--defsym --Wl,__cxa_pure_virtual=0
I have:
--Wl,--defsym --Wl,__cxa_pure_virtual=1
This way runtime linker does not look for a symbol (which I think is the case when the address is set to 0).

Yet another linking issue with unresolved symbols

I am trying to build my program in OpenCL for ARM GPU - Mali.
I have a library libMali.so, which contains necessary symbols:
arm-v7a15v4r3-linux-gnueabi-nm *root_to_lib*/libMali.so
returns lines such as
002525b4 t clCreateKernel
and many others with all the expected OpenCL symbols.
However, compiling with
arm-v7a15v4r3-linux-gnueabi-g++ -c -Wall mandelbrot.cpp -o mandelbrot.o
arm-v7a15v4r3-linux-gnueabi-g++ mandelbrot.o -o mandelbrot -L*root_to_lib* -lMali
gives me errors like
mandelbrot.cpp:(.text+0x2e4): undefined reference toclCreateKernel'`
and others with all the symbols, which are actually present in libMali.so!
So, I kept the correct order of librabies in linking command, library is on the specified path (it is indeed) and it has the symbols.
Mangling is not the issue in this case as well: extern C specifiers were used in place and you can see that the raw entries of both lib and object file are not mangled.
Trying to accomplish the same thing using the arm-v7a15v4r3-linux-gnueabi-gcc didn't bring any change apart from necessity to link more c++ libs by hand (with -L*path* -llib).
libMali.so was built with arm-v7a15v4r3-linux-gnueabi-g++/gcc/ld, so this is not the matter of toolchain version.
I've ran out of ideas. May be someone here knows more tricky parts of linking process?
EDIT:
In fact, mandelbrot.cpp is a sample code from Mali-SDK. I'm just showing my linker problem on this example, since there obviously are no problems in the code. You can see the code here:
http://malideveloper.arm.com/downloads/deved/tutorial/SDK/opencl/mandelbrot_8cpp_source.html
http://malideveloper.arm.com/downloads/deved/tutorial/SDK/opencl/mandelbrot_8cl_source.html
If you look closely at the nm output:
002525b4 t clCreateKernel
you'll notice that the symbol is marked with a lowercase 't' which indicates that the symbol has a local binding (for example a static function) so it's not considered for binding to a undefined symbol in another object file. You can find an explanation of most of the cryptic "symbol type" letters used by nm here: https://sourceware.org/binutils/docs/binutils/nm.html
The readelf utility's output is more clear about symbols types.
Maybe the library was built incorrectly?

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.

Sun CC: where does symbol "PointZERO" come from? It's not in my source code

This is a really weird problem. I'm compiling a C++ project on Solaris 10, with cc "Sun C 5.7 2005/01/07" and Bison 1.25 (if that matters). After finally getting the libs built, it still fails to link the executables, with following error message:
Undefined first referenced
symbol in file
PointZERO /export/build/proj/lib/libMainLib.so
ld: fatal: Symbol referencing errors. No output written to bin/the_exe
Does anyone know this "PointZERO" symbol? It's not in my code; nor is the string contained in any of my libraries. I've never seen this name before. And I haven't found anything about this on Google...
Well, ouch... The error was induced by an #include <...> statement in one of the source files, which (for unknown reasons) was used to include a file from a completely different project.
Very embarrassing for a number of reasons. Lesson: more thorough code reviews before checking in.