Linker error for Raspberry Crosscompile - c++

So I installed the toolchain given by raspberry which works good , I managed to compile the library used by the GUI
I have installed all dependancies for the GUI using
xapt -a armhf -m [packet]
But when the linker has to link all librairies it give me the following error and I'm stuck.
arm-linux-gnueabihf-ld: pkcs11dialogs.o: undefined reference to symbol '_Znwj##GLIBCXX_3.4'
/usr/arm-linux-gnueabihf/lib/libstdc++.so.6: error adding symbols: DSO missing from command line
Any help will be very appreciate !
Merci !

The problem is that you're compiling C++ code and not C. C++ needs a runtime support library, the native GCC library is called libstdc++ and you need to link with that.
It's very easy to do: Use the g++ (arm-linux-gnueabihf-g++ in your case) frontend program to link as well as compile., it will automatically add the C++ runtime library. Or add it manually to your linker command.

Related

g++ linker error despite specifying the lib files [duplicate]

I use c++11, but also some libraries that are not configured for it, and need some type conversion. In particular I need a way to convert std::__cxx11::string to regular std::string, but googling I can't find a way to do this and putting (string) in front does not work.
If I do not convert I get linker errors like this:
undefined reference to `H5::CompType::insertMember(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, unsigned long, H5::DataType const&) const'
Is it possible that you are using GCC 5?
If you get linker errors about undefined references to symbols that involve types in the std::__cxx11 namespace or the tag [abi:cxx11] then it probably indicates that you are trying to link together object files that were compiled with different values for the _GLIBCXX_USE_CXX11_ABI macro. This commonly happens when linking to a third-party library that was compiled with an older version of GCC. If the third-party library cannot be rebuilt with the new ABI then you will need to recompile your code with the old ABI.
Source: GCC 5 Release Notes/Dual ABI
Defining the following macro before including any standard library headers should fix your problem: #define _GLIBCXX_USE_CXX11_ABI 0
If you can recompile all incompatible libs you use, do it with compiler option
-D_GLIBCXX_USE_CXX11_ABI=1
and then rebuild your project. If you can't do so, add to your project's makefile compiler option
-D_GLIBCXX_USE_CXX11_ABI=0
The define
#define _GLIBCXX_USE_CXX11_ABI 0/1
is also good but you probably need to add it to all your files while compiler option do it for all files at once.
When I had similar issue it's happened because my lib was build using clang++, and it's linked to libstdc++.so by default on my system. While app binary was build using clang and linked with -lc++ option.
Easiest way to check dependencies is to perform ldd libName.so
To fix it you should use the same library on in app and library.
Easiest way. Build library using clang++ and compile app using clang++. Without extra linking options on both steps. Default stdlib will be used.
Build library with -stdlib=c++ and compile app with -lc++. In this case both library and app will use libc++.so.
Build library without extra options and link binary to -lstdc++. In this case both library and app will use libstdc++.so.
Answers here mostly focus on short way to fix it, but if that does not help, I'll give some steps to check, that helped me (Linux only):
If the linker errors happen when linking other libraries, build those libs with debug symbols ("-g" GCC flag)
List the symbols in the library and grep the symbols that linker complains about (enter the commands in command line):
nm lib_your_problem_library.a | grep functionNameLinkerComplainsAbout
If you got the method signature, proceed to the next step, if you got no symbols instead, mostlikely you stripped off all the symbols from the library and that is why linker can't find them when linking the library. Rebuild the library without stripping ALL the symbols, you can strip debug (strip -S option) symbols if you need.
Use a c++ demangler to understand the method signature, for example, this one
Compare the method signature in the library that you just got with the one you are using in code (check header file as well), if they are different, use the proper header or the proper library or whatever other way you now know to fix it
I got this, the only way I found to fix this was to update all of mingw-64 (I did this using pacman on msys2 for your information).
For me -D_GLIBCXX_USE_CXX11_ABI=0 didn't help.
It works after I linked to C++ libs version instead of gnustl.
I had a similar issue recently while trying to link with the pre-built binaries of hdf5 version 1.10.5 on Ubuntu 16.04. None of the solutions suggested here worked for me, and I was using g++ version 9.1. I found that the best solution is to build the hdf5 library from source. Do not use the pre-built binaries since these were built using gcc 4.9! Instead, download the source code archives from the hdf website for your particular distribution and build the library. It is very easy.
You will also need the compression libraries zlib and szip from here and here, respectively, if you do not already have them on your system.
In my case, I was having a similar problem:
/usr/bin/ld: Bank.cpp:(.text+0x19c): undefined reference to 'Account::SetBank(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >)' collect2: error: ld returned 1 exit status
After some researches, I realized that the problem was being generated by the way that Visual Studio Code was compiling the Bank.cpp file. So, to solve that, I just prompted the follow command in order to compile the c++ file sucessful:
g++ Bank.cpp Account.cpp -o Bank
With the command above, It was able to linkage correctly the Header, Implementations and Main c++ files.
OBS: My g++ version: 9.3.0 on Ubuntu 20.04
I've encountered similar problems
It turns out my project was using gcc 7 and g++ 9 and tried to link together object files compiled by those two and it all messed up.
Make sure you use the same compiler versions in all your project.

OpenFrameworks and Boost

In the last days I have been learning about the Boost and Open Frameworks. I did some programs using them, however when I use them in the same program g++ gives me the error
undefined reference to `boost::system::generic_category()'
still I'm linking boost_system. Is there some reason to not use both libraries in the same program?
That is a linker error, it needs an object file or library that provides the definition of function boost::system::generic_category.
Normally, to link to boost::system library add -lboost_system to the linker command line.
See Link Your Program to a Boost Library for more details.
Here is the openFrameworks addon for Boost.
Download and see 'How to use' instructions.

Undefined reference to .. error (Linux) - Compiles fine in OSX

Have wasted almost full 4 days trying to compile this package. It compiles fine in OSX 10.6, but gives Undefined reference errors when I try to compile it on a linux (Kubuntu 10.04, 3.8.0.27 kernel) machine.
The error seem to be that the makefiles are ordered wrong, but AFAIK, I am the only one having trouble compiling it. So I'm trying to find what's making the difference. The software package is quite big and editing the Makefiles and moving 30-50 libraries here and there doesn't seem like a good idea.
Here's the differences I think I found so far
Compiler - gcc-4.7 (Linux) and llvm-gcc-4.2 (OSX)
Compiler flags --shared (Linux) and -dynamic -dynamiclib -undefined dynamic_lookup (OSX)
Anyone have any suggestions?
I tried using clang++ and llvm-gcc-4.7 as the compiler, but I think it still used the same linker (ld?). So I could try to specify to use llvm? How do I do that?
is --shared flag somehow different from the dynamic -dynamiclib -undefined dynamic_lookup flags in OSX?
Does the linux kernel or distribution matter? (I think they compiled it fine on a CentOS machine)
Please help.
Thanks a lot.
Compiled it with gcc 4.4 and worked flawlessly. I guess the order doesn't matter on 4.4 for the given package.
The undefined references type of errors can be caused by a symbol not being compiled in, not being linked or being linked out of order. The way to debug this is to check the linker line, the symbol that the linker complains about. The error message will probably tell you what object file has the dependency.
Now, you need to find out whether the symbol is compiled or linked, for that you will need to find if it is in any of the object files or in any of the libraries and which. You can use the nm command line tool to list the symbols that are defined in any given .o or library. If the symbol is not there, then you need to figure out what to add to the linker line and that will solve it.
If the symbol appears in one library, then identify which of the libraries depends on that symbol (from the linker error message) and the library that contains it. The former must be listed before the latter in the linker command line (assuming static linking).
As a simple hack, although I recommend against it, you can instruct the gcc linker to do multiple passes by using the --start-group and --end-group command line options. Although I really recommend that you figure out the order of dependencies, as that will also give you a better insight into your project.

Possible linker error when executing program on external Unix-machine

I've written a program which compiles and runs as expected (without problems) on my Ubuntu 12.10 distribution at home. I then transfer my program to my university server, where I was first met with an error (similar to this question)
/usr/lib/libstdc++.so.6: version 'GLIBCXX_3.4.9' not found (required by ./main)
As suggested in the thread, I then added -static-libstdc++ to the linker settings, such that I use static linking. Now I get the error
undefined symbol: _ZNSt8time_getIwSt19istreambuf_iteratorIwSt11char_traitsIwEEE2idE
I haven't been able to find any information about this online, but I thought perhaps it is yet another linker problem. If anyone has some input, I would be happy to read it as I am all out of good ideas.
This is a pretty standard example of compiling against one version of the C runtime library and trying to run it against another. Statically compiling should help.
Did you add -static when building?

C++ Linker Error SDL Image - could not read symbols

I'm trying to use the SDL_Image library and I've added the .so to the link libraries list for my project (I'm using Code::Blocks, by the way).
After doing this, when I go to compile, I get this error:
Linking console executable: bin/Debug/ttfx
/usr/lib32/libSDL_image-1.2.so: could not read symbols: File in wrong format
What does this mean and how can I get it working?
Edit: I'm using gcc.
Thanks!
During the linking step there are incompatibilities since some of your object files were compiled for 32-bit and some for 64-bit. Looking at its path libSDL_image.so was probably compiled for 32-bit.
If you use the GNU compiler add -m32 to your CXXFLAGS to compile your objects for 32-bit, too.
This link and this one suggest that the problem is you're doing a 64-bit compile with a 32-bit library. YMMV