Weird 'undefined reference' error - c++

I have run into a peculiar error in a C++ project at work.
I have found a fix, but I am now really satisfied, as I would like to understand what actually causes the error.
When building this snippet of code:
#include <iostream>
#include "snmp/snmp/SW_SNMP_Values.hpp"
#include "snmp/agent/SW_Agent.hpp"
#include "snmp/agent/SW_Agent_PP.hpp"
int main()
{
//SW_Agent_PP agent;
return 0;
}
Notice that SW_Agent_PP is COMMENTED OUT!! When building this, I get a ton of undefined reference errors, for classes that are in use by the SW_Agent_PP object.
The FIX is to ACTUALLY CREATE THE OBJECT! so if I do this:
#include <iostream>
#include "snmp/snmp/SW_SNMP_Values.hpp"
#include "snmp/agent/SW_Agent.hpp"
#include "snmp/agent/SW_Agent_PP.hpp"
int main()
{
SW_Agent_PP agent;
return 0;
}
everything works fine and dandy.
How can I get linker errors for NOT using something? I would like to hear if anyone have run into similar experiences before, and if they found what caused it.
I am sorry, but I cannot release more code as it is company property.
Many thanks in advance!

Linkers are complicated and this behaviour is by no means unusual. Here's one possible explanation:
You are linking with a static library libfoo.a.
libfoo.a contains foo.o that contains SW_Agent_PP::SW_Agent_PP() and a bunch of other functions.
Another library libbar.a, listed after libfoo.a in the link line, uses a bunch of other functions from libfoo.a.
The linker processes static libraries in order and never goes back. Therefore the references in libbar.a can be only satisfied if corresponding object was pulled from libfoo.a by main().
The solution is to reorder the libraries in the link line.
There are other possible explanations. It's hard to tell without seeing actual code.

Related

Undefined reference to std::*something* when linking library staticly

I've compiled some of my code into a static library. Everything from this library begins with Glow or GLOWE prefix. At the moment, I'm testing the library in Linux (Ubuntu 14.04). I made a simple program to check if I did everything correctly.
#include <GlowSystem/Package.h>
int main(void)
{
GLOWE::Package package;
return 0;
}
GLOWE::Package is a class. It uses libzip and zlib (and standard c++ files eg. string). I link both libzip and zlib. When I try to compile, it fails with some linking errors.
Build log (at pastebin)
I thought that these errors are caused by too old libstdc++, but this code compiles:
#include <string>
using namespace std;
int main(void)
{
string a;
a.resize(5000);
return 0;
}
I'm at my wits' end and I have no idea what to do. I will appreciate any help.
It looks like your linker options are incorrect:
../GlowE/GlowEngine/bin/Debug/libGlowEngine.a /usr/lib/x86_64-linux-gnu/libzip.a /usr/lib/x86_64-linux-gnu/libz.a
Try:
-l../GlowE/GlowEngine/bin/Debug/GlowEngine -l/usr/lib/x86_64-linux-gnu/zip -l/usr/lib/x86_64-linux-gnu/z

c++: How to remove libstdc++.so.6 dependencies

I have 2 program I wrote on my windows computer using Visual Studio 2013. They run fine and work perfectly on my computer, but when I brought them over to my school account that is on a Linux machine, a problem arose. They compile and 1 ran, but the other did not. The one that did not run gave me an error:
.../lib/compat/libstdc++.so.6: version CXXABI_1.3.2 required by...
I have been doing research and I can't seem to find out what in my program would be using libstdc++.so.6, I'm not even really sure what it is or does. Since I am on a student account I can't go installing it using sudo, and it is a homework so I can't submit it using my own libraries.
Any Idea on what my program might be using that would require libstdc++.so.6?
I have 3 files: main.cpp, LinkedList.cpp and LinkedList.h.
I think it might be in main.cpp because I think it stems from a library I am including and main.cpp is the only one that uses outside libraries. Here is the list of libraries it uses:
#include <iomanip>
#include <stdio.h>
#include <fstream>
#include <ctype.h>
#include <string>
#include <iostream>
#include <vector>
#include <sstream>
#include <bitset>
#include <algorithm>
#include "LinkedList.h"
Thanks in advance!
You are trying to run a program linked against one version of the libraries under another set. That should work fine as long as the library versions aren't too far apart. In your case, the difference between libraries is just too large.
GCC (C++ in particular) has changed quite a bit lately, some programs that used to compile and run fine now blow up or don't compile at all (due to language changes, compiler bugs accepting broken code, ...), and the library ABI has also changed. Your best bet is to carry source code around, and make sure you got compatible language versions on both ends. If that is inconvenient, a solution is to make sure you have the same compiler (and other environment) at both places. The easiest way to get this is to install the same distribution and version.
First you can't remove the dependencies of libstdc++.so.6, because it's a standard C++ library.
To solve your problem you have to check whether your libstdc++.so have the right version
strings /usr/lib64/libstdc++.so.6|grep GXXABI_1.3.1
if there have no matching version, you will have 2 methods like these:
update your gcc on your school's linux OS
yum intsall gcc
download a matching libstdc++.so from this website:
download gcc || download matching libstdc++
then replace the libstdc++.so to /usr/lib64/libstdc++.so.6.*
SOLUTION
I went through a few steps to find my solution. Originally I could compile my program but could not run it.
1) My first step to solve the issue was to change my method of compiling. Originally I compiled my program with the following: g++ main.cpp LinkedList.cpp -o output. I changed it to: g++ -static main.cpp LinkedList.cpp -o output which allowed me to compile and run. This worked but static is a method to dynamically link libraries. This prevents linking with the shared libraries. This is not a good solution because it takes a lot longer and increases the file size of the executable, so I wanted to improve.
2) The second thing I did was remove using namespace std. Yes, I cheated and used it. So I went through my program and added std:: to the appropriate places.
3) The last thing I did was clean up my code. I was using a lot of libraries because my program was a large and complicated program. I was using all of the libraries I had listed in my original post. I went through my code and anywhere I was using a function from a library I would try and write my own code that would do the same thing which would result in my program not depending on those libraries. I was able to replicate a decent amount of these dependent foreign functions with my own which added lot of code, but it allowing me to remove some of these includes. My list of includes is now:
#include <fstream>
#include <string>
#include <iostream>
#include <vector>
#include <algorithm>
#include "LinkedList.h"
#include <math.h>
I am not sure exactly which step resolved my issue, but now I can compile with my preferred method, g++ main.cpp LinkedList.cpp -o output, and my program runs fine.
I hope this helps someone.

Reusing Slightly Altered Makefile for Slightly Altered Program Gives Error

I made a copy of a makefile that worked for program A for a new program called program B. To keep things simple program B has all of the same include directives as program A. The only changes made to the new makefile are the obvious changes to the list of object files and the name of the created executable. I can also be sure that the compilation error is not caused by anything in main() or any of the functions of program B. Yet somehow I have an error when I use the make command that goes:
/usr/local/triclops/lib/libtriclops.a(triclops.o): In function `triclopsGetDynamicLibPath':
triclops.cpp:(.text+0x198): undefined reference to `dladdr'
In my makeflie I have the following relevant lines:
CPPFLAGS+=-I/usr/local/triclops/include
LDLIBS+=-L/usr/local/triclops/lib
LDLIBS+=-lpgrlibdcstereo -ltriclops -lpnmutils
I appreciate you help, so thanks in advance. I do not know a lot about makeflies, so I am just trying to reuse the code effectively.
EDIT
Both program A and program B have the same include directives
#include "stereoCamera.h"
#include "Aria.h"
#include <iostream>
#include <cstdio>
#include <cv.h>
#include <highgui.h>
#include <cmath>
#include <vector>
#include <opencv2/highgui/highgui.hpp>
Program B can be thought of essentially as this plus an empty int main(){ return0;} while program A does contain much code and has been working for quite some time now.
You could try linking against libdl with -ldl added to your second LDLIBS+= line. You may also need to add the path of libdl.so typically /usr/lib/ to the first LDLIBS+= line.
I cannot answer why program A compiles with the 'same' makefile while program B fails without looking at the programs or the makefiles though.

undefined reference to function code blocks

main.cpp
#include <iostream>
#include <string>
using namespace std;
void echo(string);
int main()
{
echo("hello");
cout << "Hello world!" << endl;
return 0;
}
print.cpp
#include <iostream>
#include <string>
void echo(string code){
cout << code;
}
After compiling the code in code blocks 12.11, it gives me that error:
undefined reference to `echo(std::string)
I use windows 7 x64.
I have added the directory; Project>build options > search directories and added the current working directory.
All the files are in one console project in code blocks
I believe you should read up a bit more on namespaces usage. You are missing std in print.cpp.
Generally, while starting to learn cpp or getting a grip of the language you should always try writing full names of the classes along with the namespaces. Eventually with practice and some oversights (like now) you will learn why you really need them. In a nutshell namespaces are great:
When you are writing code over multiple files
Compartmentalize your code into separate blocks.
Also, using namespace std; should be used within cpp files mostly (otherwise headers get mangled up.
Anyways, try changing your code to this:
#include <iostream>
#include <string>
void echo(std::string code){
std::cout << code;
}
Then your results will look like this:
> g++ main.cpp print.cpp -o a.out
> ./a.out
helloHello world!
You should get more than that linker error, since you use string without any namespace in your print.cpp file. And if that source file doesn't compile it can't be linked with, and you will get the linker error you have.
Change to e.g.
void echo(std::string code) { ... }
And you do try to link with the object file created from print.cpp ?
I know this is old, but for anyone looking to solve this issue, the following may be a solution for you. If you have g++ follow c++ 11 under project->build options (check your options anyway) then you must check that box for all files you make in the project for the error to be cleared up. I had that annoying undefined reference thing too but now it is gone!
Try "Project/Properties/Build Targets tab". There you should find "Build target files" field. In that filed find "print.cpp" and click the checkbox (now the compiler will build print.cpp).
Some usefull information on Project management in CB
http://www.codeblocks.org/docs/main_codeblocks_en.html
When dealing with strings in C++ its best to sue std::string and your code seems to be wrong with a changes like using std::cout instead of plain cout another thing you need to be careful is linking your files especially files in different directories you need to tell code blocks were to find this print.cpp by going to build option and go for the search tab directory and point to where print.cpp is other wise the other approach is to just build a project which will have the main.cpp and and then add print.cpp class to current project I hope this will be of some help

Code Blocks, MinGW, Boost, and static linking issues

I am using Code Blocks with MinGW and am trying to get a simple program to compile with static linking. I have built the Boost libraries using these directions. Everything worked out fine and I was able to successfully compile this simple program (it compiles, I know it doesn't work because it exits before the message is sent to the console, but I just want it to compile).
If I have a DLL in my linker libraries, it compiles fine, but when I switch it with the static .a libraries of the same contents, I get undefined references such as "undefined reference to `_imp___ZN5boost6threadD1Ev'|".
I have no idea what the problem is and can't find the solution. I think it might have to do with linker settings but I can't find information on how to change them. I would be extremely grateful for any help that could be provided.
#include <iostream>
#include <boost/thread.hpp>
void myfunction()
{
std::cout << "this is a thread" << std::endl;
return;
}
int main()
{
boost::thread mythread(&myfunction);
return 0;
}
It's from trying to link statically when the headers are configured for a dynamic link. I explain this for libssh in this question. Poking around in boost/thread/detail/config.hpp makes me think you should #define BOOST_THREAD_USE_LIB, or use the -D flag to do the same.